Binary search tree is also called binary search trees, binary sort tree
What kind of tree is this?
In fact, the root node of the Zuzi Shubigen node small right subtree is larger than the root node and the left and right subtree is also a binary search tree
Code comparison simple Basic recursive implementation better understand that it is difficult to understand only deleting nodes with left and right subtrees
The method is to find a minimum node in the right subtree instead of the node to be deleted and continue to delete the node in the right subtree.
See the code in detail
1#include"stdio.h"2#include"stdlib.h"3typedefstructBintree4 {5 intdata;6 structBintree *Left ;7 structBintree *Right ;8 }bintree;9 TenBintree *bintreefind (intKey,bintree *t) One { A while(!t) - { - if(key>t->data) theT=t->Right ; - Else if(key<t->data) -T=t->Left ; - Else + returnT; - } + returnNULL; A } at -Bintree *bintreeinsert (intKey,bintree *t) - { - if(!t) - { -T= (bintree*)malloc(sizeof(Bintree)); inT->data=key; -t->left=NULL; tot->right=NULL; + } - Else if(key>t->data) theT->right=bintreeinsert (key,t->Right ); * Else if(key<t->data) $T->left=bintreeinsert (key,t->Left );Panax Notoginseng Else - returnT; the } +Bintree *findmin (Bintree *t) A { the if(t) + { - while(t->Left ) $T=t->Left ; $ } - returnT; - } theBintree *delete (intKey,bintree *t) - {WuyiBintree *temp; the if(!t) -printf"Delete Failed"); Wu Else if(key<t->data) -T->left=delete (key,t->Left ); About Else if(key>t->data) $T->right=delete (key,t->Right ); - Else if(T->left && t->Right ) - { -Temp=findmin (t->Right ); A +T->data=temp->data; theT->right=delete (t->data,t->Right ); - } $ Else the { thetemp=T; the if(!t->Left ) theT=t->Right ; - Else if(!t->Right ) inT=t->Left ; the Free(temp); the } About}
Binary search tree with two-step learning algorithm