二叉树递归的基本操作(求叶子数目、深度、路径汇总)
2011-08-02 16:48
519 查看
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define STACKMAX 20
typedef char Elemtype;
typedef int status;
typedef struct node
{
Elemtype data;
struct node *lchild,*rchild;
}*BiTree,BiTNode;
typedef Elemtype StackElemtype;
typedef struct
{
StackElemtype base[STACKMAX];
int top;
}SqStack;
/*---------------栈的基本操作-----------*/
void InitStack(SqStack *S)
{
S->top=0;
}
void Push(SqStack *S,StackElemtype e)
{
S->base[S->top++]=e;
}
void Pop(SqStack *S)
{
if(S->top==0)
printf("The Stack is Empty\n");
S->top--;;
}
StackElemtype GetTop(SqStack *S)
{
if(S->top==0) exit(0) ;
return S->base[S->top-1];
}
int StackEmpty(SqStack *S)
{
if(S->top==0)
return 1;
else
return 0;
}
void TraverseStack(SqStack *S)
{
int i=0;
while(i<S->top)
{
printf("%c ",S->base[i]);
i++;
}
}
/*---------------树的基本操作-----------*/
BiTree CreateBiTree () //按先序遍历次序输入结点的值
{
BiTree T;
Elemtype ch;
scanf("%c",&ch);
if(ch=='/') T=NULL;
else
{
if(T=( BiTNode *)malloc(sizeof(BiTNode)))
T->data=ch;
T->lchild=CreateBiTree (); //将新结点插入左子树
T->rchild=CreateBiTree (); //将新结点插入右子树
}
return T;
}
status DepthTree(BiTree T)
{
int ld,rd;
ld=rd=0; //此地不初始化也可以
if(!T) return 0;
ld=DepthTree(T->lchild);
rd=DepthTree(T->rchild);
if(ld>rd)
return ld+1;
else
return rd+1;
}
status CountLeaf(BiTree T)
{
int lc,rc;
lc=rc=0; //初始化叶子数
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
else
{
lc=CountLeaf(T->lchild);
rc=CountLeaf(T->rchild);
return lc+rc;
}
}
}
void PathTree(BiTree T,SqStack *S)
{
if(T)
{
Push(S,T->data);
if(T->lchild==NULL&&T->rchild==NULL)
{
TraverseStack(S);
printf("\n");
}
else
{
PathTree(T->lchild,S); //输出根到所有左子树叶子结点的路径
PathTree(T->rchild,S); //输出根到所有右子树叶子结点的路径
}
Pop(S); //当前根退栈
}
}
void main()
{
BiTree T;
SqStack S;
InitStack(&S);
T=CreateBiTree();
printf("The depth of the Tree is:%d\n",DepthTree(T));
printf("The amount of the leafs is:%d\n",CountLeaf(T));
printf("根到所有叶子结点的路径依次是:\n");
PathTree(T,&S);
}
#include <malloc.h>
#include <stdlib.h>
#define STACKMAX 20
typedef char Elemtype;
typedef int status;
typedef struct node
{
Elemtype data;
struct node *lchild,*rchild;
}*BiTree,BiTNode;
typedef Elemtype StackElemtype;
typedef struct
{
StackElemtype base[STACKMAX];
int top;
}SqStack;
/*---------------栈的基本操作-----------*/
void InitStack(SqStack *S)
{
S->top=0;
}
void Push(SqStack *S,StackElemtype e)
{
S->base[S->top++]=e;
}
void Pop(SqStack *S)
{
if(S->top==0)
printf("The Stack is Empty\n");
S->top--;;
}
StackElemtype GetTop(SqStack *S)
{
if(S->top==0) exit(0) ;
return S->base[S->top-1];
}
int StackEmpty(SqStack *S)
{
if(S->top==0)
return 1;
else
return 0;
}
void TraverseStack(SqStack *S)
{
int i=0;
while(i<S->top)
{
printf("%c ",S->base[i]);
i++;
}
}
/*---------------树的基本操作-----------*/
BiTree CreateBiTree () //按先序遍历次序输入结点的值
{
BiTree T;
Elemtype ch;
scanf("%c",&ch);
if(ch=='/') T=NULL;
else
{
if(T=( BiTNode *)malloc(sizeof(BiTNode)))
T->data=ch;
T->lchild=CreateBiTree (); //将新结点插入左子树
T->rchild=CreateBiTree (); //将新结点插入右子树
}
return T;
}
status DepthTree(BiTree T)
{
int ld,rd;
ld=rd=0; //此地不初始化也可以
if(!T) return 0;
ld=DepthTree(T->lchild);
rd=DepthTree(T->rchild);
if(ld>rd)
return ld+1;
else
return rd+1;
}
status CountLeaf(BiTree T)
{
int lc,rc;
lc=rc=0; //初始化叶子数
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
else
{
lc=CountLeaf(T->lchild);
rc=CountLeaf(T->rchild);
return lc+rc;
}
}
}
void PathTree(BiTree T,SqStack *S)
{
if(T)
{
Push(S,T->data);
if(T->lchild==NULL&&T->rchild==NULL)
{
TraverseStack(S);
printf("\n");
}
else
{
PathTree(T->lchild,S); //输出根到所有左子树叶子结点的路径
PathTree(T->rchild,S); //输出根到所有右子树叶子结点的路径
}
Pop(S); //当前根退栈
}
}
void main()
{
BiTree T;
SqStack S;
InitStack(&S);
T=CreateBiTree();
printf("The depth of the Tree is:%d\n",DepthTree(T));
printf("The amount of the leafs is:%d\n",CountLeaf(T));
printf("根到所有叶子结点的路径依次是:\n");
PathTree(T,&S);
}
相关文章推荐
- 二叉树的基本操作(创建、递归和非递归遍历、求深度、求叶子数)
- 二叉树基本操作的递归实现(二叉树建立,先序,中序,后序,深度的递归遍历。广度优先,高度优先的非递归遍历)
- 二叉树的一些基本操作(括号表示法,宽度,深度,结点个数,叶子节点个数)
- 二叉树基本操作(输出所有叶子节点到根节点的路径)
- 二叉树的建立(非递归建立与定义建立)与基本操作(广度和深度遍历,求叶子树高)实现
- 二叉树基本操作集(创建、递归遍历、深度等)
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 二叉树的创建和基本操作(递归和非递归)
- 二叉树的各种操作(递归和非递归遍历,树深度,结点个数等等)(Java)
- 数据结构与算法 -- 二叉树链式详解((非)/递归遍历,叶子个数,深度计算)
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 二叉树与其基本递归操作:创建、遍历、特征量计算等
- 二叉树先序后序递归建立,前中后序层次非递归遍历,以及统计叶子结点个数以及树的深度
- 如何求一棵二叉树深度和叶子数(递归)C++版
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 二叉树的基本操作-递归实现
- C二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- [置顶] 二叉树的递归基本操作
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点