1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| void build_tree() { Node *cur_node=node[1]; scanf("%d",&tem); node[1]->num=tem; node[1]->depth=1; if(n==1) { return ; } for(int i=2;i<=n;i++) { scanf("%d",&tem); cur_node=node[1]; do{ if((tem<cur_node->num)&&(cur_node->lchild==NULL)) { node[i]->num=tem; node[i]->depth=cur_node->depth+1; cur_node->lchild=node[i]; break; } if((tem>=cur_node->num)&&(cur_node->rchild==NULL)) { node[i]->num=tem; node[i]->depth=cur_node->depth+1; cur_node->rchild=node[i]; break; } if(tem<cur_node->num) cur_node=cur_node->lchild; else cur_node=cur_node->rchild; }while(1); } }
|