实现二叉树遍历的递归算法(求二叉树的节点总数,高度)
答案:1 悬赏:40 手机版
解决时间 2021-01-03 17:56
- 提问者网友:暗中人
- 2021-01-03 09:11
实现二叉树遍历的递归算法(求二叉树的节点总数,高度)
最佳答案
- 五星知识达人网友:北方的南先生
- 2021-01-22 06:21
#include<stdlib.h>
#include<stdio.h>
#define MAXSIZE 100
typedef char ElemType;
typedef struct bonde
{
ElemType data;
struct bonde *lchild,*rchild;
}BTree;
typedef struct
{
BTree *data[MAXSIZE];
int top;
}Stack;
BTree *CreateBTree();
void porder(BTree *T);
int leafs(BTree *T);
int depth(BTree *T);
void main()//主函数
{
BTree *b;
int m,n;
printf("Create a tree....\n\n");
b=(BTree *)malloc(sizeof(int));
b=CreateBTree();
printf("\n");
porder(b);
m=leafs(b);
printf("\nthe tree's leafs:%d\n",m);
n=depth(b);
printf("\n\nthe tree's heigh:%d\n",n);
}
void StackInit(Stack *s)
{
s->top=-1;
}
int StackEmpty(Stack s)
{
if(s.top==-1)
return 1;
else
return 0;
}
int push(Stack *s,BTree *e)
{
if(s->top==MAXSIZE-1)
return 0;
else
{
(s->top)++;
s->data[s->top]=e;
return 1;
}
}
BTree *pop(Stack *s)
{
BTree *e;
if(s->top==-1)
return NULL;
else
{
e=s->data[(s->top)--];
return e;
}
}
BTree * CreateBTree()//建树过程
{
char ch;
BTree *b;
ch = getchar();
if(ch == '@')
{
b = NULL;
}
else
{
if(!(b = (BTree *)malloc(sizeof(BTree)))) exit(-1);
b->lchild = CreateBTree();
b->data=ch;
printf("%5c",p->data);
b->rchild = CreateBTree();
}
return b;
}
void porder(BTree *T)//先序遍历该树并输出
{
Stack s;
StackInit(&s);
while (t!=NULL||!StackEmpty(s))
{
if(t!=NULL)
{
printf("%c ",t->data);
push(&s,t);
t=t->lchild;
}
else
{
t=pop(&s);
t=t->rchild;
}
}
}
int leafs(BTree *T)、、求节点总数
{
int num1,num2;
if(T==NULL)
return 0;
else
if(T->lchild==NULL && T->rchild==NULL)
return 1;
else
{
num1=leafs(T->lchild);
num2=leafs(T->rchild);
return(num1+num2);
}
}
int depth(BTree *T)//求高度
{
int dep1,dep2;
if(T==NULL)
return 0;
else
{
dep1=depth(T->lchild);
dep2=depth(T->rchild);
if(dep1>dep2)
return(dep1+1);
else
return(dep2+1);
}
}
#include<stdio.h>
#define MAXSIZE 100
typedef char ElemType;
typedef struct bonde
{
ElemType data;
struct bonde *lchild,*rchild;
}BTree;
typedef struct
{
BTree *data[MAXSIZE];
int top;
}Stack;
BTree *CreateBTree();
void porder(BTree *T);
int leafs(BTree *T);
int depth(BTree *T);
void main()//主函数
{
BTree *b;
int m,n;
printf("Create a tree....\n\n");
b=(BTree *)malloc(sizeof(int));
b=CreateBTree();
printf("\n");
porder(b);
m=leafs(b);
printf("\nthe tree's leafs:%d\n",m);
n=depth(b);
printf("\n\nthe tree's heigh:%d\n",n);
}
void StackInit(Stack *s)
{
s->top=-1;
}
int StackEmpty(Stack s)
{
if(s.top==-1)
return 1;
else
return 0;
}
int push(Stack *s,BTree *e)
{
if(s->top==MAXSIZE-1)
return 0;
else
{
(s->top)++;
s->data[s->top]=e;
return 1;
}
}
BTree *pop(Stack *s)
{
BTree *e;
if(s->top==-1)
return NULL;
else
{
e=s->data[(s->top)--];
return e;
}
}
BTree * CreateBTree()//建树过程
{
char ch;
BTree *b;
ch = getchar();
if(ch == '@')
{
b = NULL;
}
else
{
if(!(b = (BTree *)malloc(sizeof(BTree)))) exit(-1);
b->lchild = CreateBTree();
b->data=ch;
printf("%5c",p->data);
b->rchild = CreateBTree();
}
return b;
}
void porder(BTree *T)//先序遍历该树并输出
{
Stack s;
StackInit(&s);
while (t!=NULL||!StackEmpty(s))
{
if(t!=NULL)
{
printf("%c ",t->data);
push(&s,t);
t=t->lchild;
}
else
{
t=pop(&s);
t=t->rchild;
}
}
}
int leafs(BTree *T)、、求节点总数
{
int num1,num2;
if(T==NULL)
return 0;
else
if(T->lchild==NULL && T->rchild==NULL)
return 1;
else
{
num1=leafs(T->lchild);
num2=leafs(T->rchild);
return(num1+num2);
}
}
int depth(BTree *T)//求高度
{
int dep1,dep2;
if(T==NULL)
return 0;
else
{
dep1=depth(T->lchild);
dep2=depth(T->rchild);
if(dep1>dep2)
return(dep1+1);
else
return(dep2+1);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯