CH5_9.C
上传用户:lgb298
上传日期:2013-03-22
资源大小:1025k
文件大小:1k
- #include <stdio.h>
- #include <alloc.h>
- typedef struct node
- { int data;
- struct node *lchild,*rchild;
- }JD;
- JD *insertbst(JD *r,int x)
- { JD *p,*q,*s;
- s=(JD *)malloc(sizeof(JD));
- s->data=x; s->lchild=s->rchild=NULL;
- q=NULL;
- if(r==NULL) { r=s; return(r);}
- p=r;
- while(p!=NULL)
- { q=p;
- if(x<p->data)
- p=p->lchild;
- else
- p=p->rchild;
- }
- if(x<q->data)
- q->lchild=s;
- else
- q->rchild=s;
- return(r);
- }
- void inorder(JD *bt)
- { if(bt!=NULL)
- { inorder(bt->lchild);
- printf("%dt",bt->data);
- inorder(bt->rchild);
- }
- }
- void main()
- { static int key[]={10,18,3,8,12,2,7,3};
- JD *head=NULL;
- int i,n=8;
- for(i=0;i<n;i++)
- head=insertbst(head,key[i]);
- inorder(head);
- }