===Deletion===
[[File: AVL-tree-deleteBST node deletion. svgpng| 600px400px|right|The node <math>\text{D}</math> to be deleted has 2 children]] ▼
Deletion of a node, say <math>\text{D}</math>, from a binary search tree <math>\text{BST}</math> should abide three cases:{{r|algo_cormen|p=295}}
#The Ifdeletion <math>\text{D}</math> isof a leaf node, the parent node's pointer tosay <math>\text{DZ}</math>, getsfrom replacedthe withbinary search tree <math>\text{NILBST}</math> andhas consequentlythree <math>\textcases:{D{r|algo_cormen|p=295-297}}</math> gets removed from the tree.
# If <math>\text{DZ}</math> hasis a single childleaf node, the childparent gets elevated as either left or right childnode of {{nowrap|<math>\text{DZ}</math>'s}} parentgets dependingreplaced on the position ofby <math>\text{DNIL}</math> within the BST, as shown in fig. 2 part (a) and part (b), and as a result,consequently <math>\text{DZ}</math> getsis removed from the tree<math>\text{BST}</math>.
# If <math>\text{DZ}</math> has bothonly a left and rightone child, the successorchild node of <math>\text{DZ}</math> (letgets itelevated, betaking <math>\text{EZ}</math> which can not have a left child) takes the's position of <math>\text{D}</math> in the tree., Thisby depends onmodifying the positionparent node of <math>\text{EZ}</math> withinto <math>\text{BST}</math>:{{r|algo_cormen|p=296}}the child node.
# If <math>\text{EZ}</math> ishas {{nowrap|<math>\text{D}</math>'s}}both immediateleft and right child, the successor of <math>\text{EZ}</math>, gets elevated andsay <math>\text{EY}</math>'s, leftdisplaces child pointer is made point to {{nowrap|<math>\text{DZ}</math>'s}} initial left sub-tree, as shown in fig.by 2the partfollowing (c).cases:
## If <math>\text{EY}</math> is not the immediate right child of <math>\text{D}</math>, deletion proceeds by replacing the position of <math>\text{E}</math> by {{nowrap|<math>\text{EZ}</math>'s}} right child, as shown in (here <math>\text{F}</math>c), and <math>\text{EY}</math> takes the position ofdisplaces <math>\text{DZ}</math> inand <math>\text{BSTY}</math>,'s asright shownchild hereremain unchanged.<ref name="Algs4">According to Sedgewick ({{Cite book
## If <math>\text{Y}</math> lies within <math>\text{Z}</math>'s right subtree but is not <math>\text{Z}</math>'s right child, as shown in (d), <math>\text{Y}</math> gets replaced by its own right child, and then it displaces <math>\text{Z}</math>'s position in the tree.
|last1=Sedgewick
|first1=Robert
|author1-link=Robert Sedgewick (computer scientist)
|last2=Wayne
|first2=Kevin
|title=Algorithms
|edition=4th
|isbn=978-0-321-57351-3
|year=2011
|publisher=Addison-Wesley Professional
|url=http://algs4.cs.princeton.edu}})
the idea of this node exchange is due to T. Hibbard.</ref>
▲[[File:AVL-tree-delete.svg|600px|The node <math>\text{D}</math> to be deleted has 2 children]]
{{clear}}
The following pseudocode implements the deletion operation in a binary search tree.{{r|algo_cormen|p=296-298}}
{|
|