Searching Searching in a binary search tree for a specific key can be programmed
recursively or
iteratively. Searching begins by examining the
root node. If the tree is Null pointer|, the key being searched for does not exist in the tree. Otherwise, if the key equals that of the root, the search is successful and the node is returned. If the key is less than that of the root, the search proceeds by examining the left subtree. Similarly, if the key is greater than that of the root, the search proceeds by examining the right subtree. This process is repeated until the key is found or the remaining subtree is \text{nil}. If the searched key is not found after a \text{nil} subtree is reached, then the key is not present in the tree.
Recursive search The following
pseudocode implements the BST search procedure through
recursion. Operations such as finding a node in a BST whose key is the maximum or minimum are critical in certain operations, such as determining the successor and predecessor of nodes. Following is the pseudocode for the operations.
Insertion Operations such as insertion and deletion cause the BST representation to change dynamically. The data structure must be modified in such a way that the properties of BST continue to hold. New nodes are inserted as
leaf nodes in the BST. Following is an iterative implementation of the insertion operation. The procedure maintains a "trailing pointer" \text{y} as a parent of \text{x}. After initialization on line 2, the
while loop along lines 4-11 causes the pointers to be updated. If \text{y} is \text{nil}, the BST is empty, thus \text{z} is inserted as the root node of the binary search tree \text{T}, if it is not \text{nil}, insertion proceeds by comparing the keys to that of \text{y} on the lines 15-19 and the node is inserted accordingly.
Deletion The deletion of a node, say \text{Z}, from the binary search tree \text{BST} has three cases: • If \text{Z} is a leaf node, it is replaced by \text{NIL} as shown in (a). • If \text{Z} has only one child, the child node of \text{Z} gets elevated by modifying the parent node of \text{Z} to point to the child node, consequently taking \text{Z}'s position in the tree, as shown in (b) and (c). • If \text{Z} has both left and right children, the
in-order successor of \text{Z}, say \text{Y}, displaces \text{Z} by following the two cases: • If \text{Y} is \text{Z}'s right child, as shown in (d), \text{Y} displaces \text{Z} and \text{Y}'s right child remain unchanged. • If \text{Y} lies within \text{Z}'s right subtree but is not \text{Z}'s right child, as shown in (e), \text{Y} first gets replaced by its own right child, and then it displaces \text{Z}'s position in the tree. • Alternatively, the in-order predecessor can also be used. The following pseudocode implements the deletion operation in a binary search tree. The \text{BST-Delete} procedure deals with the 3 special cases mentioned above. Lines 2-3 deal with case 1; lines 4-5 deal with case 2 and lines 6-16 for case 3. The
helper function \text{Shift-Nodes} is used within the deletion algorithm for the purpose of replacing the node \text{u} with \text{v} in the binary search tree \text{BST}. This procedure handles the deletion (and substitution) of \text{u} from \text{BST}. ==Traversal==