二叉树的建立并求出叶子节点数目
1二叉树采用链表存储结构,实现建立、遍历(先序、中序、后序)、求结点总数、叶子数、度为1.2的结点数。
前几天写的,输入二叉树的广义表形式,建立二叉树的链式存储。输出的是中序。有注释。例如输入:a(b,c(d,e(f)),g,h(i))
#include
#include
int n=0; //全局变量
struct tree //二叉树结构体
{
char data;
struct tree *lc;
struct tree *rc;
};
tree *creat(char a[]) //创建树的二叉树
{
tree *h;
h=(tree *)malloc(sizeof(tree));
h->lc=NULL;
h->rc=NULL;
if(a[n]!=')'&&a[n]!='('&&a[n]!=',') //当a[n]为字母存入a[]
{
h->data=a[n];
n++;
}
if(a[n]=='(') //a[n]为左括弧对h->lc递归操作
{
n++;
h->lc=creat(a);
}
if(a[n]==',') //a[n]为逗号对h->rc递归操作
{
n++;
h->rc=creat(a);
return h;
}
if(a[n]==')') //a[n]为右括弧返回h {
n++;
return h;
}
else
return h;
}
void print(tree *h) //二叉树中序输出{
if(h!=NULL)
{
print(h->lc);
printf("%c",h->data);
print(h->rc);
}
}
int high(char a[]) //判断树的高度{
int i=0,max=0,p=0;
while(a[i]!=0)
{
if(a[i]=='(')
{
p++;
if(max
max=p;
}
if(a[i]==')')
p--;
i++;
}
if(p!=0)
{
printf("左右括弧数不等,输入错误\n"); //判断左右括弧数是否相等
exit(1);
}
return max+1;
}
void main() //主函数
{
int i=0;
tree *h;
char a[50]={0};
gets(a);
while(a[i]!=0) //判断各种可能出现的输入错误
{
if(i==0&&(a[i]=='('||a[i]==')'||a[i]==',')) //判断数组首元素是否为字母{
printf("首节点错误\n");
exit(1);
}
if(a[i]=='(') //左括弧之前一定是字母
{
if(a[i-1]=='('||a[i-1]==')'||a[i-1]==',')
{
printf("输入错误\n");
exit(1);
}
}
if(a[i]!='('&&a[i]!=')'&&a[i]!=',') //两个字母不能在一起
{
if(a[i+1]!='('&&a[i+1]!=')'&&a[i+1]!=',')
{
printf("输入错误,两个字母不能在一起\n");
exit(1);
}
}
i++;
}
h=creat(a); //创建树
printf("该树的高度为:%d\n",high(a));
printf("该二叉树的中序输出为:");
print(h);
printf("\n");
}
2一棵二叉树中共有70个叶子结点与80个度为1的结点,则该二叉树中的总结点数为?给出的答案是219 求高手解析
假设n表示二叉树的所有结点数,n0表示度为0的结点(叶子结点),n1表示度为1的结点,n2表示度为2的结点,由二叉树的性质有:
n0 = n2 + 1
已知n0 = 70,
则n2 = n0 -1 = 69
而
n = n0 + n1 + n2
= 70 + 80 + 69
= 219
3创建一棵二叉树,以二叉链表作存储结构,实现先根遍历算法2、创建一棵二叉树,实现先根遍历算法、中根
你需要的东西这里面都包含了,你自己看哪里用得上就摘一段下来吧。
// 数据结构.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include
#include
#define MAX 20
#define NULL 0
typedef char ElemType;
typedef int Status;
typedef enum{link,thread} pointertag;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
pointertag Ltag,Rtag;
}BiTNode,*BiTree;
BiTree pre;
Status CreateBiTree(BiTree *T)//按前序构建二叉树。
{
char ch;
ch=getchar();
if(ch=='#')
*T=NULL;
else
{
*T=(BiTree)malloc(sizeof(BiTNode));
(*T)->data=ch;
(*T)->Ltag=(*T)->Rtag=link;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
return 1;
}
Status CreateBiTree1(BiTree *T)//按中序输入构建二叉树。{
char ch;
ch=getchar();
if(ch=='#')
*T=NULL;
else
{
*T=(BiTree)malloc(sizeof(BiTNode));
CreateBiTree(&(*T)->lchild);
(*T)->data=ch;
CreateBiTree(&(*T)->rchild);
}
return 1;
}
void MidOrder(BiTree T)//递归中序输出。
{
if(T)
{
MidOrder(T->lchild);
printf("%2c",T->data);
MidOrder(T->rchild);
}
}
void PreOrder(BiTree T)//前序输出。
{
if(T)
{
printf("%2c",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void EndOrder(BiTree T)//后序输出。
{
if(T)
{
EndOrder(T->lchild);
EndOrder(T->rchild);
printf("%2c",T->data);
}
}
void MidOrder1(BiTree T)//非递归中序输出。{
int i=0;
BiTree p,s[100];
p=T;
do
{
while(p)
{
s[i++]=p;
p=p->lchild;
}
if(i>0)
{
p=s[--i];
printf("%2c",p->data);
p=p->rchild;
}
}while(i>0||p!=NULL);
}
void Levle(BiTree T)//按层次遍历二叉树。{
BiTree Queue[MAX],b;
int front,rear;
front=rear=0;
if(T)
{
Queue[rear++]=T;
while(front!=rear)
{
b=Queue[front++];
printf("%2c",b->data);
if(b->lchild!=NULL)
Queue[rear++]=b->lchild;
if(b->rchild!=NULL)
Queue[rear++]=b->rchild;
}
}
}
int depth(BiTree T)//求二叉树的深度。
{
int dep1,dep2;
if(T==NULL)return 0;
else
{
dep1=depth(T->lchild);
dep2=depth(T->rchild);
return dep1>dep2?dep1+1:dep2+1;
}
}
void InThreading(BiTree p)//线索化。
{
if(p)
{
InThreading(p->lchild);
if(p->lchild=NULL){p->Ltag=thread;p->lchild=pre;}
if(pre->rchild==NULL){pre->Rtag=thread;pre->rchild=p;}
pre=p;
InThreading(p->rchild);
}
}
Status InorderThreading(BiTree *Thrt,BiTree T)//中序遍历二叉树T,并将其线索化。{
(*Thrt)=(BiTree)malloc(sizeof(BiTNode));
(*Thrt)->Ltag=link;
(*Thrt)->Rtag=thread;
(*Thrt)->rchild=*Thrt;
if(T==NULL)
(*Thrt)->lchild=*Thrt;
else
{
(*Thrt)->lchild=T;
pre=*Thrt;
InThreading(T);
pre->rchild=(*Thrt);
pre->Rtag=thread;
(*Thrt)->rchild=pre;
}
return 1;
}
Status InorderTraverse(BiTree Thrt)//中序遍历线索二叉树Thrt,Thrt指向头结点。{
BiTree p;
p=Thrt->lchild;
while(p!=Thrt)
{
while(p->Ltag==link)p=p->lchild;
printf("%2c",p->data);
while(p->Rtag==thread&&p->lchild!=Thrt)
{
p=p->rchild;
printf("%2c",p->data);
}
p=p->rchild;
}
return 1;
}
void main()
{
BiTree T=NULL,Thrt=NULL;
printf("创建一个二叉树\n");
CreateBiTree(&T);
//InorderThreading(&Thrt,T);
//InorderTraverse(Thrt);
//printf("中序遍历的结果是:");
//MidOrder(T);
//printf("\n");
printf("先序遍历的结果是:");
PreOrder(T);
printf("\n");
/*printf("后序遍历的结果是:");
EndOrder(T);
printf("\n");
printf("按层次遍历二叉树的结果是:");
Levle(T);
printf("\n");
printf("深度输出为:%d",depth(T));
printf("\n");*/
}
#include "stdio.h"
#include "malloc.h"
#define ELEMTYPE char
typedef struct BiTNode
{ ELEMTYPE data;
struct BiTNode *lchild,*rchild;
} BiTNode;
BiTNode *bulid() /*建树*/
{ BiTNode *q;
BiTNode *s[20];
int i,j;
char x;
printf("请按顺序输入二叉树的结点以输入0和*号结束\n"); printf("请输入你要输入的为第几个结点i=\n");
scanf("%d",&i);
printf("请输入你要输入该结点的数为x=");
getchar();
scanf("%c",&x);
while(i!=0&&x!='*')
{q=(BiTNode*)malloc(sizeof(BiTNode));
q->data=x;
q->rchild=NULL;
q->lchild=NULL;
s[i]=q;
if(i!=1)
{ j=i/2;
if(i%2==0)
s[j]->lchild=q;
else
s[j]->rchild=q;
}
printf("请输入你要输入的为第几个结点x=\n");
scanf("%d",&i);
printf("请输入你要输入该结点的数x=");
getchar();
scanf("%c",&x);
}
return s[1];
}
void preoder(BiTNode *bt) /*先序遍历*/ { if(bt!=NULL)
{
printf("%c\n",(bt->data));
preoder(bt->lchild);
preoder(bt->rchild);
}
}
void InOrder(BiTNode *bt) /*中序遍历*/ { if(bt!=NULL)
{ InOrder(bt->lchild);
printf("%c\n",bt->data);
InOrder(bt->rchild);
}
}
void postOrder(BiTNode *bt) /*后序遍历*/ { if(bt!=NULL)
{ postOrder(bt->lchild);
postOrder(bt->rchild);
printf("%c\n",bt->data);
}
}
main()
{ int a;
BiTNode *bt;
bt=bulid();
k1: printf("需要先序遍历输出请输入1,中序遍历请输入2,后序遍历请输入3,结束输入0:");
scanf("%d",&a);
switch(a)
{
case(1): preoder(bt); goto k1;
case(2): InOrder(bt); goto k1;
case(3): postOrder(bt); goto k1;
case(0): break;
}
}
求二叉树的深度叶子结点数总结点数()
#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)
二叉树叶子结点个数计算
计算二叉树叶子结点 1.程序设计简介 已知一棵二叉树,求该二叉树中叶子结点的个数。 2.基本要求 (1)设计二叉树的二叉链表为存储结构 (2)设计求叶子结点个数的递归算法 (3)输入:一颗二叉树 (4)输出:二叉树中叶子结点的个数 3.实现提示 (1)存储设计 二叉树采用二叉链表为存储结构 (2)算法设计 求二叉树中叶子结点个数,即求二叉树的所有结点中左、右子树均为空的结点个数之和。可以将此问题转化为遍历问题,在遍历中“访问一个结点”时判断该结点是不是叶子,若是则将计数器累加。 4.源程序 #include
struct BiNode 行与测试 6.调试感想 非递归算法求叶子结点的个数 #include
(root); root=root->lchild; } else { root=(); // cout<
实验报告二叉树求叶子结点数目(内容清晰)
实验叶子结点的计算 姓名:xxx 班级:xxx) 学号:16130xxxxx 时间2017.10.22 1 问题描述 二叉树叶子节点的计算 1.二叉树的创建 2.二叉树的图形显示 3.二叉树叶子节点的计算 2 结构设计 二叉树叶子结点的计算主要是二叉树的创建,在这里选择的存储结构是一个链式存Data lchild rchild struct BTNode{ int data; BTNode*lchild; BTNode*rchild; }; 3 算法设计 在程序正式编写之前我定义了几个功能函数 (1)指针清空函数,预定义一个指针bt 使lchild和rchild的值分别赋予bt并且使其为空 static int clear(BTNode *bt) { if (bt) { clear(bt->lchild ); clear(bt->rchild ); cout<<"释放了指针"< { if(p->lchild==NULL&&p->rchild==NULL)count++; Leaf(p->lchild,count); Leaf(p->rchild,count); } return count; } (2)二叉树的创建 同样是利用递归的方式,输入参数包括指针,左右判断,以及判空条件static int create(BTNode *p,int k ,int end) { BTNode *q; int x; cin>>x; if(x!=end) { q=new BTNode; q->data =x; q->lchild=NULL; q->rchild=NULL; if(k==1)p->lchild=q; if(k==2)p->rchild=q; create(q,1,end); create(q,2,end); } return 0; }; (3)类的构造函数创建树并且输入各结点数值 在这里,采用的时先序遍历法依次输入树中的各结点数值 Step 1:定义新的结构体指针, Step 2:申请动态存储空间; Step 3:输入节点元素,并且指针后移到输入结点的后继结点,end作为结点结束标志; Step 4:重复步骤3,直到输入结束; void BinaryTree::CreateBiTree (int end) { cout<<"请按照先序序列的顺序输入二叉树,-1为空指针域标志:"< #include 题目一:统计二叉树中叶子结点的个数 [问题描述] 已知一棵二叉树,求该二叉树中叶子结点的个数。 [基本要求] (1)采用二叉链表作存储结构,存储二叉树; (2)输出前序、中序、后序遍历该二叉树的遍历结果; (3)设计递归算法求叶子结点的个数; (4)设计非递归算法求叶子结点的个数。 [测试数据] [源代码] //tree1.h #ifndef tree1_H #define tree1_H #include void PreOrder() {PreOrder(root);} void InOrder() {InOrder(root);} void PostOrder() {PostOrder(root);} void CountLeaf1() {cout< 求二叉树叶子节点的个数并输出 实验目的: 设二叉树采用链式存储结构,试设计一个算法计算一颗给定二叉树中叶子结点的数目。 实验类容与步骤: (1)建立一颗二叉树; (2)先序遍历输出该二叉树; (3)计算出该二叉树的叶子结点个数; (4)输出叶子结点个数; 实验平台: Windows xp 操作系统,VC 6.0集成环境 实验设计方案: (1)输入扩展先序遍历序列并建立对应的二叉树. 输入#表示输入的二叉树元素为空。输入回车键表示输入结束。 (2)先序输出当前二叉树的叶子节点和叶子节点个数. 源程序代码: #include /* HELLO.C -- Hello, world */ #include "stdio.h" #include "conio.h" #include "malloc.h" /*=============二叉树的二叉链表存储表示===================*/ typedef struct BiTNode {int data; struct BiTNode *lchild,*rchild;/*左右孩子指针*/ }; typedef struct BiTNode chenchen; /*=============构建二叉树======================*/ chenchen *create() {int x; static int z=0; chenchen *p; z=z+1; printf("%3d: ",z); scanf("%3d",&x); if(x!=0) {p=(chenchen*)malloc(sizeof(chenchen)); p->data=x; p->lchild=create(); p->rchild=create();} else p=0; return p; } /*=====构建函数计算叶子节点的个数======*/ int count(chenchen *t){ static int y=0; if(t) {count(t->lchild); count(t->rchild); if(t->lchild==0&&t->rchild==0) {y++;}} return y;} /*============主函数===============*/ main() { chenchen *T ;int c; printf("Input the data:\n"); T=create(); if(T){c=count(T);printf("\nNumber=%d",c);} else{printf("Empty");}printf("\n"); getch(); } 南通大学数据结构实践教程 实验报告册 1.程序设计简介 已知一棵二叉树,求该二叉树中叶子结点的个数。 2.基本要求 (1)设计二叉树的二叉链表为存储结构 (2)设计求叶子结点个数的递归算法 (3)输入:一颗二叉树 (4)输出:二叉树中叶子结点的个数 3.实现提示 (1)存储设计 二叉树采用二叉链表为存储结构 (2)算法设计 求二叉树中叶子结点个数,即求二叉树的所有结点中左、右子 树均为空的结点个数之和。可以将此问题转化为遍历问题,在遍历中“访问一个结点”时判断该结点是不是叶子,若是则将计数器累加。 4.源程序 #include void BiTree::yezi(BiNode *root,int &n); private: BiNode *root; //指向根结点的头指针 BiNode *Creat( ); //有参构造函数调用 void Release(BiNode *root); //析构函数调用}; BiTree::BiTree( ) { root = Creat( ); } BiTree::~BiTree(void) { Release(root); } BiNode* BiTree::Getroot( ) { return root; } #include //两种算法实现计算二叉树叶子节点的个数 //二叉树叶子节点个数算法之非递归算法和递归算法 #include"stdio.h" #include"stdlib.h" #define MAXSIZE 12500 typedef struct BitNode{ char data; struct BitNode *lchild,*rchild; }*BitTree; int w = 0; typedef struct stack{ int top; BitTree MaxSize[MAXSIZE]; }*Stack; void creattree(BitTree *T) { char ch; scanf("%c",&ch); if(ch == '.') *T = NULL; else{ (*T) = (struct BitNode *)malloc(sizeof(struct BitNode)); (*T)->data = ch; creattree(&((*T)->lchild)); creattree(&((*T)->rchild)); } } void PrintTree1(BitTree Boot) { Stack S; S = (Stack)malloc(sizeof(struct stack)); S->top = 0; while(Boot != NULL||S->top != 0) { if(Boot != NULL) { printf("%2c",Boot->data); S->MaxSize[S->top] = Boot; S->top++; Boot = Boot->lchild; } else { Boot = S->MaxSize[S->top-1]; S->top--; if(Boot->rchild == NULL && Boot->lchild == NULL) { w ++; } Boot = Boot->rchild; } } } int leaf(BitTree T) { if(!T) return 0; //空树,无叶子 else if(!(T)->lchild && !(T)->rchild) return 1; else return (leaf(T->lchild) + leaf(T->rchild)); } main() { BitTree T; printf("请先序输入二叉树(‘.’代表空子树):\n"); creattree(&T); printf("先序输出为:"); PrintTree1(T); printf("\n"); printf("%d",w); printf("\n"); w = leaf(T); printf("%d",w); printf("\n"); 二叉树叶子结点个数计算 姓名:许严班级:计122 学号:1213023050 1.问题描述 已知一棵二叉树,求该二叉树中叶子结点的个数。 2.基本要求 (1)设计二叉树的二叉链表存储结构。 (2)设计求叶子结点个数的递归算法。 (3)输入:一棵二叉树。 (4)输出:二叉树中叶子结点的个数。 3.实验提示 (1)存储设计 二叉树采用二叉链表为存储结构。 typedef struct BiTNode{ TElemType data; Struct BiTNode *lchild,*rchild;//左右孩子指针 } BiTNode,*BiTree; (2)算法设计 求二叉树中叶子结点个数,即求二叉树的所有结点中左右字数均为空的结点个数之和。可以将此问题转化为遍历问题,在遍历中“访问一个结点”时判断该结点是不是叶子,若是则将计数器累加。算法如下: Void CountLeaf(BiNode 本人上机实测,数据结构c++ ,可以运行,放心使用 #include } int PostOrderTraverse(BiTree T){ if(T){ PostOrderTraverse(T->Lchild); PostOrderTraverse(T->Rchild); cout< 1二叉树采用链表存储结构,实现建立、遍历(先序、中序、后序)、求结点总数、叶子数、度为1.2的结点数。 前几天写的,输入二叉树的广义表形式,建立二叉树的链式存储。输出的是中序。有注释。例如输入:a(b,c(d,e(f)),g,h(i)) #include n++; h->rc=creat(a); return h; } if(a[n]==')') //a[n]为右括弧返回h { n++; return h; } else return h; } void print(tree *h) //二叉树中序输出{ if(h!=NULL) { print(h->lc); printf("%c",h->data); print(h->rc); } } int high(char a[]) //判断树的高度{ int i=0,max=0,p=0; while(a[i]!=0) { if(a[i]=='(')设二叉树采用链式存储结构,试设计一个算法计算一棵给定二叉树中叶子结点的数目
二叉树中叶子结点的个数
8.求二叉树叶子节点的个数并输出
二叉树计算叶子节点的算法(数据结构)C语言版
二叉树叶子结点个数计算
设二叉树采用链式存储结构,试设计一个算法计算一棵给定二叉树中叶子结点的数目
二叉树中叶子结点个数算法
二叉树叶子结点个数计算
数据结构二叉树遍历叶子结点及深度
二叉树的建立并求出叶子节点数目