#include #include #include #include struct tree { int x; struct tree *left,*right; } *root; void inorder(struct tree *rr) { if (rr != NULL) { inorder(rr->left); printf("%2d ",rr->x); inorder(rr->right); } } void main() { int a[15]={5,2,8,16,9,3,7,66,12,10,8,12,16,9,66},i=0,ff=0; struct tree *p1,*p2,*pp; while (i<=14) { // total have 15 data if (ff == 0) { p1=root=(struct tree *)malloc(sizeof(struct tree)); p1->x=a[i]; p1->left=p1->right=NULL; ff=1; } else { p2=root; while ((p2 != NULL) && (p2->x != a[i])) { pp=p2; if (a[i] > p2->x) p2=p2->right; else p2=p2->left; } if ((p2 != NULL) && (a[i] == p2->x)) { printf("%d is duplicate !!!\n",a[i]); } else { p1=(struct tree *)malloc(sizeof(struct tree)); p1->x=a[i]; p1->left=p1->right=NULL; if (a[i] > pp->x) { pp->right=p1; printf("process right %d --> %d\n",i,a[i]); } else { pp->left=p1; printf("process left %d --> %d\n",i,a[i]); } } } i++; } // while printf("inorder traversal -->"); inorder(root); printf("\n"); }