1#include <iostream>2 using namespacestd;3typedefstructnode4 {5 intvalue;6Node *lchild,*Rchild;7}*Bitree,binode;8 /*find the parent node to insert*/9 BOOLSearchbsd (Bitree root,intData,bitree F,bitree &p)/*reference, F is the parent node.*/Ten { One if(!root) A { -p=F; - return false; the } - Else if(root->value==data) - { -p=Root; + return false; - } + Else if(data<root->value) A returnSearchbsd (root->lchild,data,root,p); at Else - returnSearchbsd (root->rchild,data,root,p); - } - voidInsertbst (Bitree &root,intData/*Junction Insertion*/ - { -Bitree p,temp;/*p is the inserted node, temp is the node that will be inserted*/ in if(!Searchbsd (root,data,null,p)) - { totemp=NewBinode; +Temp->value=data; -Temp->rchild=temp->lchild=NULL; the if(p==NULL) *root=temp; $ Else if(p->value>data)Panax NotoginsengP->lchild=temp; - Else theP->rchild=temp; + } A } the voidInordertraverse (Bitree root)/*Middle sequence Traversal binary search tree*/ + { - if(Root) $ { $Inordertraverse (root->lchild); -cout<<root->value<<" "; -Inordertraverse (root->rchild); the } - }Wuyi intMainintargcChar Const*argv[]) the { - Bitree Root; Wu intN; - inta[ -]; About while(cin>>N) $ { -root=NULL; - for(intI=0; i<n;i++) - { ACin>>A[i]; + Insertbst (Root,a[i]); the } - Inordertraverse (root); $ } the the return 0; the}
Binary search Tree