c语言利用遍历求树高的程序,《数据结构与算法(C语言版)》严蔚敏 | 第五章 建立二叉树,并完成三/四种遍历算法...

PS:所有的代码示例使用的都是这个图

6eacb98d87c50ac622e78d60791912f0.png

2019-10-29

利用p126的算法5.3建立二叉树,并完成三种遍历算法 中序 后序 先序

#include

#include

#define TElemType char

using namespace std;

typedef struct BiTNode

{

TElemType data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

//先序遍历的顺序建立二叉链表

void xCreateBiTree(BiTree &T)

{

char ch;

cin>>ch;

if(ch=='#') T=NULL;

else

{

T=new BiTNode;

T->data=ch;

xCreateBiTree(T->lchild);

xCreateBiTree(T->rchild);

}

}

//先序遍历输出

void xPrintTree(BiTree T)

{

if(T)

{

cout<data;

xPrintTree(T->lchild);

xPrintTree(T->rchild);

}

else

{

return;

}

}

//中序遍历的顺序建立二叉链表

void zCreateBiTree(BiTree &T)

{

char ch;

cin>>ch;

if(ch=='#') T=NULL;

else

{

T=new BiTNode;

zCreateBiTree(T->lchild);

T->data=ch;

zCreateBiTree(T->rchild);

}

}

//中序遍历输出

void zPrintTree(BiTree T)

{

if(T)

{

zPrintTree(T->lchild);

cout<data;

zPrintTree(T->rchild);

}

else

{

return;

}

}

//后序遍历的顺序建立二叉链表

void hCreateBiTree(BiTree &T)

{

char ch;

cin>>ch;

if(ch=='#') T=NULL;

else

{

T=new BiTNode;

hCreateBiTree(T->lchild);

hCreateBiTree(T->rchild);

T->data=ch;

}

}

//后序遍历输出

void hPrintTree(BiTree T)

{

if(T)

{

hPrintTree(T->lchild);

hPrintTree(T->rchild);

cout<data;

}

else

{

return;

}

}

int main()

{

BiTree root;

//xCreateBiTree(root);

//xPrintTree(root);

/*

输入:ABC##DE#G##F###

输出:ABCDEGF

*/

//zCreateBiTree(root);

//zPrintTree(root);

/*

输入:ABC##DE#G##F###

输出:CBEGDFA

*/

hCreateBiTree(root);

hPrintTree(root);

/*

输入:ABC##DE#G##F###

输出:CGEFDBA

*/

return 0;

}

2019-11-04

利用p126的算法5.3建立二叉树,并完成四种遍历算法 中序 先序 后序 层次

输入:ABC##DE#G##F###

#include#include#include

#define TElemType char

#define status int

#define OK 1

#define OVERFLOW 0

using namespacestd;

typedefstructBiTNode

{

TElemType data;struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;//先序遍历输出

voidxPrintTree(BiTree T)

{if(T)

{

cout<data;

xPrintTree(T->lchild);

xPrintTree(T->rchild);

}else{return;

}

}//中序遍历输出

voidzPrintTree(BiTree T)

{if(T)

{

zPrintTree(T->lchild);

cout<data;

zPrintTree(T->rchild);

}else{return;

}

}//后序遍历输出

voidhPrintTree(BiTree T)

{if(T)

{

hPrintTree(T->lchild);

hPrintTree(T->rchild);

cout<data;

}else{return;

}

}//层次遍历输出

voidLPrintTree(BiTree T)

{

queueq;

q.push(T);while(!q.empty())

{

BiTree head=q.front();

q.pop();

cout<data;if(head->lchild)

{

q.push(head->lchild);

}if(head->rchild)

{

q.push(head->rchild);

}

}

}void CreateBiTree(BiTree &T)

{

TElemType ch;

cin>>ch;if(ch=='#')

{

T=NULL;

}else{

T=newBiTNode;

T->data =ch;

CreateBiTree(T->lchild);

CreateBiTree(T->rchild);

}

}intmain()

{

BiTree root;

CreateBiTree(root);

cout<

xPrintTree(root);

cout<

zPrintTree(root);

cout<

hPrintTree(root);

cout<

LPrintTree(root);return 0;

}

设计四个算法,分别实现:

(1)统计二叉树中结点的个数。

(2)统计二叉树中叶子结点的个数

(3)统计二叉树中度为2的结点的个数

(4)统计二叉树中度为1的结点的个数

/**

#图P121(b)

#建立二叉树,并且用三种遍历法遍历他

#2019/11/4

输入:

ABC##DE#G##F###

输出:

这棵树的节点数:7

这棵树的叶子节点数:3

这棵树的度为2的结点的数:2

这棵树的度为1的结点的数:2

*/

#include

#include

#define TElemType char

#define status int

#define OK 1

#define OVERFLOW 0

using namespace std;

typedef struct BiTNode

{

TElemType data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

//统计二叉树中结点的个数

int CountTreeNode(BiTree T,int &count)

{

if(T)

{

count++;

CountTreeNode(T->lchild,count);

CountTreeNode(T->rchild,count);

}

else

{

return count;

}

}

//统计二叉树中叶子结点的个数

int CountTreeLeaf(BiTree T,int &count)

{

if(T)

{

if(!T->lchild&&!T->rchild)

count++;

CountTreeLeaf(T->lchild,count);

CountTreeLeaf(T->rchild,count);

}

else

{

return count;

}

}

//统计二叉树中度为2的结点的个数

int CountTreeDegreeTwo(BiTree T,int &count)

{

if(T)

{

if(T->lchild&&T->rchild)

count++;

CountTreeDegreeTwo(T->lchild,count);

CountTreeDegreeTwo(T->rchild,count);

}

else

{

return count;

}

}

//统计二叉树中度为1的结点的个数

int CountTreeDegreeOne(BiTree T,int &count)

{

if(T)

{

if((T->lchild&&!T->rchild)||(!T->lchild&&T->rchild))

count++;

CountTreeDegreeOne(T->lchild,count);

CountTreeDegreeOne(T->rchild,count);

}

else

{

return count;

}

}

void CreateBiTree(BiTree &T)

{

TElemType ch;

cin>>ch;

if(ch=='#')

{

T=NULL;

}

else

{

T=new BiTNode;

T->data = ch;

CreateBiTree(T->lchild);

CreateBiTree(T->rchild);

}

}

int main()

{

BiTree root;

CreateBiTree(root);

int count=0;

cout<

count=0;

cout<

count=0;

cout<

count=0;

cout<

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值