递归建立二叉搜索树
来源:互联网 发布:mac怎么新建txt 编辑:程序博客网 时间:2024/06/08 09:49
typedef struct Tree{ int value; struct Tree *left,*right;}Node,*ptrNode;void CreatBinTree(ptrNode &root,int key){ if(root==NULL){ptrNode tmp=new Node;tmp->value=key;tmp->left=tmp->right=NULL;root=tmp;} else if(root->value>key){ CreatBinTree(root->left,key);}else if(root->value<key){ CreatBinTree(root->right,key);}else if(root->value==key){ printf("There is a same value\n"); return;}}void main(){int a[10]={2,3,5,6,7,1,10,12,8,16};ptrNode head=NULL;for(int i=0;i<10;i++){ CreatBinTree(head,a[i]);}}