求二叉树的深度叶子结点数总结点数(免费)

时间:2024.4.27

#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()/*二叉树先序创建算法*/

{

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); 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)

{

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);

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)/*求二叉树叶子结点数的递归遍历算法*/ {

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)/*求二叉树结点数的递归遍历算法*/ {

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)

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\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* 0-------退 出 *");

printf("\n\t\t\t************************************");

printf("\t\t\t请选择菜单号(0--7):");

scanf("%c",&choice);getchar();

if(choice=='1')

{

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该二叉树的后序遍历序列为: "); Postorder(bt); } else if(choice=='5') { count=0; CountLeaf(bt); printf("\n\t\t\t该二叉树有%d个叶子结点。\n",count); } else if(choice=='6') { 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') { j=0; printf("\t\t\t程序结束!\n"); }

}

求二叉树的深度叶子结点数总结点数免费

求二叉树的深度叶子结点数总结点数免费

}


第二篇:求二叉树的深度


#include<stdio.h>#include<malloc.h>#define NULL 0typedef struct ECshu{struct ECshu *lchild;struct ECshu *rchild;char data;}*linkecshu;linkecshu Creatshu(linkecshu &root){char s1;scanf("%c",&s1);if (s1==' ') root=NULL;else{if((root=(linkecshu)malloc(sizeof(struct ECshu)))) {root->data=s1;Creatshu(root->lchild);Creatshu(root->rchild);}}return root;}void Printecshu(linkecshu &root)//这里肯定没有错误。{if(root){Printecshu(root->lchild);printf("%c",root->data);Printecshu(root->rchild);}}int Depth(linkecshu root)// 返回二叉树的深度{ int depthval,depthLeft,depthRight;if ( !root) depthval = 0;else {depthLeft = Depth( root->lchild );depthRight= Depth( root->rchild );depthval =1+(depthLeft > depthRight ?depthLeft : depthRight);} return depthval;}void main(){linkecshu r,h;int sd;printf("请输入您要建立的二叉树的各项数据元素:\n");h=Creatshu(r);printf("您输入的二叉树为:\n");Printecshu(h);printf("\n");sd=Depth(r);printf("二叉树深度为%d",sd);getchar();getchar();}

更多相关推荐:
二叉树总结

二叉树与二叉搜索树指针与引用1intcount18intptrampcount2intcount18intamppcountcount创建二叉树与创建二叉搜索树前者只是为了熟悉课本知识后者具有实际应用的功能比较...

二叉树的性质总结

一二叉树的性质性质1二叉树的第i层上至多有2i1i1个结点用数学归纳法证明推广k叉树或度为k的树的第i层上至多有ki1i1个结点性质2度为h的二叉树中至多含有2h1个结点2112212h12h1推广深度为h的k...

二叉树的性质总结

一二叉树的性质性质1二叉树的第i层上至多有2i1i1个结点用数学归纳法证明推广k叉树或度为k的树的第i层上至多有ki1i1个结点性质2度为h的二叉树中至多含有2h1个结点2112212h12h1推广深度为h的k...

(数据结构课程设计)二叉树

甘肃政法学院数据结构课程设计题目二叉树遍历计算机科学学院信息管理与信息系统专业09级信息管理与信息系统班学号20xx81020xx6姓名唐占红指导教师金涛成绩完成时间20xx年12月1摘要二叉树是树形结构的一个...

数据结构二叉树遍历有关算法

二叉树结点的数据结构typedefstructBiTNode树节点定义intvalueBiTNodelchildrchildBiTNodeBiTree二叉树的遍历voidPreorderBiTreeroot先序...

数据结构二叉树实验报告

实验三二叉树的遍历一实验目的熟悉二叉树的结点类型和二叉树的基本操作掌握二叉树的前序中序和后序遍历的算法3加深对二叉树的理解逐步培养解决实际问题的编程能力二实验环境运行或VC的微机三实验内容1依次输入元素值以链表...

二叉树的基本操作完整版,包含二叉树的所有操作,凡是你想要的都在里面--数据结构版

includequotstdiohquotincludequotstdlibhquotincludequotstringhquotincludequotmathhquottypedefcharTElemType...

算法大全-面试题-链表-栈-二叉树-数据结构

一单链表目录1单链表反转2找出单链表的倒数第4个元素3找出单链表的中间元素4删除无头单链表的一个节点5两个不交叉的有序链表的合并6有个二级单链表其中每个元素都含有一个指向一个单链表的指针写程序把这个二级链表称一...

二叉树实验报告

实验报告实验题目二叉树需求分析程序的功能从键盘接受输入先序以二叉链表作为存储结构建立二叉树以先序来建立并采用递归算法对其进行遍历先序中序后序将遍历结果打印输出输入的形式ABCDEGF其中表示空格字符输出的形式先...

二叉树操作实验报告

实验报告姓名班级12南航网络学号

实验12 二叉树遍历实验报告

实验12二叉树遍历题目实现链式存储的二叉树的多种遍历算法包括递归非递归以及线索二叉树等班级信息学院20xx级理科实验班1班姓名学号完成日期20xx519一需求分析1演示程序分别用多种遍历算法遍历二叉树并把数据输...

数据结构二叉树操作实验报告

实验报告指导教师XX实验时间20xx年11月1日学院计算机学院专业信息安全班级XXXXXX学号XXXXX姓名XX实验室S331实验题目二叉树操作实验要求采用二叉树链表作为存储结构完成二叉树的建立先序中序和后序以...

二叉树总结(10篇)