求二叉树的深度叶子结点数总结点数()
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
printf("\n\t\t\t请输入按先序建立二叉树的结点序列: ");
printf("\n\t\t\t说明:逐个输入,输入空格代表后续结点为空,按回车输入下一个结点.");
printf("\n\t\t\t请输入根结点: ");
bt=crt_bt_pre();
printf("\n\t\t\t二叉树成功建立!\n");
}
else if(choice=='2')
{
printf("\n\t\t\t该二叉树的先序遍历序列为: ");
Preorder(bt);
}
else if(choice=='3')
{
printf("\n\t\t\t该二叉树的中序遍历序列为: ");
Inorder(bt);
}
else if(choice=='4')
printf("\n\t\t\t* 0-------退出*");
printf("\n\t\t\t************************************");
printf("\t\t\t请选择菜单号(0--7):");
scanf("%c",&choice);getchar();
if(choice=='1')
bt->lchild=crt_bt_pre();
printf("\n\t\t\t请输入%c结点的右孩子:",bt->data);
bt->rchild=crt_bt_pre();
}
return(bt);
}
void Preorder(NODE* bt)/*二叉树先序递归遍历算法*/
{
if(bt!=NULL)
{
j=0;
printf("\t\t\t程序结束!\n");
}
}
}
街拍http://www.jiepaia.comQ438K3bt53Kv
return 0;
else
x=TreeDepth(bt->lchild);
y=TreeDepth(bt->rchild);
if(x>y)
return(x+1);
else
return(y+1);
}
void main()
{
NODE *bt;
char choice;
int j=1;
int x;
while(j)
{
printf("\n\t\t\t %c",bt->data);
Preorder(bt->lchild);
Preorder(bt->rchild);
}
}
void Inorder(NODE* bt)
{
if(bt!=NULL)
{
Inorder(bt->lchild);
printf("\n\t\t\t %c",bt->data);
{
NODE * bt;
char ch;
printf("\n\t\t\t");
scanf("%c",&ch);
getchar();
if(ch==' ') bt=NULL;
else
{
bt=(NODE*)malloc(sizeof(NODE));
bt->data=ch;
printf("\n\t\t\t请输入%c结点的左孩子:",bt->data);
{
if(bt==NULL)
return 0;
else
count++;
CountNode(bt->lchild);
CountNode(bt->rchild);
return(count);
}
int TreeDepth(NODE* bt)/*求二叉树深度的递归遍历算法*/
{
int x,y;
if(bt==NULL)
Inorder(bt->rchild);
}
}
void Postorder(NODE* bt)
{
if(bt!=NULL)
{
Postorder(bt->lchild);
Postorder(bt->rchild);
printf("\n\t\t\t %c",bt->data);
}
}
int CountLeaf(NODE *bt)/*求二叉树叶子结点数的递归遍历算法*/
#include"malloc.h"
#define NULL 0
#include"stdio.h"
typedef struct node
{
char data;
struct node *lchild,*rchild;
}NODE;
int count;
NODE *crt_bt_pre()/*二叉树先序创建算法*/
{
printf("\n\t\t\t该二叉树的后序遍历序列为: ");
Postorder(bt);
}Leabharlann Baidu
else if(choice=='5')
{
count=0;
CountLeaf(bt);
printf("\n\t\t\t该二叉树有%d个叶子结点。\n",count);
}
else if(choice=='6')
{
if(bt==NULL)
return 0;
if(bt->lchild==NULL&&bt->rchild==NULL)
count++;
CountLeaf(bt->lchild);
CountLeaf(bt->rchild);
return(count);
}
int CountNode (NODE* bt)/*求二叉树结点数的递归遍历算法*/
{
count=0;
x=CountNode(bt);
printf("\n\t\t\t该二叉树共有%d个叶子结点。\n",count);
}
else if(choice=='7')
{
x=TreeDepth(bt);
printf("\n\t\t\t该二叉树的深度为%d",x);
}
else if(choice=='0')
{
printf("\n\n\n");
printf("\t\t\t-二叉树的基本运算--\n");
printf("\n\t\t\t************************************");
printf("\n\t\t\t* 1-------建二差树*");
printf("\n\t\t\t* 2-------先序遍历*");
printf("\n\t\t\t* 3-------中序遍历*");
printf("\n\t\t\t* 4-------后序遍历*");
printf("\n\t\t\t* 5-------统计叶子数*");
printf("\n\t\t\t* 6-------统计结点数*");
printf("\n\t\t\t* 7-------求二叉树深度*");
printf("\n\t\t\t请输入按先序建立二叉树的结点序列: ");
printf("\n\t\t\t说明:逐个输入,输入空格代表后续结点为空,按回车输入下一个结点.");
printf("\n\t\t\t请输入根结点: ");
bt=crt_bt_pre();
printf("\n\t\t\t二叉树成功建立!\n");
}
else if(choice=='2')
{
printf("\n\t\t\t该二叉树的先序遍历序列为: ");
Preorder(bt);
}
else if(choice=='3')
{
printf("\n\t\t\t该二叉树的中序遍历序列为: ");
Inorder(bt);
}
else if(choice=='4')
printf("\n\t\t\t* 0-------退出*");
printf("\n\t\t\t************************************");
printf("\t\t\t请选择菜单号(0--7):");
scanf("%c",&choice);getchar();
if(choice=='1')
bt->lchild=crt_bt_pre();
printf("\n\t\t\t请输入%c结点的右孩子:",bt->data);
bt->rchild=crt_bt_pre();
}
return(bt);
}
void Preorder(NODE* bt)/*二叉树先序递归遍历算法*/
{
if(bt!=NULL)
{
j=0;
printf("\t\t\t程序结束!\n");
}
}
}
街拍http://www.jiepaia.comQ438K3bt53Kv
return 0;
else
x=TreeDepth(bt->lchild);
y=TreeDepth(bt->rchild);
if(x>y)
return(x+1);
else
return(y+1);
}
void main()
{
NODE *bt;
char choice;
int j=1;
int x;
while(j)
{
printf("\n\t\t\t %c",bt->data);
Preorder(bt->lchild);
Preorder(bt->rchild);
}
}
void Inorder(NODE* bt)
{
if(bt!=NULL)
{
Inorder(bt->lchild);
printf("\n\t\t\t %c",bt->data);
{
NODE * bt;
char ch;
printf("\n\t\t\t");
scanf("%c",&ch);
getchar();
if(ch==' ') bt=NULL;
else
{
bt=(NODE*)malloc(sizeof(NODE));
bt->data=ch;
printf("\n\t\t\t请输入%c结点的左孩子:",bt->data);
{
if(bt==NULL)
return 0;
else
count++;
CountNode(bt->lchild);
CountNode(bt->rchild);
return(count);
}
int TreeDepth(NODE* bt)/*求二叉树深度的递归遍历算法*/
{
int x,y;
if(bt==NULL)
Inorder(bt->rchild);
}
}
void Postorder(NODE* bt)
{
if(bt!=NULL)
{
Postorder(bt->lchild);
Postorder(bt->rchild);
printf("\n\t\t\t %c",bt->data);
}
}
int CountLeaf(NODE *bt)/*求二叉树叶子结点数的递归遍历算法*/
#include"malloc.h"
#define NULL 0
#include"stdio.h"
typedef struct node
{
char data;
struct node *lchild,*rchild;
}NODE;
int count;
NODE *crt_bt_pre()/*二叉树先序创建算法*/
{
printf("\n\t\t\t该二叉树的后序遍历序列为: ");
Postorder(bt);
}Leabharlann Baidu
else if(choice=='5')
{
count=0;
CountLeaf(bt);
printf("\n\t\t\t该二叉树有%d个叶子结点。\n",count);
}
else if(choice=='6')
{
if(bt==NULL)
return 0;
if(bt->lchild==NULL&&bt->rchild==NULL)
count++;
CountLeaf(bt->lchild);
CountLeaf(bt->rchild);
return(count);
}
int CountNode (NODE* bt)/*求二叉树结点数的递归遍历算法*/
{
count=0;
x=CountNode(bt);
printf("\n\t\t\t该二叉树共有%d个叶子结点。\n",count);
}
else if(choice=='7')
{
x=TreeDepth(bt);
printf("\n\t\t\t该二叉树的深度为%d",x);
}
else if(choice=='0')
{
printf("\n\n\n");
printf("\t\t\t-二叉树的基本运算--\n");
printf("\n\t\t\t************************************");
printf("\n\t\t\t* 1-------建二差树*");
printf("\n\t\t\t* 2-------先序遍历*");
printf("\n\t\t\t* 3-------中序遍历*");
printf("\n\t\t\t* 4-------后序遍历*");
printf("\n\t\t\t* 5-------统计叶子数*");
printf("\n\t\t\t* 6-------统计结点数*");
printf("\n\t\t\t* 7-------求二叉树深度*");