What is "sentinel element"

I am learning heap and was introduced to something called "sentinel element"as you can see “None” in the list.
I am not sure what it is, and more importantly, what it does and what happens if I don’t use the sentinel element. And I also encountered something called “sentinel node.” Do they share a same concept?

class MinHeap:

  def __init__(self):

    self.heap_list = [None]

    self.count = 0

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

1 Like

I was going to write something, but this sentence is as concise as it gets, from the link: “the standard Python sentinel is the built-in None object, used wherever some alternative needs to be provided to an integer, float, string, or other meaningful value.”

This is a very good read:
https://python-patterns.guide/python/sentinel-object/

As a side note, other algorithms will use an infinity value in a similar fashion (as in, it needs a unique value to be a boundary-setter for the logic to work).

And as a second side note: If you don’t know so already, setting a variable to a default value (None often, but anything else really) can be done in the parameters of a function when you need them to be set before any code. This is useful in many instances (recursion being one of them).

1 Like

Apologies for just replying with links but I think they’re more helpful than anything I could come out with. Perhaps someone a little better versed in CompSci than me could offer a better answer-


Not the same thing (but they do mark endings)-

2 Likes