資源簡(jiǎn)介
二叉樹(shù)插入算法的完整版,其中包含了二叉樹(shù)的構(gòu)造,插入,遍歷,查找算法
代碼片段和文件信息
#include“stdlib.h“
#include“stdio.h“???????????
struct?btnode????????????????
{?int?d;??????????????????????
??struct?btnode?*lchild;?????
??struct?btnode?*rchild;?????
};
struct?btnode?*gouzao(int?a[]int?n)
{
??struct?btnode?*p*q*bt=NULL;
??int?i;
??for(i=0;i ???{
????p=(struct?btnode?*)malloc(sizeof(struct?btnode));
????p->d=a[i];p->lchild=NULL;p->rchild=NULL;
q=bt;
if(q==NULL)?bt=p;
else
??{
????while((q->lchild!=p)&&(q->rchild!=p))
????{?if(a[i]d)
??{?if(q->lchild!=NULL?)?q=q->lchild;
????else?q->lchild=p;
??}
??else
???{?if(q->rchild!=NULL)?q=q->rchild;
?????else?q->rchild=p;
???}
????}
??}
???}
???return(bt);
}
struct?btnode?*insort(struct?btnode?*btint?b)??
{?struct?btnode?*p*q;
??p=(struct?btnode?*)malloc(sizeof(struct?btnode));
??p->d=b;p->lchild=NULL;p->rchild=NULL;
??q=bt;
??if(q==NULL)?bt=p;??????????????????
??else???????????????????????????????
????{?while((q->lchild!=p)&&(q->rchild!=
評(píng)論
共有 條評(píng)論