Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a sentinel node offer benefits over NULL?

On the Sentinel Node wikipedia page it says that the benefits of a sentinel node over NULL are :

  • Increased speed of operations
  • Reduced algorithmic code size
  • Increased data structure robustness (arguably).

I don't really understand how the checks against a sentinel node would be faster (or how to properly implement them in a linked list or tree), so I suppose this is more of a two part question:

  1. What causes the sentinel node to be a better design than NULL?
  2. How would you implement a sentinel node in (for example) a list?
like image 698
spbots Avatar asked Mar 21 '11 22:03

spbots


People also ask

Why do we need sentinel node in linked list?

We could eliminate the container object that we used for singly linked lists, since the sentinel node can keep track of both the first and last elements in the list. If we did so, then we would return a pointer to the sentinel node to the user.

What is Sentinel in data structure?

Data-structure sentinels are often used to mark the structure of data. A common example of this is the null character at the end of strings or a special sentinel to mark the end of a linked list. It is dangerous to allow this type of control data to be easily accessible.

What is sentinel node in binary tree?

sentinel : a special Node object used to indicate an absent child or parent node. For example, the root's parent is the sentinel, and a leaf's children are both the sentinel. If the binary search tree is empty, then the root is the sentinel.

What is a sentinel node Python?

This sentinel node is a special node that acts as the 0th node in the linked list. No data is stored in this special node; it's there just as a placeholder in the list. The list will be circular.


1 Answers

I think that a little code example would be a better explanation than a theoretic discussion.

The following is the code for node deletion in a doubly-linked list of nodes where NULL is used to mark the end of the list and where two pointers first and last are used to hold the address of first and last node:

// Using NULL and pointers for first and last if (n->prev) n->prev->next = n->next;         else first = n->next; if (n->next) n->next->prev = n->prev;         else last = n->prev; 

and this is the same code where instead there is a special dummy node to mark the end of the list and where the address of first node in the list is stored in the next field of the special node and where the last node in the list is stored in the prev field of the special dummy node:

// Using the dummy node n->prev->next = n->next; n->next->prev = n->prev; 

The same kind of simplification is also present for node insertion; for example to insert node n before node x (having x == NULL or x == &dummy meaning insertion in last position) the code would be:

// Using NULL and pointers for first and last n->next = x; n->prev = x ? x->prev : last; if (n->prev) n->prev->next = n;         else first = n; if (n->next) n->next->prev = n;         else last = n; 

and

// Using the dummy node n->next = x; n->prev = x->prev; n->next->prev = n; n->prev->next = n; 

As you can see the dummy node approach removed for a doubly-linked list all special cases and all conditionals.

The following picture represents the two approaches for the same list in memory...

NULL/dummy node alternatives for a doubly-linked list

like image 174
6502 Avatar answered Oct 27 '22 00:10

6502