Sentinel node

In computer programming, a sentinel node is a specifically designated node used with linked lists and trees as a traversal path terminator. This type of node does not hold or reference any data managed by the data structure.

Benefits
Sentinels are used as an alternative over using  as the path terminator in order to get one or more of the following benefits:
 * Marginally increased speed of operations
 * Increased data structure robustness (arguably)

Drawbacks

 * Marginally increased algorithmic complexity and code size.
 * If the data structure is accessed concurrently (which means that all nodes being accessed have to be protected at least for “read-only”), for a sentinel-based implementation the sentinel node has to be protected for “read-write” by a mutex. This extra mutex in quite a few use scenarios can cause severe performance degradation. One way to avoid it is to protect the list structure as a whole for “read-write”, whereas in the version with  it suffices to protect the data structure as a whole for “read-only” (if an update operation will not follow).
 * The sentinel concept is not useful for the recording of the data structure on disk.

Search in a linked list
Below are two versions of a subroutine (implemented in the C programming language) for looking up a given search key in a singly linked list. The first one uses the sentinel value, and the second one a (pointer to the) sentinel node  , as the end-of-list indicator. The declarations of the singly linked list data structure and the outcomes of both subroutines are the same.

First version using NULL as an end-of-list indicator
The -loop contains two tests (yellow lines) per iteration:

Second version using a sentinel node
The globally available pointer  to the deliberately prepared data structure   is used as end-of-list indicator. Note that the pointer sentinel has always to be kept at the end of the list. This has to be maintained by the insert and delete functions. It is, however, about the same effort as when using a NULL pointer.

The -loop contains only one test (yellow line) per iteration:

Python implementation of a circular doubly-linked list
Linked list implementations, especially one of a circular, doubly-linked list, can be simplified remarkably using a sentinel node to demarcate the beginning and end of the list.


 * The list starts out with a single node, the sentinel node which has the next and previous pointers point to itself. This condition determines if the list is empty.
 * In a non-empty list, the sentinel node's next pointer gives the head of the list, and the previous pointer gives the tail of the list.

Following is a Python implementation of a circular doubly-linked list:

Notice how the  method takes the node that will be displaced by the new node in the parameter. For appending to the left, this is the head of a non-empty list, while for appending to right, it is the tail. But because of how the linkage is set up to refer back to the sentinel, the code just works for empty lists as well, where  will be the sentinel node.

Search in a binary tree
General declarations, similar to article Binary search tree:

The globally available pointer  to the single deliberately prepared data structure   is used to indicate the absence of a child. Note that the pointer sentinel has always to represent every leaf of the tree. This has to be maintained by the insert and delete functions. It is, however, about the same effort as when using a NULL pointer.


 * Remarks:
 * 1) With the use of SearchWithSentinelnode searching loses the read-only property. This means that in applications with concurrency it has to be protected by a mutex, an effort which normally exceeds the savings of the sentinel.
 * 2) SearchWithSentinelnode does not support the tolerance of duplicates.
 * 3) There has to be exactly one “node” to be used as sentinel, but there may be extremely many pointers to it.