Reference article: http://blog.csdn.net/ns_code/article/details/19823463
But Bo Master's use of the first method after the operation of the tree is not a binary sorting tree, it is worth pondering!!
#include"stdio.h"#include"stdlib.h"//two cross-linked table nodestypedefstructnode{intdata; structNode *lchild,*Rchild;} Node,*Bstree;/*in the two-fork sort tree referred to by pointer Ptree, the element with the keyword key is searched recursively, and if the lookup succeeds, it returns ture, and the node pointer corresponding to the found data is saved in P, otherwise 0 is returned, and the last node pointer accessed on the lookup path is saved in P. The argument f here points to the parent node of the root node of each recursive traversal of the subtree, which is always the parent node of the parameter ptree, whose initial value is null, which is intended to track the parent node of the current node accessed on the lookup path (that is, the previous access node) that function is used to be called by the subsequent insertion node function. */intSearch_node (Bstree PTree,intKey,bstree F,bstree &p) { if(!PTree) {P=F; return 0; }Else if(Key = = Ptree->data) {P=PTree; return 1; }Else if(Key > Ptree->data) {Search_node (PTree-rchild,key,ptree,p); }Else{search_node (PTree-lchild,key,ptree,p); } }/*To the binary sort tree tree insert node when a data element with the keyword key is not found in the two-fork sort trees pointed to by ptree, it is inserted into the two-fork sort tree and returns 1, otherwise 0 is returned. When the tree is empty, the insertion changes the value of the root node, so it passes in the reference. */intInsertnode (Bstree &ptree,intkey) {Bstree p; if(!Search_node (ptree,key,null,p)) {Bstree pnew= (Bstree)malloc(sizeof(Node));//nodes that produce new elementsPnew->lchild = Pnew->rchild =NULL; Pnew->data =key; if(!p) {PTree= Pnew;//How to tree empty, direct pnew to the root node}Else{ if(Key > P->data) {P->rchild = pnew;//as the right child inserts the right side of P}Else{p->lchild = pnew;//as the left child, insert the left side of P } } return 1; }Else return 0; }//create a two-fork sort treeBstree Create_bstree (int*arr,intnum) {Bstree PTree=NULL; inti; for(i=0; i<num;i++) Insertnode (Ptree,arr[i]); returnPTree; }//recursive sequential traversal of binary tree to get the ordered arrangement of elements from small to largevoidInordertraverse (Bstree pTree) {if(PTree) {inordertraverse (PTree-lchild); printf ("%d",ptree->data); Inordertraverse (PTree-rchild); }}//Modify the left subtree method to delete a nodeintDelete_node1 (Bstree &6) {Bstree q,s; if(!p->lchild) {//left dial hand tree is empty, just re-connect the right sub-treeQ =p; P= p->Rchild; Free(q); }Else if(!p->rchild) {//The right subtree is empty, just re-connect the left sub-treeQ =p; P= p->Lchild; Free(q); }Else{//if the left and right sub-trees are not empty//Here we take the method of modifying the left subtree, or you can modify the right subtree, similar to the methodQ =p; S= p->lchild;//take the left node to the truncated point while(S->rchild) {//always to the right, and finally s to the precursor node for the truncated point. //if each node element is arranged in a sequence from small to large,//The predecessor node of a node is the previous node of that node in the sequenceQ =s; S= s->Rchild; } //replace the pending point P with SP->data = s->data; //Depending on the situation, the left subtree of S is re-attached to the Q if(P! =q) {Q->rchild = s->Lchild; }Else{Q->lchild = s->Lchild; } Free(s); } return 1;}//Modify the right subtree method to delete a nodeintDelete_node2 (Bstree &p) {Bstree q,s; if(!p->lchild) {//left dial hand tree is empty, just re-connect the right sub-treeQ =p; P= p->Rchild; Free(q); }Else if(!p->rchild) {//The right subtree is empty, just re-connect the left sub-treeQ =p; P= p->Lchild; Free(q); }Else{Q=p; S= p->Rchild; while(s->lchild) {Q=s; S= s->Lchild; } //replace the pending point P with SP->data = s->data; //Depending on the situation, the left subtree of S is re-attached to the Q if(P! =q) {Q->lchild = s->Rchild; }Else{Q->rchild = s->Rchild; } Free(s); } return 1;} //Delete a node.intDelete_bstree (Bstree &ptree,intkey) { if(!ptree) {//there is no node for key keyword return 0; }Else{ if(Key = = Ptree->data) { //return Delete_node1 (pTree); returnDelete_node2 (PTree); }Else if(Key > Ptree->data) { returnDelete_bstree (ptree->Rchild,key); }Else{ returnDelete_bstree (ptree->Lchild,key); } }}intMain () {Bstree pTree; intI,num,flag; printf ("Please enter the number of nodes:"); scanf ("%d",&num);//printf ("%d\n", num); int*arr = (int*)malloc(NUM *sizeof(int)); for(i=0; i<num;i++) scanf ("%d", arr+i); PTree=Create_bstree (Arr,num); printf ("The middle sequence traverses the result of the two-fork sort tree:"); Inordertraverse (PTree);p rintf ("\ n"); printf ("Please enter the node you want to delete:"); scanf ("%d",&num); Flag=Delete_bstree (Ptree,num); if(flag) {printf ("Delete Success! \ n"); }Else{printf ("Delete failed! \ n"); } printf ("The middle sequence traverses the result of the two-fork sort tree:"); Inordertraverse (PTree);p rintf ("\ n"); return 0;}
View Code
Knocking code is good for your body and mind, Gaga
Reference book: "Big Talk data Structure"
Binary sorting trees (binary sort tree)