Binary sorting tree: Also known as "binary lookup tree", "Binary search Tree".
A binary sort tree is an empty tree, or has the following properties:
1. If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node.
2. If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node.
3. Its left and right sub-trees are also two-fork sorting trees respectively.
Binary sort tree structure
typedef int ELEMTYPE;
typedef struct BSTNODE
{
Elemtype data;
struct bstnode* parent; Parent domain
struct bstnode* leftchild; Left child
struct bstnode* rightchild; Right child
}bstnode, *bstree;
Insert thought: Starting from the root, compare the inserted value with the data field of the current pointer, if it is equal, it cannot be inserted, if the inserted value is greater than the current pointer's data field, the pointer wants to run right, otherwise the pointer runs to the left.
Delete ideas: In three different situations:
1. Delete leaves and single branches, just adjust the corresponding leftchild and rightchild.
2. Delete the dual branch, need to find its successor, the data exchange between the two, converted to delete the subsequent relay.
3. Delete both the root node and the leaf node, then adjust the corresponding root.
It is important to summarize the idea that the deletion of all nodes is rooted in the deletion of the leaves.
Static Bstnode*Buynode () {Bstnode*S=(Bstnode*) malloc (sizeof (Bstnode)); ASSERT (S!= NULL); memset (s),0, sizeof (Bstnode));returns;} StaticvoidFreenode (Bstnode*P) {if(p== NULL) {return; } free (p); P= NULL;} Static Bstnode*Findval (Bstnode*PTR, const elemtype x) {if(PTR== NULL ||X==END) {return NULL; }if(x==Ptr -Data) {returnptr }Else if(x>Ptr -Data) {returnFindval (PTR -Rightchild, x); }Else{returnFindval (PTR -Leftchild, x); }}/** Recursive Implementation * /BOOL Insert (Bstree*Ptree, const elemtype x, Bstnode*PA) {if(Ptree== NULL)return false;if(*Ptree== NULL) {Bstnode*S=Buynode (); S -Data =X S -Leftchild=S -Rightchild= NULL;*Ptree=S S -Parent =Paif(PA!= NULL) {if(x>Pa -Data) {PA -Rightchild=S }Else{PA -Leftchild=S } }return true; } Bstnode*Ptr= *Ptreeif(x==Ptr -Data) {return false; }Else if(x<Ptr -Data) {returnInsert (&Ptr -Leftchild, x, PTR); }Else{returnInsert (&Ptr -Rightchild, x, PTR); }}/** Iterative Implementation * /BOOL Insertbst (Bstree*Ptree,const Elemtype val) {if(Ptree== NULL)return false;if(*Ptree== NULL) {Bstnode*S=Buynode (); S -Data =Val S -Leftchild=S -Rightchild= NULL;*Ptree=S S -Parent = NULL;return true; } Bstnode*Ptr= *Ptree Bstnode*Pa=ptr while(PTR!= NULL) {if(PTR -Data ==Val) {return false; }Else if(Val>Ptr -Data) {PA=ptr Ptr=Ptr -Rightchild; }Else{PA=ptr Ptr=Ptr -Leftchild; }} PTR=Buynode (); Ptr -Data =Val Ptr -Parent =Paif(Val>Pa -Data) {PA -Rightchild=ptr }Else{PA -Leftchild=ptr }return true;} Static Bstnode*Next (Bstnode*PTR) {if(PTR -Parent != NULL &&Ptr==Ptr -Parent -Leftchild) {returnPtr -Parent; }Else{PTR=Ptr -Rightchild; while(PTR!= NULL &&Ptr -Leftchild!= NULL) {PTR=Ptr -Leftchild; }returnptr }}static bool Deletebst (bstree*Ptree, Bstnode*PTR, const elemtype x) {if(PTR -Leftchild!= NULL &&Ptr -Rightchild!= NULL) {Bstnode*Res=Next (PTR); Ptr -Data =Res -Data; Ptr=Res } Bstnode*Pa=Ptr -Parent; Bstnode*Child=Ptr -Leftchild== NULL ?Ptr -Rightchild:ptr -Leftchild;if(PA== NULL) {*Ptree=Childif(Child!= NULL) {Child -Parent = NULL; } }Else{if(PTR==Pa -Leftchild) {PA -Leftchild=Child }Else{PA -Rightchild=Child }} freenode (PTR);return true; }bool Removebst (Bstree*Ptree, const elemtype x) {Bstnode*Ptr= NULL;if(Ptree== NULL || *Ptree== NULL||(PTR=Findval (*Ptree, X))== NULL) {return false; }returnDeletebst (Ptree, PTR, x);}
Insertion and deletion of binary sort trees