This article needs additional citations for verification .(January 2021) |
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.
Sentinels are used as an alternative over using NULL
as the path terminator in order to get one or more of the following benefits:
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 NULL
, and the second one a (pointer to the) sentinel node Sentinel
, as the end-of-list indicator. The declarations of the singly linked list data structure and the outcomes of both subroutines are the same.
structsll_node{// one node of the singly linked liststructsll_node*next;// end-of-list indicator or -> next nodeintkey;}sll,*first;
// global initializationfirst=NULL;// before the first insertion (not shown)structsll_node*Search(structsll_node*first,intsearch_key){structsll_node*node;for(node=first;node!=NULL;node=node->next){if(node->key==search_key)returnnode;// found}// search_key is not contained in the list:returnNULL;}
The for
-loop contains two tests (yellow lines) per iteration:
node != NULL;
if (node->key == search_key)
.The globally available pointer sentinel
to the deliberately prepared data structure Sentinel
is used as end-of-list indicator.
// global variablesll_nodeSentinel,*sentinel=&Sentinel;// global initializationsentinel->next=sentinel;first=sentinel;// before the first insertion (not shown)
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.
structsll_node*SearchWithSentinelnode(structsll_node*first,intsearch_key){structsll_node*node;// Prepare the “node” Sentinel for the search:sentinel->key=search_key;for(node=first;node->key!=search_key;node=node->next){}// Post-processing:if(node!=sentinel)returnnode;// found// search_key is not contained in the list:returnNULL;}
The for
-loop contains only one test (yellow line) per iteration:
node->key != search_key;
.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.
Following is a Python implementation of a circular doubly-linked list:
classNode:def__init__(self,data,next=None,prev=None):self.data=dataself.next=nextself.prev=prevdef__repr__(self)->str:returnf'Node(data={self.data})'classLinkedList:def__init__(self):self._sentinel=Node(data=None)self._sentinel.next=self._sentinelself._sentinel.prev=self._sentineldefpop_left(self)->Node:returnself.remove_by_ref(self._sentinel.next)defpop(self)->Node:returnself.remove_by_ref(self._sentinel.prev)defappend_nodeleft(self,node):self.add_node(self._sentinel,node)defappend_node(self,node):self.add_node(self._sentinel.prev,node)defappend_left(self,data):node=Node(data=data)self.append_nodeleft(node)defappend(self,data):node=Node(data=data)self.append_node(node)defremove_by_ref(self,node)->Node:ifnodeisself._sentinel:raiseException('Can never remove sentinel.')node.prev.next=node.nextnode.next.prev=node.prevnode.prev=Nonenode.next=Nonereturnnodedefadd_node(self,curnode,newnode):newnode.next=curnode.nextnewnode.prev=curnodecurnode.next.prev=newnodecurnode.next=newnodedefsearch(self,value):self._sentinel.data=valuenode=self._sentinel.nextwhilenode.data!=value:node=node.nextself._sentinel.data=Noneifnodeisself._sentinel:returnNonereturnnodedef__iter__(self):node=self._sentinel.nextwhilenodeisnotself._sentinel:yieldnode.datanode=node.nextdefreviter(self):node=self._sentinel.prevwhilenodeisnotself._sentinel:yieldnode.datanode=node.prev
Notice how the add_node()
method takes the node that will be displaced by the new node in the parameter curnode
. 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 curnode
will be the sentinel node.
General declarations, similar to article Binary search tree:
structbst_node{// one node of the binary search treestructbst_node*child[2];// each: ->node or end-of-path indicatorintkey;};structbst{// binary search treestructbst_node*root;// ->node or end-of-path indicator}*BST;
The globally available pointersentinel
to the single deliberately prepared data structure Sentinel = *sentinel
is used to indicate the absence of a child.
// global variablebst_nodeSentinel,*sentinel=&Sentinel;// global initializationSentinel.child[0]=Sentinel.child[1]=sentinel;BST->root=sentinel;// before the first insertion (not shown)
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.
structbst_node*SearchWithSentinelnode(structbst*bst,intsearch_key){structbst_node*node;// Prepare the “node” Sentinel for the search:sentinel->key=search_key;for(node=bst->root;;){if(search_key==node->key)break;ifsearch_key<node->key:node=node->child[0];// go leftelsenode=node->child[1];// go right}// Post-processing:if(node!=sentinel)returnnode;// found// search_key is not contained in the tree:returnNULL;}