swust oj 987
输出用先序遍历创建的二叉树是否为完全二叉树的判定结果
输入
输入为接受键盘输入的由大写英文字符和"#"字符构成的一个字符串(用于创建对应的二叉树)。
输出
对应的二叉树是否为完全二叉树的判断结果。若是输出"Y",否则输出"N"。
样例输入
A##
ABC####
AB##C##
ABCD###EF##G###
A##B##
ABC##D##EG###
样例输出
Y
N
Y
N
Y
Y
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
typedef char Datetype;
using namespace std;
Datetype value;
int x; typedef struct link{
Datetype date;
struct link *lchild;
struct link *rchild;
}tree; typedef struct queue{
tree *data;
struct queue *next;
}que; typedef struct {
que *front;
que *rear;
}lin; void Initqueue(lin *&L)
{
L=(lin *)malloc(sizeof(lin));
L->front=L->rear=NULL;
} void destroyed(lin *&L)
{
que *p=NULL,*r=NULL;
p=L->front;
while(p!=NULL)
{
r=p;
p=p->next;
free(r);
}
free(L);
} bool pop(lin *&L, tree *&e)
{
que *p;
if(L->rear==NULL)
return false;
p=L->front;
if(L->rear==L->front)
L->front=L->rear=NULL;
else
L->front=p->next;
e=p->data;
free(p);
return true;
} int empty(lin *&L)
{
return (L->rear==NULL);
} void push(lin *&L,tree *e)
{
que *p;
p = (que *)malloc(sizeof(que));
p->data=e;
p->next=NULL;
if(L->rear==NULL)
{
L->front=p;
L->rear=p;
}
else
{
L->rear->next=p;
L->rear=p;
}
} void creattree(tree *&L) //先序建立二叉树
{
char c;
cin>>c;
if(c=='#')
L=NULL;
else
{
L = (tree *)malloc(sizeof(tree)) ;
L->date=c;
creattree(L->lchild);
creattree(L->rchild);
}
} void find(tree *L) //找树的棵树
{
if(L!=NULL)
{
x++;
find(L->rchild);
}
} void destroytree(tree *&L) //销毁树
{
if(L!=NULL)
{
destroytree(L->lchild);
destroytree(L->rchild);
free(L);
}
} int deep(tree *L) //深度
{
int ldep,rdep,max;
if(L!=NULL)
{
ldep=deep(L->lchild);
rdep=deep(L->rchild);
max=ldep>rdep?ldep+:rdep+;
return max;
}
else
return ;
} void finddegree(tree *&L, int n) //找最大度数
{
if(L!=NULL)
{
if(x<n)
x=n;
finddegree(L->lchild,n);
finddegree(L->rchild,n+);
} } void run(tree *L) //层次遍历
{
tree *p=L;
lin *qu;
Initqueue(qu);
if(L!=NULL)
push(qu,p);
while(!empty(qu))
{
pop(qu,p);
cout<<p->date;
if(p->lchild!=NULL)
push(qu,p->lchild);
if(p->rchild!=NULL)
push(qu,p->rchild);
}
destroyed(qu);
} void displayhou(tree *&L) //后序输出
{
if(L!=NULL)
{
displayhou(L->lchild);
displayhou(L->rchild);
cout<<L->date;
}
} void displaypre(tree *&L) //先序输出
{
if(L!=NULL)
{
cout<<L->date;
displaypre(L->lchild);
displaypre(L->rchild);
}
} void creatinpre(tree *&L ,char *in,char *pre,int x)//根据中先序确定后序
{
int k=;
char *p;
if(x<=)
{
L=NULL;
return ;
}
L=(tree *)malloc(sizeof(tree));
L->date = *pre;
for(p=in ; p<in+x; p++)
{
if(*p==*pre)
break;
}
k=p-in;
creatinpre(L->lchild,in,pre+,k);
creatinpre(L->rchild,p+,pre+k+,x-k-);
} void creatinhou(tree *&L ,char *in,char *pre,int x) //根据中后序确定先序
{
int k=;
char *p;
if(x<=)
{
L=NULL;
return ;
}
L=(tree *)malloc(sizeof(tree));
L->date = *pre;
for(p=in ; p>in-x; p--)
{
if(*p==*pre)
break;
}
k=in-p;
creatinhou(L->rchild,in,pre-,k);
creatinhou(L->lchild,p-,pre-k-,x-k-);
} void findson(tree *&L) //找指定节点的儿子
{
if(L!=NULL)
{
if(L->date==x)
{
if(L->lchild==NULL)
cout<<"L:#";
else
cout<<"L:"<<L->lchild->date;
if(L->rchild==NULL)
cout<<",R:#";
else
cout<<",R:"<<L->rchild->date;
return ;
}
findson(L->lchild);
findson(L->rchild);
}
} void finddad(tree *&L) //找指定节点的父亲节点
{
if(L!=NULL)
{
if(L->lchild!=NULL&&L->lchild->date==x||L->rchild!=NULL&&L->rchild->date==x)
{
cout<<L->date;
return ;
} finddad(L->lchild);
finddad(L->rchild);
}
} int find_the_one_degree(tree *&L) //找寻某指定节点的度
{
if(L!=NULL)
{
if(L->date==value)
{
if(L->lchild!=NULL&&L->rchild==NULL||L->lchild==NULL&&L->rchild!=NULL)
return ;
else if(L->lchild==NULL&&L->rchild==NULL)
return ;
else
return ;
}
find_the_one_degree(L->lchild);
find_the_one_degree(L->rchild);
}
} void exchange(tree *&L) //交换左右儿子的值
{
if(L!=NULL)
{
tree *p;
p=L->lchild;
L->lchild=L->rchild;
L->rchild=p;
exchange(L->lchild);
exchange(L->rchild);
}
} void displayin(tree *&L) //中序输出
{
if(L!=NULL)
{
displayin(L->lchild);
cout<<L->date;
displayin(L->rchild);
}
} bool banlancetree(tree *&L) //平衡树
{
if(L==NULL)
return true;
int left=deep(L->lchild);
int right=deep(L->rchild);
int gas=left-right;
if(gas>||gas<-)
return false;
return banlancetree(L->lchild)&&banlancetree(L->rchild);
} bool perfecttree(tree *&L,int deepth) //完全二叉树的判定
{
if(L==NULL)
return true;
if(L->rchild!=NULL&&L->lchild==NULL)
return false;
if(x-deepth>&&L->rchild==NULL)
return false;
return perfecttree(L->lchild,deepth+)&&perfecttree(L->rchild,deepth+);
} int main()
{
tree *L = NULL;
creattree(L);
x=deep(L);
if(perfecttree(L,))
cout<<"Y";
else
cout<<"N";
destroytree(L);
return ;
}
swust oj 987的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
- [Swust OJ 385]--自动写诗
题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535 Descripti ...
随机推荐
- 帆软认证BI工程师FCBA-部分题目
1.安装32位系统的FineBI,最多只能支持2G内存. 正确 错误 2.Spider数据引擎中适合内存化的表通常为数据量小且更新频率较低的表. 正确 错误 3.Spider数据引擎支持跨数据源进行数 ...
- mysql 5.7版本后时间datetime 默认为 0000-00-00 00:00:00 问题
CREATE TABLE `test_user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` char(25) DEFAULT '' ...
- JQuery插件:ScrollTo平滑滚动到页面指定位置
1.准备jQuery库和scrollTo.js插件. <script type="text/javascript" src="js/jquery.js"& ...
- 学习java23种设计模式自我总结
首先先做个广告,以前看过@maowang 这位大神转的Java开发中的23种设计模式详解(转) ,但是看了之后都忘差不多了, 所以,开个帖子边学习边自我总结(纯手敲).一直以来像这种需要长久的运动,真 ...
- python Django2.X,报错 ‘learning_logs ’is not a registered namespace,如何解决?
自己也查阅了自己出现了的问题,其中就有这么个按照书中来写的代码但是Django却是提示了 ‘learning_logs ’is not a registered namespace. 然后错误提示可 ...
- 前言:JNI调用-简单使用
JNI JNI是(Java Native Interface 本地接口)是一个协议,用来沟通Java 代码和C/C++代码,是 Java和 C.C++之间的桥梁. 通过JNI协议,Java可以调用外 ...
- 末学者笔记--Centos7系统部署cobbler批量安装系统
[前言]: cobbler是一个可以实现批量安装系统的Linux应用程序.它有别于pxe+kickstart,cobbler可以实现同个服务器批量安装不同操作系统版本. 系统环境准备及其下载cob ...
- Centos安装Nginx(转载)
一.概述 项目总使用到Nginx的代理转发,学习和整理内容如下,由于是整理所以参考博客大牛的内容,有很多雷同之处,还望见谅(非抄袭对待) 二.Nginx依赖包的安装 yum install gcc y ...
- 激活windows专业版(激活windows10专业版,解决“我们无法在此设备上激活windows因为无法连接到你的组织的激活服务器 ”)
本来系统用的好好的,但是前几天系统突然提示我要去取设置里面激活windows,我就想:我的系统好像是原厂正版的吧,怎么就过期了呢?没办法只能搜索下怎么激活,去系统城,各大网站什么的试了好多密钥全部不行 ...
- Tutorial for MI5 ! flash MI5 to Native Anriod 9
First ,you should download all zips you need Anriod 9.0 rom link:https://drive.google.com/uc?id=1 .. ...