structtreenode{Searchtree left; Searchtree right; ElementType Ele;};/*there must be an exit for recursion .*//*The recursive code is to be reused*/Searchtreeinsert (Searchtree T, X) {/*This is not the parameter in the tree, increasing the node*/ if(T = =NULL) {T=malloc(sizeof(structTreeNode)); if(T = =NULL) fatalerror (); T->ele =X; T->left =NULL; T->right =NULL:}/*continue to compare*/ Else if(T->ele >X) T->left = Insert (T->left, X);//for no left child node, the new node pointer is returned, and if so, it is equivalent to nothing when recursive returns Else if(T->ele <X) T->right = Insert (t->Right , X); returnT;} Searchtreefindmin (Searchtree T) {if(T = =NULL)returnNULL;//for the first time only if(T->left! =NULL)returnFindmin (t->Left ); returnT;}//Two-fork find tree removal routinesSearchtreedelete (Searchtree T, ElementType X) {S earchtree Tmpcell; if(T = =NULL) Error ("no Element found"); if(T->ele >X) T->left = Delete (t->Left , X); Else if(T->ele <X) Delete (T-Right , X); Else if(T->right && t->Left ) {Tmpcell= Findmin (t->Right ); T->ele = tmpcell->Ele; Delete (Tmpcell, T-Ele); } Else//Supply 1. Delete parent node with only one or no sons 2. Delete Right subtree minimum node { if(T->left = =NULL) T= t->Right ; Else if(T->right = =NULL) T= t->Left ; } returnT;}View Code
Insert and delete operations for binary lookup trees