Binary lookup tree: For each node in the tree X, its left subtree has all keywords that are less than the X keyword, and all the keywords for the right subtree are larger than the X keyword.
The average depth of the binary lookup tree is O (logn).
Binary search Tree Delete operation:
- If the node is a leaf, it can be deleted immediately.
- If there is a son, the tuning parent node pointer bypasses the node after it is deleted.
- If there are two sons, replace the node's data with the smallest data from the right subtree and delete the node recursively (now it is empty). Because the smallest node of the right subtree cannot have a left son, it is easier to delete the second time.
1 #ifndef _tree_h2 3 structTreeNode;4typedefstructTreeNode *Position;5typedefstructTreeNode *Searchtree;6 7 searchtree makeempty (Searchtree T);8 Position Find (ElementType X, Searchtree T);9 Position findmin (Searchtree T);Ten Position Findmax (Searchtree T); One searchtree Insert (ElementType X, Searchtree T); A searcgtree Delete (ElementType X, Searchtree T); - ElementType Retrieve (Position P); - the #endif - - structTreeNode - { + ElementType Element; - Searchtree left; + Searchtree right; A } at - - searchtree makeempty (searchtree T) - { - if(T! =NULL) - { inMakeempty (t->Left ); -Makeempty (t->Right ); to Free(T); + } - returnNULL; the } * $ Panax Notoginseng Position Find (ElementType X, Searchtree T) - { the if(T = =NULL) + returnNULL; A if(X < t->Element) the returnFind (X, t->Left ); + Else if(X > t->Element) - returnFind (X, t->Right ); $ Else $ returnT; - } - the - Position findmin (searchtree T)Wuyi { the if(T = =NULL) - returnNULL; Wu Else if(T->left = =NULL) - returnT; About Else $ returnFindmin (t->Left ); - } - - Position Findmax (searchtree T) A { + if(T! =NULL) the while(T->right! =NULL) -T = t->Right ; $ returnT; the } the the the searchtree Insert (ElementType X, Searchtree T) - { in if(T = =NULL) the { theT =malloc(sizeof(structTreeNode)); About if(T =NULL) thePerror ("Out of Space"); the Else the { +T->element =X; -T->left = T->right =NULL; the }Bayi } the Else if(X < t->Element) theT->left = Insert (X, t->Left ); - Else -T->right = Insert (X, t->Right ); the the returnT; the } the - the searchtree Delete (ElementType X, Searchtree T) the { the Position Tmpcell;94 the if(T = =NULL) thePerror ("Element not found"); the //Found The element to be deleted98 Else if(X < t->Left ) AboutT->left = Delete (X, t->Left ); - Else if(X > tRight )101T->right = Delete (X, t->Right );102 103 //Children104 Else if(T->left && t->Right ) the {106Tmpcell = Findmin (t->Right );107T->element = tmpcell->Element;108T->right = Delete (t->element, t->Right );109 } the //One or zero children111 Else the {113Tmpcell =T; the if(T->left = =NULL) theT = t->Right ; the Else if(T->right = =NULL)117T = t->Left ;118 Free(Tmpcell);119 } - 121 returnT;122}
Two fork find tree basic operations