
//BTree.cpp
#include <stdio.h>
#include "BTree.h"
void ancestor1(BTNode *bt,ElemType path[],int pathlen)
{ int i;
if (bt!=NULL)
{ if (bt->lchild==NULL && bt->rchild==NULL) //*bt为叶子结点
{ printf(" %c结点的所有祖先结点: ",bt->data);
for (i=pathlen-1;i>=0;i--)
printf("%c ",path[i]);
printf("\n");
}
else
{ path[pathlen]=bt->data; //将当前结点放入路径中
pathlen++; //path中元素个数增1
ancestor1(bt->lchild,path,pathlen); //递归扫描左子树
ancestor1(bt->rchild,path,pathlen); //递归扫描右子树
}
}
}
void ancestor2(BTNode *bt)
{
BTNode *p;
int i;
struct
{ BTNode *s; //存放结点指针
int parent; //存放其双亲结点在qu中的下标
} qu[MaxSize]; //qu存放队中元素
int front=-1,rear=-1; //队头队尾指针
rear++;
qu[rear].s=bt; //根结点进队
qu[rear].parent=-1; //根结点没有双亲,其parent置为-1
while (front!=rear) //队不空循环
{
front++;
p=qu[front].s; //出队一个结点*p,它在qu中的下标为front
if (p->lchild==NULL && p->rchild==NULL) //若*p为叶子结点
{ printf(" %c结点的所有祖先结点: ",p->data);
i=qu[front].parent;
while (i!=-1)
{
printf("%c ",qu[i].s->data);
i=qu[i].parent;
}
printf("\n");
}
if (p->lchild!=NULL) //*p有左孩子,将左孩子进队
{
rear++;
qu[rear].s=p->lchild;
qu[rear].parent=front; //左孩子的双亲为qu[front]结点
}
if (p->rchild!=NULL) //*p有右孩子,将右孩子进队
{
rear++;
qu[rear].s=p->rchild;
qu[rear].parent=front; //右孩子的双亲为qu[front]结点
}
}
}
int main()
{ BTNode *bt;
ElemType path[MaxSize];
CreateBTree(bt,"A(B(D,E(G,H)),C(,F(I)))");//建立图的二叉链
printf("bt括号表示法:"); DispBTree(bt); printf("\n");
printf("先序遍历的递归方法:\n");
printf("输出每个叶结点的所有祖先结点:\n");
ancestor1(bt,path,0);
printf("层次遍历方法:\n");
printf("输出每个叶结点的所有祖先结点:\n");
ancestor2(bt);
}
//BTree.h
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct tnode
{ ElemType data; //数据域
struct tnode *lchild,*rchild; //指针域
} BTNode;
void CreateBTree(BTNode * &bt,char *str)
{ BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
bt=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 (bt==NULL) //*p为二叉树的根结点
bt=p;
else //已建立二叉树根结点
{ switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
void DestroyBTree(BTNode *&bt)
{ if (bt!=NULL)
{ DestroyBTree(bt->lchild);
DestroyBTree(bt->rchild);
free(bt);
}
}
int BTHeight(BTNode *bt)
{ int lchilddep,rchilddep;
if (bt==NULL) return(0); //空树的高度为0
else
{ lchilddep=BTHeight(bt->lchild); //求左子树的高度为lchilddep
rchilddep=BTHeight(bt->rchild); //求右子树的高度为rchilddep
return (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);
}
}
int NodeCount(BTNode *bt) //求二叉树bt的结点个数
{ int num1,num2;
if (bt==NULL) //空树返回0
return 0;
else
{ num1=NodeCount(bt->lchild); //求左子树结点个数
num2=NodeCount(bt->rchild); //求右子树结点个数
return (num1+num2+1); //返回和加上1
}
}
int LeafCount(BTNode *bt) //求二叉树bt的叶子结点个数
{ int num1,num2;
if (bt==NULL) //空树返回0
return 0;
else if (bt->lchild==NULL && bt->rchild==NULL)
return 1; //叶子结点时返回1
else
{ num1=LeafCount(bt->lchild); //求左子树叶子结点个数
num2=LeafCount(bt->rchild); //求右子树叶子结点个数
return (num1+num2); //返回和
}
}
void DispBTree(BTNode *bt)
{ if (bt!=NULL)
{ printf("%c",bt->data);
if (bt->lchild!=NULL || bt->rchild!=NULL)
{ printf("("); //有子树时输入'('
DispBTree(bt->lchild); //递归处理左子树
if (bt->rchild!=NULL) //有右子树时输入'.'
printf(",");
DispBTree(bt->rchild); //递归处理右子树
printf(")"); //子树输出完毕,再输入一个')'
}
}
}
输出结果:

3万+



被折叠的 条评论
为什么被折叠?



