Review the record, the master ignores, about the binary search tree some basic operations.
Code by pnig0s1992//date:2012,3,28 #include <stdio.h> #include <stdlib.h> typedef int Elemen
T_type;
typedef struct TREENODE * PTRTREENODE;
typedef struct TREENODE * ADTTREE;
struct TreeNode {element_type Element;
Ptrtreenode Pleftchild;
Ptrtreenode Prightchild;
}; void Makeempty (Adttree mytree); The ADT tree is empty ptrtreenode find (Element_type x,adttree);//finds the node of the specified element and returns Mytree Fi Ndmin (Adttree mytree); Find the minimum value Ptrtreenode Findmax (Adttree mytree) in the ADT Tree;//Find the maximum value ADT Insert in the Ptrtreenode tree (element_ Type X,adttree mytree);//Insert node Ptrtreenode Delete to ADT Tree (Element_type x,adttree); Delete specified node in mytree tree//adt
(Element_type x);//Construct a new ADT tree and return the root node pointer int main (int argc,char * * argv) {element_type el = 6;
Adttree NewTree = Createtree (EL);
NewTree = Insert (2,newtree);
NewTree = Insert (8,newtree);
NewTree = Insert (1,newtree);
NewTree = Insert (4,newtree); NewTree = InsERT (3,newtree);
NewTree = Delete (6,newtree);
return 0;
}//Build ADT Tree Adttree createtree (Element_type x) {Adttree newtree = (ptrtreenode) malloc (sizeof (TreeNode));
Newtree->element = x;
Newtree->pleftchild = Newtree->prightchild = NULL;
return newtree; Insert a node into the ADT Tree Ptrtreenode Insert (element_type x,adttree mytree) {if (Mytree = NULL) {mytr
EE = (ptrtreenode) malloc (sizeof (TreeNode));
if (mytree = NULL) {printf ("\nout of Space.");
else {mytree->element = x;
Mytree->pleftchild = Mytree->prightchild = NULL;
}}else if (x < mytree->element) {Mytree->pleftchild = Insert (x,mytree->pleftchild);
}else if (x > Mytree->element) {mytree->prightchild = Insert (x,mytree->prightchild);
return mytree; }//Find ADT Tree min node ptrtReenode findmin (Adttree mytree) {if (Mytree = NULL) {printf ("\nthe tree is empty.");
}else if (Mytree->pleftchild = NULL) {return mytree;
}else {return findmin (mytree->pleftchild); }///Find ADT Tree Maximum node Ptrtreenode findmax (Adttree mytree) {if (Mytree = NULL) {printf ("\nthe tr EE is empty. ");}
else if (Mytree->prightchild = NULL) {return mytree;
}else {return Findmax (mytree->prightchild);
}//delete the specified ADT tree node ptrtreenode Delete (Element_type x,adttree mytree) {Ptrtreenode Tempcell;
if (mytree = = NULL) {printf ("\nthe tree is empty.");
}else if (x < mytree->element) {Mytree->pleftchild = Delete (x,mytree->pleftchild);
}else if (x > Mytree->element) {mytree->prightchild = Delete (x,mytree->prightchild); }else if (MYTREE->pleftchild && mytree->prightchild) {Tempcell = Findmin (mytree->prightchild);
Mytree->element = tempcell->element;
Mytree->prightchild = Delete (mytree->element,mytree->prightchild);
}else {Tempcell = mytree;
if (Mytree->pleftchild = = NULL) {mytree = mytree->prightchild;
}else if (mytree->prightchild = = NULL) {mytree = mytree->pleftchild;
Free (Tempcell);
return mytree; } Ptrtreenode Find (Element_type x,adttree mytree) {if (Mytree = NULL) {printf ("\nelement Not to be found. ");}
else if (x < mytree->element) {return find (x,mytree->pleftchild);
}else if (x > Mytree->element) {return find (x,mytree->prightchild);
}else return mytree;
} void Makeempty (Adttree mytree) { if (mytree!= NULL) {makeempty (mytree->pleftchild);
Makeempty (Mytree->prightchild);
Free (mytree);
} return; }
This article is from the "About:blank h4cking" blog, please be sure to keep this source http://pnig0s1992.blog.51cto.com/393390/819473