二叉树叶子结点个数计算.docx

上传人:b****5 文档编号:3116613 上传时间:2022-11-17 格式:DOCX 页数:7 大小:28.06KB
下载 相关 举报
二叉树叶子结点个数计算.docx_第1页
第1页 / 共7页
二叉树叶子结点个数计算.docx_第2页
第2页 / 共7页
二叉树叶子结点个数计算.docx_第3页
第3页 / 共7页
二叉树叶子结点个数计算.docx_第4页
第4页 / 共7页
二叉树叶子结点个数计算.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

二叉树叶子结点个数计算.docx

《二叉树叶子结点个数计算.docx》由会员分享,可在线阅读,更多相关《二叉树叶子结点个数计算.docx(7页珍藏版)》请在冰豆网上搜索。

二叉树叶子结点个数计算.docx

二叉树叶子结点个数计算

计算二叉树叶子结点

 

1.程序设计简介

已知一棵二叉树,求该二叉树中叶子结点的个数。

2.基本要求

(1)设计二叉树的二叉链表为存储结构

(2)设计求叶子结点个数的递归算法

(3)输入:

一颗二叉树

(4)输出:

二叉树中叶子结点的个数

3.实现提示

(1)存储设计

二叉树采用二叉链表为存储结构

(2)算法设计

求二叉树中叶子结点个数,即求二叉树的所有结点中左、右子树均为空的结点个数之和。

可以将此问题转化为遍历问题,在遍历中“访问一个结点”时判断该结点是不是叶子,若是则将计数器累加。

4.源程序

#include

#include

usingnamespacestd;

structBiNode//二叉树的结点结构

{

chardata;

BiNode*lchild,*rchild;

};

classBiTree

{

public:

BiTree();//构造函数,初始化一棵二叉树,其前序序列由键盘输入

~BiTree(void);//析构函数,释放二叉链表中各结点的存储空间

BiNode*Getroot();//获得指向根结点的指针

voidPreOrder(BiNode*root);//前序遍历二叉树

voidBiTree:

:

yezi(BiNode*root,int&n);

private:

BiNode*root;//指向根结点的头指针

BiNode*Creat();//有参构造函数调用

voidRelease(BiNode*root);//析构函数调用

};

BiTree:

:

BiTree()

{

root=Creat();

}

BiTree:

:

~BiTree(void)

{

Release(root);

}

BiNode*BiTree:

:

Getroot()

{

returnroot;

}

voidBiTree:

:

PreOrder(BiNode*root)

{

if(root==NULL)return;

else{

cout<data<<"";

PreOrder(root->lchild);

PreOrder(root->rchild);

}

}

voidBiTree:

:

yezi(BiNode*root,int&n)

{

if(root)

{

if(root->lchild==NULL&&root->rchild==NULL)

n++;

yezi(root->lchild,n);

yezi(root->rchild,n);

}

}

BiNode*BiTree:

:

Creat()

{

BiNode*root;

charch;

cin>>ch;

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

else{

root=newBiNode;//生成一个结点

root->data=ch;

root->lchild=Creat();//递归建立左子树

root->rchild=Creat();//递归建立右子树

}

returnroot;

}

voidBiTree:

:

Release(BiNode*root)

{

if(root!

=NULL){

Release(root->lchild);//释放左子树

Release(root->rchild);//释放右子树

deleteroot;

}

}

voidmain()

{cout<<"请输入二叉树的结点数据:

";

BiTreebt;//创建一棵树

BiNode*root=bt.Getroot();//获取指向根结点的指针

intn=0;

cout<<"------前序遍历------"<

bt.PreOrder(root);

bt.yezi(root,n);

cout<

cout<<"叶子节点数:

"<

cout<

}

5.运行与测试

 

6.调试感想

非递归算法求叶子结点的个数

#include

#include

usingnamespacestd;

structnode

{

intdata;

node*lchild;

node*rchild;

};

node*root=NULL;

voidmid(node*root,intkey=500)

{

intsum=0;

stacks;

while(NULL!

=root||!

s.empty())

{

if(NULL!

=root)

{

s.push(root);

root=root->lchild;

}

else

{

root=s.top();

//cout<data<<"";

if(NULL==root->lchild&&NULL==root->rchild)

++sum;

s.pop();

root=root->rchild;

}

}

cout<

}

intmain()

{

root=newnode;

root->data=100;

node*a=newnode;

node*b=newnode;

node*a1=newnode;

node*a2=newnode;

node*b1=newnode;

node*b2=newnode;

a->data=200;

b->data=300;

a1->data=400;

a2->data=500;

b1->data=600;

b2->data=700;

root->lchild=a;

root->rchild=b;

a->lchild=a1;

a->rchild=a2;

b->lchild=b1;

b->rchild=b2;

a1->lchild=NULL;

a1->rchild=NULL;

a2->lchild=NULL;

a2->rchild=NULL;

b1->lchild=NULL;

b1->rchild=NULL;

b2->lchild=NULL;

b2->rchild=NULL;

mid(root);

return0;

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 判决书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1