求二叉树节点个数、叶子节点、节点层次与宽度

需实现:(1)输出二叉树b的节点个数

              (2)输出二叉树b的叶子节点个数

              (3)求二叉树b中指定节点值(假设所有节点值不同)的节点的层次。

              (4)利用层次遍历求二叉树b的宽度

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
const int MaxSize=10000;
using namespace std;
typedef struct node
{char data;struct node * lchild;struct node * rchild;
}BTNode;
typedef struct qnode
{BTNode *data;struct qnode *next;
}DataNode;
typedef struct
{int Size;DataNode *front;DataNode *rear;
}SqQueue;
void InitQueue(SqQueue * &q)
{q=(SqQueue *)malloc(sizeof(SqQueue));q->front=q->rear=NULL;q->Size=0;
}
bool QueueEmpty(SqQueue *q)
{return (q->rear==NULL);
}
void enQueue(SqQueue * &q,BTNode *e)
{DataNode *p;p=(DataNode *)malloc(sizeof(DataNode));p->data=e;p->next=NULL;if(q->rear==NULL)q->front=q->rear=p;else{q->rear->next=p;q->rear=p;}q->Size++;
}
bool deQueue(SqQueue * &q,BTNode * &e)
{DataNode * t;if(q->rear==NULL)return false;t=q->front;if(q->front==q->rear)q->front=q->rear=NULL;elseq->front=q->front->next;e=t->data;free(t);q->Size--;return true;
}
int QueueSize(SqQueue * q)
{return q->Size;
}
void CreatBTree(BTNode * &b,char * str)         //建立二叉树
{BTNode *St[MaxSize],*p;int top=-1,k,j=0;char ch;b=NULL;ch=str[j];while(ch!='\0'){switch(ch){case '(':top++;St[top]=p;k=1;break;case ')':top--;break;case ',':k=2;break;default:p=(BTNode *)malloc(sizeof(BTNode));p->data=ch;p->lchild=p->rchild=NULL;if(b==NULL)b=p;else{switch(k){case 1:St[top]->lchild=p;break;case 2:St[top]->rchild=p;break;}}}j++;ch=str[j];}
}
int yznum;
char yz[MaxSize];
void YZjiedian(BTNode *b)    //找叶子节点的个数
{if(b!=NULL){if(b->lchild==NULL&&b->rchild==NULL) {yznum++;yz[yznum]=b->data;}YZjiedian(b->lchild);YZjiedian(b->rchild);}
}
int jdnum;
char jd[MaxSize];
void JieDian(BTNode *b)      //找节点的个数
{if(b!=NULL){jdnum++;jd[jdnum]=b->data;JieDian(b->lchild);JieDian(b->rchild);}
}
void PreOrder(BTNode *b)       //先序遍历
{if(b!=NULL){printf("%c ",b->data);PreOrder(b->lchild);PreOrder(b->rchild);}
}
void InOrder(BTNode *b)      //中序遍历
{if(b!=NULL){InOrder(b->lchild);printf("%c ",b->data);InOrder(b->rchild);}
}
void PostOrder(BTNode *b)       //后序遍历
{if(b!=NULL){PostOrder(b->lchild);PostOrder(b->rchild);printf("%c ",b->data);}
}
int result;
void LevelOrder(BTNode *b)         //求二叉树的最大宽度
{BTNode *nape;nape=b;int cnt;BTNode *p;SqQueue *qu;InitQueue(qu);enQueue(qu,b);while(!QueueEmpty(qu)){cnt=QueueSize(qu);result=max(result,cnt);while(cnt--){deQueue(qu,p);if(p->lchild!=NULL)enQueue(qu,p->lchild);if(p->rchild!=NULL)enQueue(qu,p->rchild);}}
}
int FindJieDian(BTNode *b,char c)    //寻找节点层次
{BTNode *nape;nape=b;int cnt;int level=0;BTNode *p;SqQueue *qu;InitQueue(qu);enQueue(qu,b);while(!QueueEmpty(qu)){cnt=QueueSize(qu);level++;while(cnt--){deQueue(qu,p);if(p->data==c)return level;if(p->lchild!=NULL)enQueue(qu,p->lchild);if(p->rchild!=NULL)enQueue(qu,p->rchild);}}return -1;
}
int main()
{char str[MaxSize]={"A(B(D,E(H(J,K(L,M(,N))),)),C(F,G(,I)))"};printf("此棵二叉树为:%s\n",str);BTNode *b;CreatBTree(b,str);JieDian(b);printf("此二叉树的结点个数为:%d\n",jdnum);printf("此二叉树的节点为:");for(int i=1;i<=jdnum;i++)printf(" %c",jd[i]);printf("\n");printf("先序遍历输出节点为:");PreOrder(b);printf("\n");printf("中序遍历输出节点为:");InOrder(b);printf("\n");printf("后序遍历输出节点为:");PostOrder(b);printf("\n");YZjiedian(b);printf("此二叉树的叶子结点个数为:%d\n",yznum);printf("此二叉树的叶子节点为:");for(int i=1;i<=yznum;i++)printf(" %c",yz[i]);printf("\n");LevelOrder(b);printf("此二叉树的最大宽度为:%d\n",result);char ch;while(printf("请输入你要查询的节点:")!=EOF){scanf(" %c",&ch);int nape=FindJieDian(b,ch);if(nape==-1)printf("二叉树中无此节点\n");elseprintf("节点%c在二叉树中的层数为:%d\n",ch,nape);}return 0;
}

 

实现图片:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536328.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数学】Chaarshanbegaan at Cafebazaar

题目描述 Chaarshanbegaan is a gathering event at Cafebazaar similar to TGIF events at Google. Some entertainment programs like pantomime, foosball, Xbox/PS4, and several board games are part of the event. You are going to set up a dart game in Chaarshanbe…

【思维】Congestion Charging Zone

题目描述 Tehran municipality has set up a new charging method for the Congestion Charging Zone (CCZ) which controls the passage of vehicles in Tehran’s high-congestion areas in the congestion period (CP) from 6:30 to 19:00. There are plate detection came…

【二分】LED

题目描述 A Light-Emitting Diode (LED) is a semiconductor light source, which emits light when an electric current of voltage higher than a threshhold is applied to its leads. ACM R&D recently reported that they have succesfully developed a new LED, na…

【模拟】Thanks, TuSimple!

题目链接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId5979 Thanks, TuSimple! Time Limit: 1 Second Memory Limit: 65536 KB In the very first sentence of the very first problem, we would like to give our sincere thanks to TuSimple,…

【二维差分】Monitor

Monitor 题目&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid6514 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 600 Accepted Submission(s): 190 Problem Description Xiaoteng has a la…

【二分+二维前缀和】Largest Allowed Area

Largest Allowed Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 146 解决: 54 [提交] [状态] [命题人:admin] 题目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【数学】Floating-Point Hazard

Floating-Point Hazard 时间限制: 1 Sec 内存限制: 128 MB 提交: 106 解决: 42 [提交] [状态] [命题人:admin] 题目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【线段树】Segment Tree

Segment Tree 时间限制: 1 Sec 内存限制: 512 MB 提交: 107 解决: 23 [提交] [状态] [命题人:admin] 题目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach w…

扶桑号战列舰【RMQ+分治】

扶桑号战列舰 时间限制: 1 Sec 内存限制: 128 MB Special Judge 提交: 197 解决: 63 [提交] [状态] [命题人:admin] 题目描述 众所周知&#xff0c;一战过后&#xff0c;在世界列强建造超无畏级战列舰的竞争之中&#xff0c;旧日本海军根据“个舰优越主义”&#xff0c;建造了扶…

大凤号装甲空母【找规律+矩阵快速幂】

大凤号装甲空母 时间限制: 1 Sec 内存限制: 128 MB 提交: 108 解决: 15 [提交] [状态] [命题人:admin] 题目描述 大凤号航空母舰很喜欢算术。 它&#xff0c;是旧日本海军中最为先进的航空母舰。 它&#xff0c;是旧日本海军中最为短命的航空母舰。 同时&#xff0c;她还是最平…

Degree Sequence of Graph G【模拟】

Degree Sequence of Graph G 时间限制: 1 Sec 内存限制: 128 MB 提交: 362 解决: 92 [提交] [状态] [命题人:admin] 题目描述 Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep lov…

【动态规划】魔法石矿

【动态规划】魔法石矿 时间限制: 1 Sec 内存限制: 64 MB 提交: 116 解决: 27 [提交] [状态] [命题人:admin] 题目描述 为了找到回家的路&#xff0c;张琪曼施展魔法&#xff0c;从高维空间召唤出了一种叫作“读者”的生物&#xff0c;据说“读者”这种生物无所不能&#xff0c;…

简单类及成员实例【C#】

简单类及成员实例&#xff08;C#&#xff09; 题目描述 简单类及成员实例。定义了如下图所示类Student&#xff0c;根据下图和给出代码&#xff0c;补写缺失的代码。 using System; namespace sample{ class Student { public string studentid;//学号 p…

c#随机数的产生与输出【C#】

c#随机数的产生与输出 题目描述 编写一个实例方法Method01。该方法使用Random类随机产生n个3位数字&#xff08;如636&#xff09;的随机正整数&#xff0c;并把产生的随机数存入数组中并输出该数组int num Convert.ToInt32(Console.ReadLine()); using System; using System…

C# teacher类【C#】

C# teacher类 题目描述 定义一个教师类Teacher&#xff0c;具体要求如下&#xff1a; 1、私有字段工号no&#xff08;string&#xff09;、姓名name&#xff08;string&#xff09;、出生日期birthday&#xff08;DateTime&#xff09;、性别sex&#xff08;SexFlag&#xff0…

接口实例(C#,IShape)【C#】

接口实例&#xff08;C#,IShape&#xff09; 题目描述 接口实例。接口和类如下图所示&#xff0c;根据给出代码&#xff0c;补写缺失的代码&#xff0c;然后在Program类的静态Main方法中验证所实现的类。 using System; namespace Myinterface { public interface IShape…

1439: 2.4.5 Fractions to Decimals 分数化小数

1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB提交: 194 解决: 13题目描述 写一个程序&#xff0c;输入一个形如N/D的分数(N是分子&#xff0c;D是分母)&#xff0c;输出它的小数形式。 如果小数有循环节的话&#xff0c;把循环节放在一对圆…

Problem B: 求各位数字之和

#include <stdio.h> #include <stdlib.h> int main() { int n,sum0,m; while(~scanf("%d",&n)) { while(n>0) { mn%10; nn/10; summ; } printf("%d\n",sum); sum0; } return 0; }

Problem C: 判断字符串是否为回文

#include <stdio.h> #include <stdlib.h> int main() { int i,j,n; char str[10]; gets(str); nstrlen(str); for(i0,jn-1;i<j;i,j--) { if(str[i]!str[j]) { printf("No\n"); break; } } if(i>j)printf("Yes\n"); return 0; }

Problem A: 童年生活二三事

斐波那契数列:F(n)F(n-1)F(n-2) #include <stdio.h> #include <stdlib.h> int f(int n) {int b;if(n1)b1;if(n2)b2;if(n>2)bf(n-1)f(n-2);return b; }int main() {int a,n;while(~scanf("%d",&n)&&n!0){af(n);printf("%d\n",a…