数据结构算法设计——统计二叉树叶子结点的个数,并输出结果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构算法设计——统计二叉树叶子结点的个数,并输出结果相关的知识,希望对你有一定的参考价值。

C语言

代码如下:

#include<stdio.h>

#include<stdlib.h>

typedef struct BiTNode

char data;

struct BiTNode *lchild,*rchild;

BiTNode,*BiTree;

void CreatTree(BiTree &A)

char ch;

scanf("%c",&ch);

if(ch=='#')

A=NULL;

else

A=new BiTNode;

A->data=ch;

CreatTree(A->lchild);

CreatTree(A->rchild);

int NodeTree(BiTree A)

if(A==NULL)

return 0;

else if(A->lchild==NULL&&A->rchild==NULL)

return 1;

else

return NodeTree(A->lchild)+NodeTree(A->rchild);

int main()

BiTree  A;

int b;

printf("先序法赋值(空用#表示):");

CreatTree(A);

b=NodeTree(A);

printf("共有%d个叶子节点\\n",b);

扩展资料

二叉树的性质

1、对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2,则N0=N2+1;

2、有N个结点的完全二叉树各结点如果用顺序方式存储,则结点之间有如下关系:

若I为结点编号则 如果I>1,则其父结点的编号为I/2;

如果2*I<=N,则其左孩子(即左子树的根结点)的编号为2*I;若2*I>N,则无左孩子;

如果2*I+1<=N,则其右孩子的结点编号为2*I+1;若2*I+1>N,则无右孩子。

3、给定N个结点,能构成h(N)种不同的二叉树。h(N)为卡特兰数的第N项。h(n)=C(2*n,n)/(n+1)。

4、设有i个枝点,I为所有枝点的道路长度总和,J为叶的道路长度总和J=I+2i[2]

参考技术A 同学,你们老师和我们老师留的作业是一模一样的阿,我有现成的做好了的程序,调试成功。这个程序的难点就在于这种很别扭的输入形式,所以我为它设计了一个结构体形式存放输入内容,再将它转化成了线性结构。

#include <iostream.h>
#include <stdlib.h>

struct inform /*建立输入信息结构体inform*/
char data;

int l;

int r;

int signl; /*作为标记的signl,signr*/

int signr;
;

struct leafnode /*建立叶子节点结构体*/

char leaf;

leafnode* lchild;

leafnode* rchild;

;

void print(inform* ps, int n);

void judge ( inform* ps );

leafnode* creatree(); /*声明二叉树的建立函数*/

void preorder (leafnode* T); /*声明先序遍历函数*/

void inorder (leafnode* T); /*声明中序遍历函数*/

void postorder (leafnode* T); /*声明后序遍历函数*/

char a[100];

int k=1;
int s=0;

inform *p;

void main()

/*-------------------------------按格式输入信息-----------------------------------*/

int n;

cout<<"请输入二叉树内容:第一行为节点总数n ,后面的n行是节点的具体形式:"<<endl;

cout<<"n= ";

cin>>n;

p=(inform* )malloc( n*sizeof(inform) ); /*开辟的一个叶子结构体型的指针数组*/
inform *p1; p1=p;

for(int i=0; i<n; i++)


cin>>(p+i)->data>>(p+i)->l>>(p+i)->r;
if((p+i)->l != -1) (p+i)->signl=1; /*用signl signr 的0,1标示输入的信息中是否有左或右孩子*/
else (p+i)->signl= 0;
if((p+i)->r !=-1) (p+i)->signr=1;
else (p+i)->signr= 0;


/*--------------------------------------------------------------------------------------------*/
a[0]= p->data;

judge ( p1 ); /*用递归算法将输入数据信息转为线性字符串*/

cout<<endl<<"输出转换的线性字符串: "<<endl;

cout<<a<<endl<<endl;
/*------------------------------------------遍历-----------------------------------*/
leafnode* T;

T= creatree();

/*先续遍历二叉树*/
cout<<"先序遍历二叉树: "<<endl;
preorder( T );

cout<<endl<<"中序遍历二叉树: "<<endl;
inorder ( T );

cout<<endl<<"后序遍历二叉树: "<<endl;
postorder( T );
cout<<endl<<endl;



/*------------------------------------------函数定义-------------------------------*/

void judge( inform* ps ) /*用函数的递归来将输入的信息转化为线性的数组*/

inform* b;

if (ps->signl==0)

a[k]='@';
k++;

else

b = p+(ps->l);
a[k] = b->data;
k++;
judge(b);


if ((ps->signr) == 0)

a[k]='@';
k++;

else

b = p+(ps->r );
a[k] = b->data;
k++;
judge(b);




leafnode* creatree() /*建立二叉树函数*/

char ch;

leafnode *t;

ch= a[s];
s++;

if(ch=='@')

t=NULL;

else

t=(leafnode* )malloc(sizeof(leafnode));
t->leaf=ch;
t->lchild=creatree();
t->rchild=creatree();

return t;



/*先序遍历的递归函数*/
void preorder (leafnode* T)

if(T)

cout<<T->leaf;
preorder(T->lchild);
preorder(T->rchild);


/*中序遍历的递归函数*/
void inorder (leafnode* T)

if(T)

inorder(T->lchild);
cout<<T->leaf;
inorder(T->rchild);


/*后序遍历的递归函数*/
void postorder (leafnode* T)

if(T)

postorder(T->lchild);
postorder(T->rchild);
cout<<T->leaf;

本回答被提问者采纳
参考技术B 完全二叉树有1000个结点,度为1的节点个数可能是0或1,若为0,则该题无解,所以显然不能为0了,若为1,则度为2的结点个数为499个,度为1的节点数为1,度为0的节点为500 参考技术C 楼主哥哥,看下面,你把这个函数看成一个f(x)
当x=NULL f(x)=0;
当x左右子树为空 f(x)=1;
其他 f(x)=f(bt->lchild)+f(bt-rchild)
-----------------------------------------------------------------------
int Count(BTreeNode *BT)

int l,r;
if(BT==NULL) return 0;
else if(BT->Lchild==NULL&&BT->Rchild==NULL) return 1;
else

l=Count(BT->Lchild);
r=Count(BT->Rchild);
return (l+r);

C++程序设计创建并统计二叉树的结点个数

要求代码简单易懂!

参考技术A

头文件:binary_tree.h

#ifndef BINARYTREE
#define BINARYTREE

#include <stack>
#include <iostream> //cin>> cout<<
#include <queue>
#include <utility>
#include <iomanip> //setw()
#define MaxSize 100
#define MaxWidth 50

////用打括号的字符串来作为创建树时的输入
////例如 
//binary_tree::pbinary_tree_node T,Tnew;
//char pch[100]="A(B(F,G(H,I)),C(D(J,K),E))";
//Tnew=NULL; binary_tree::create_binary_tree(T,pch);
//binary_tree::print_binary_tree(T);
//binary_tree::copy_binary_tree(T,Tnew);
//binary_tree::print_binary_tree(Tnew);
//binary_tree::pbinary_tree_node Tnew2=binary_tree::copy_binary_tree(Tnew);
//binary_tree::print_binary_tree(Tnew2);
//binary_tree::pre_order(T);
//binary_tree::in_order(T);
//binary_tree::post_order(T);
//cout<<"leafs:";
//binary_tree::print_leaf(T);
//cout<<"number of leaf:"<<binary_tree::count_leaf(T)<<endl;
//cout<<endl;

using namespace std;
namespace binary_tree


typedef struct tnode

tnode():data('\\0'),lchild(NULL),rchild(NULL)
char data;
struct tnode *lchild,*rchild;
binary_tree_node ,*pbinary_tree_node;


void create_binary_tree(pbinary_tree_node &bt,char *str)


pbinary_tree_node St[MaxSize],p;
int top=-1,k,j=0;
char ch;
bt=NULL;
while((ch=str[j++])!='\\0')

switch(ch)

case '(':top++;St[top]=p;k=1;break;
case ')':top--;break;
case ',':k=2;break;
default:
p=new binary_tree_node;
p->data=ch;
p->lchild=p->rchild=NULL;
if(bt==NULL)
bt=p;
else

switch(k)

case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;






void create_binary_search_tree(pbinary_tree_node &bt,const vector<char>& vc)

for (vector<char>::const_iterator i=vc.begin();i!=vc.end();i++)




void print_binary_tree(pbinary_tree_node bt)
//用左右孩子的方式输出一颗树
stack<pair<pbinary_tree_node,int>> Stack;
if (bt)

Stack.push(make_pair(bt,0));

pair<pbinary_tree_node,int> onenode;
while (!Stack.empty())

onenode=Stack.top();
Stack.pop();

if (onenode.second==0)

cout<<onenode.first->data<<endl;

else if (onenode.second==1)

cout<<" --"<<onenode.first->data<<endl;

else

int depth=onenode.second;
while(--depth)

cout<<"   ";

cout<<" --"<<onenode.first->data<<endl;


if (onenode.first->lchild!=0)

Stack.push(make_pair(onenode.first->rchild,onenode.second+1));

if (onenode.first->lchild!=0)

Stack.push(make_pair(onenode.first->lchild,onenode.second+1));


cout<<endl;


void pre_order(pbinary_tree_node T)
//先序遍历输出一颗树的全部结点值1,2,3
stack<pbinary_tree_node> Stack;
if (T)

Stack.push(T);
pbinary_tree_node t;
while (!Stack.empty())

t=Stack.top();
Stack.pop();
cout<<t->data<<" ";
if (t->rchild!=0)

Stack.push(t->rchild);

if (t->lchild!=0)

Stack.push(t->lchild);


cout<<endl;




void in_order(pbinary_tree_node t)
//中序序遍历输出一颗树的全部结点值2,1,3
//广度优先遍历
stack<pbinary_tree_node> tempstack;
if (t!=NULL)

do 

tempstack.push(t);
t=t->lchild;
 while (t!=NULL);

while (!tempstack.empty())

pbinary_tree_node p=tempstack.top();
cout<<p->data<<" ";
tempstack.pop();
if (p->rchild!=NULL)

p=p->rchild;
do 

tempstack.push(p);
p=p->lchild;
 while (p!=NULL);


cout<<endl;


void post_order(pbinary_tree_node T)
//后续序序遍历输出一颗树的全部结点值2,3,1
//广度优先遍历
typedef pair<pbinary_tree_node,bool> multinode; 
stack<multinode> tempstack;
if (T)

tempstack.push(make_pair(T,false));

while (!tempstack.empty())

multinode m=tempstack.top();tempstack.pop();
if (m.first->lchild==NULL && m.first->rchild==NULL)
//叶子节点直接输出
cout<<m.first->data<<" ";

else if (m.second==true)
//所有孩子都遍历完了才会到这一步
cout<<m.first->data<<" ";

else 
//非终结点,并且孩子还没遍历完。
m.second=true;tempstack.push(m);
if (m.first->rchild!=NULL)

tempstack.push(make_pair(m.first->rchild,false));

if (m.first->lchild!=NULL)

tempstack.push(make_pair(m.first->lchild,false));



cout<<endl;

int binary_tree_hight(pbinary_tree_node bt)
//递归实现计算深度最大的路径长度
int lchilddep,rchilddep;
if(bt==NULL)
return 0;
else

lchilddep=binary_tree_hight(bt->lchild);
rchilddep=binary_tree_hight(bt->rchild);
return (lchilddep>rchilddep)?(lchilddep+1):(rchilddep+1);



int count_node(pbinary_tree_node bt)
//计算一颗树全部的结点个数
int num1,num2;
if(bt==NULL)
return 0;
else

num1=count_node(bt->lchild);
num2=count_node(bt->rchild);
return (num1+num2+1);



int count_leaf(pbinary_tree_node bt)
//返回一颗树上全部的叶节点个数
int num1,num2;
if(bt==NULL)
return 0;
else if(bt->lchild==NULL && bt->rchild==NULL)
return 1;
else

num1=count_leaf(bt->lchild);
num2=count_leaf(bt->rchild);
return (num1+num2);


void print_leaf(pbinary_tree_node bt)


if (bt==NULL)

return ;

//root first 
else if (bt->lchild==NULL && bt->rchild==NULL)

cout<<" "<<bt->data<<" ";
 
else

print_leaf(bt->lchild);
print_leaf(bt->rchild);




void level_order(pbinary_tree_node T)
//广度优先遍历一棵树的所有节点,借助一个队列,简单实现。
queue<pbinary_tree_node> Queue;
if (T)

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

pbinary_tree_node t=Queue.front();
Queue.pop();
cout<<t->data<<" ";
if (t->lchild!=0)

Queue.push(t->lchild);

if (t->rchild!=0)

Queue.push(t->rchild);


cout<<endl;



pbinary_tree_node find_node(pbinary_tree_node bt,char x)
//查找一颗树中是否有值为x的结点
pbinary_tree_node p;
if(bt==NULL)
return NULL;
else if(bt->data==x)
return bt;
 else
 
 p=find_node(bt->lchild,x);
 if(p!=NULL)
 return p;
 else
 return find_node(bt->rchild,x);
 


void max_node(pbinary_tree_node bt,pbinary_tree_node &p)

if(bt!=NULL)

if(bt->data>p->data)
p=bt;
max_node(bt->lchild,p);
max_node(bt->rchild,p);



void copy_binary_tree(pbinary_tree_node bt,pbinary_tree_node &newbt)

if(bt!=NULL)

newbt=(pbinary_tree_node )malloc(sizeof(binary_tree_node));
newbt->data=bt->data;
copy_binary_tree(bt->lchild,newbt->lchild);
copy_binary_tree(bt->rchild,newbt->rchild);

else
newbt=NULL;

//pbinary_tree_node copy_binary_tree(pbinary_tree_node root) 
//
// pbinary_tree_node new_root;
// if(root!=NULL)
// new_root=(pbinary_tree_node )malloc(sizeof(binary_tree_node));
// new_root->data=root->data;
// new_root->lchild=copy_binary_tree(root->lchild);
// new_root->rchild=copy_binary_tree(root->rchild);
//  
// else 
// return NULL;
// return new_root;
//
pbinary_tree_node copy_binary_tree(pbinary_tree_node bt)
//先序遍历输出一颗树的全部结点值1,2,3
stack<pbinary_tree_node> stack_left,stack_right;
pbinary_tree_node newbt;
if (bt!=NULL)

//new root
newbt=new binary_tree_node;
newbt->data=bt->data;

//travel bt and travel newbt at the same time
stack_left.push(bt);
stack_right.push(newbt);

while (!stack_left.empty())

pbinary_tree_node pleft=stack_left.top();
pbinary_tree_node pright=stack_right.top();
stack_left.pop();
stack_right.pop();
if (pleft->rchild!=0)

stack_left.push(pleft->rchild);
pright->rchild=new binary_tree_node;
pright->rchild->data=pleft->rchild->data;
stack_right.push(pright->rchild);

if (pleft->lchild!=0)

stack_left.push(pleft->lchild);
pright->lchild=new binary_tree_node;
pright->lchild->data=pleft->lchild->data;
stack_right.push(pright->lchild);



return newbt;


void delete_binary_tree(pbinary_tree_node &bt)

if(bt!=NULL)

delete_binary_tree(bt->lchild);
delete_binary_tree(bt->rchild);
delete(bt);bt=NULL;



int  size(pbinary_tree_node T)
//树的元素个数
if(T==NULL) return 0;
else return size(T->lchild)+size(T->rchild)+1;



int level(pbinary_tree_node T,pbinary_tree_node p)
//返回p所指结点的高度(高度自下往上计算)
if(T==NULL) return 0;
if(T->data==p->data ) return 1;
int h1=level(T->lchild,p );
if(h1>0) return h1+1;
int h2=level(T->rchild,p );
if(h2>0) return h2+1;
return 1;


int reflect(pbinary_tree_node T)
//交换数的左右子树
if(T==NULL) return 0;
reflect(T->lchild );
reflect(T->rchild );
pbinary_tree_node p;
p=T->rchild ;
T->rchild=T->lchild;
T->lchild=p;
return 1;

int reflect1(pbinary_tree_node T)

queue<pbinary_tree_node> queue1;
if (T!=NULL)

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

pbinary_tree_node temp=queue1.front();
queue1.pop();
swap(temp->lchild,temp->rchild);
if (temp->lchild!=NULL)

queue1.push(temp->lchild);

if (temp->rchild!=NULL)

queue1.push(temp->rchild);


return 1;

else

return 0;



int max_path(pbinary_tree_node &T)
//输出一条最长的根路径
if(T==NULL) return 0;
if((T->lchild==NULL)&&(T->rchild==NULL))
delete T;
T=NULL;
return 1;

else 
max_path(T->lchild );
max_path(T->rchild );
return 1;



void level_number(pbinary_tree_node T,int a[],int level)
//计算树每层的结点数
if(T!=NULL)
a[level]+=1;
level_number(T->lchild,a,level+1);
level_number(T->rchild,a,level+1);



int width(pbinary_tree_node T)
//求树的宽度
int wid,i=0;
int a[11];//树的最大层次11
for(i=0;i<11;i++) a[i]=0;
level_number(T,a,0);
for(i=0;i<11;i++) cout<<setw(3)<<a[i];
cout<<endl;
wid=a[0];
for(i=1;i<11;i++) 
if(wid<a[i]) wid=a[i];
return wid;


int height(pbinary_tree_node t)

if(t==NULL) return 0;
int hl=height(t->lchild );
int hr=height(t->rchild );
if(hl>hr) return 1+hl;
else return 1+hr;




#endif

源文件main.cpp

#include <iostream>
using namespace std;
#include "binary_tree.h"

int main()

//用打括号的字符串来作为创建树时的输入
//例如 
binary_tree::pbinary_tree_node T,Tnew;
char pch[100]="A(B(F,G(H,I)),C(D(J,K),E))";
Tnew=NULL; binary_tree::create_binary_tree(T,pch);
binary_tree::print_binary_tree(T);
binary_tree::copy_binary_tree(T,Tnew);
binary_tree::print_binary_tree(Tnew);
binary_tree::pbinary_tree_node Tnew2=binary_tree::copy_binary_tree(Tnew);
binary_tree::print_binary_tree(Tnew2);
binary_tree::pre_order(T);
binary_tree::in_order(T);
binary_tree::post_order(T);
cout<<"leafs:";
binary_tree::print_leaf(T);
cout<<"number of leaf:"<<binary_tree::count_leaf(T)<<endl;
cout<<endl;

输出:

A

 --B

    --F

    --G

       --H

       --I

 --C

    --D

       --J

       --K

    --E


A

 --B

    --F

    --G

       --H

       --I

 --C

    --D

       --J

       --K

    --E


A

 --B

    --F

    --G

       --H

       --I

 --C

    --D

       --J

       --K

    --E


A B F G H I C D J K E

F B H G I A J D K C E

F H I G B J K D E C A

leafs: F  H  I  J  K  E number of leaf:6


请按任意键继续. . .

追问

没有简短一些的吗……这也太复杂了

追答

简短的你就直接看看那个count_leaf函数的实现不就行了啊,又没让你全部看,之所以给你那么多是因为你总要输出出来能跑起来吧,

以上是关于数据结构算法设计——统计二叉树叶子结点的个数,并输出结果的主要内容,如果未能解决你的问题,请参考以下文章

分别统计二叉树中树叶和度为1的结点个数.

设计一个算法把二叉数的叶子结点按从左到右的顺序连成一个单链表,二叉树按ldchild-rchild方式存储,?

Swust OJ973: 统计利用先序遍历创建的二叉树叶结点的个数

设计一个算法,计算出给定二叉树中任意2 个结点之间的最短路径。

C++程序设计创建并统计二叉树的结点个数

PTA 统计二叉树叶子结点个数