问题、代码及运行结果:
(preparation:二叉树算法库)
btree.h:
#include
#include
#define MaxSize 100
typedef char ElemType;
typedef struct node //二叉链存储结构
{
ElemType data; //数据元素
struct node *lchild; //指向左孩子节点
struct node *rchild; //指向右孩子节点
} BTNode;
void CreateBTNode(BTNode *&b,char *str); //由str串创建二叉链
BTNode *FindNode(BTNode *b,ElemType x); //返回data域为x的节点指针
BTNode *LchildNode(BTNode *p); //返回*p节点的左孩子节点指针
BTNode *RchildNode(BTNode *p); //返回*p节点的右孩子节点指针
int BTNodeDepth(BTNode *b); //求二叉树b的深度
void DispBTNode(BTNode *b); //以括号表示法输出二叉树
void DestroyBTNode(BTNode *&b); //销毁二叉树
#include "btree.h"
void CreateBTNode(BTNode *&b,char *str) //由str串创建二叉链
{
BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL; //建立的二叉树初始时为空
ch=str[j];
while (ch!='\0') //str未扫描完时循环
{
switch(ch)
{
case '(':
top++;
St[top]=p;
k=1;
break; //为左节点
case ')':
top--;
break;
case ',':
k=2;
break; //为右节点
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=p->rchild=NULL;
if (b==NULL) //p指向二叉树的根节点
b=p;
else //已建立二叉树根节点
{
switch(k)
{
case 1:
St[top]->lchild=p;
break;
case 2:
St[top]->rchild=p;
break;
}
}
}
j++;
ch=str[j];
}
}
BTNode *FindNode(BTNode *b,ElemType x) //返回data域为x的节点指针
{
BTNode *p;
if (b==NULL)
return NULL;
else if (b->data==x)
return b;
else
{
p=FindNode(b->lchild,x);
if (p!=NULL)
return p;
else
return FindNode(b->rchild,x);
}
}
BTNode *LchildNode(BTNode *p) //返回*p节点的左孩子节点指针
{
return p->lchild;
}
BTNode *RchildNode(BTNode *p) //返回*p节点的右孩子节点指针
{
return p->rchild;
}
int BTNodeDepth(BTNode *b) //求二叉树b的深度
{
int lchilddep,rchilddep;
if (b==NULL)
return(0); //空树的高度为0
else
{
lchilddep=BTNodeDepth(b->lchild); //求左子树的高度为lchilddep
rchilddep=BTNodeDepth(b->rchild); //求右子树的高度为rchilddep
return (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);
}
}
void DispBTNode(BTNode *b) //以括号表示法输出二叉树
{
if (b!=NULL)
{
printf("%c",b->data);
if (b->lchild!=NULL || b->rchild!=NULL)
{
printf("(");
DispBTNode(b->lchild);
if (b->rchild!=NULL) printf(",");
DispBTNode(b->rchild);
printf(")");
}
}
}
void DestroyBTNode(BTNode *&b) //销毁二叉树
{
if (b!=NULL)
{
DestroyBTNode(b->lchild);
DestroyBTNode(b->rchild);
free(b);
}
}
main.cpp:
#include
#include
#include "btree.h"
BTNode *CreateBT1(char *pre,char *in,int n)
/*pre存放先序序列,in存放中序序列,n为二叉树结点个数,
本算法执行后返回构造的二叉链的根结点指针*/
{
BTNode *s;
char *p;
int k;
if (n<=0) return NULL;
s=(BTNode *)malloc(sizeof(BTNode)); //创建二叉树结点*s
s->data=*pre;
for (p=in; plchild=CreateBT1(pre+1,in,k); //递归构造左子树
s->rchild=CreateBT1(pre+k+1,p+1,n-k-1); //递归构造右子树
return s;
}
int main()
{
ElemType pre[]="ABDGCEF",in[]="DGBAECF";
BTNode *b1;
b1=CreateBT1(pre,in,7);
printf("b1:");
DispBTNode(b1);
printf("\n");
return 0;
}
main.cpp:
#include
#include
#include "btree.h"
BTNode *CreateBT2(char *post,char *in,int n)
/*post存放后序序列,in存放中序序列,n为二叉树结点个数,
本算法执行后返回构造的二叉链的根结点指针*/
{
BTNode *s;
char r,*p;
int k;
if (n<=0) return NULL;
r=*(post+n-1); //根结点值
s=(BTNode *)malloc(sizeof(BTNode)); //创建二叉树结点*s
s->data=r;
for (p=in; plchild=CreateBT2(post,in,k); //递归构造左子树
s->rchild=CreateBT2(post+k,p+1,n-k-1); //递归构造右子树
return s;
}
int main()
{
ElemType in[]="DGBAECF",post[]="GDBEFCA";
BTNode *b2;
b2=CreateBT2(post,in,7);
printf("b2:");
DispBTNode(b2);
printf("\n");
return 0;
}
#include
#include
#include "btree.h"
#define N 30
typedef ElemType SqBTree[N];
BTNode *trans(SqBTree a,int i)
{
BTNode *b;
if (i>N)
return(NULL);
if (a[i]=='#')
return(NULL); //当节点不存在时返回NULL
b=(BTNode *)malloc(sizeof(BTNode)); //创建根节点
b->data=a[i];
b->lchild=trans(a,2*i); //递归创建左子树
b->rchild=trans(a,2*i+1); //递归创建右子树
return(b); //返回根节点
}
int main()
{
BTNode *b;
ElemType s[]="0ABCD#EF#G####################";
b=trans(s,1);
printf("b:");
DispBTNode(b);
printf("\n");
return 0;
}
注:参考资料: 数据结构例程——二叉树的构造