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 ...
随机推荐
- 分布式事务2PC_PENDING异常处理
set heading off;set feedback off;set echo off;Set lines 999;Spool rollback.sqlselect 'ROLLBACK FORCE ...
- 解决:win7右键打开方式添加应用程序无法设置和删除多余的打开方式
win7右键打开方式添加应用程序无法设置 点击“开始”—“运行”,输入“regedit”打开注册表,在“HKEY_CLASSES_ROOT\Applications\”中找到无法添加的程序 ( 比如“ ...
- SP283 NAPTIME - Naptime
SP283 NAPTIME - Naptime 题意: 在某个星球上,一天由N小时构成.我们称0-1点为第一个小时,1-2点为第二个小时,以此类推.在第i个小时睡觉能恢复Ui点体力.在这座星球上住着一 ...
- Windows Internals 笔记——线程优先级
1.每个线程都被赋予0(最低)~31(最高)的优先级数.当系统确定给哪个线程分配CPU时,它会首先查看优先级为31的线程,并以循环的方式进行调度.如果有优先级为31的线程可供调度,那么系统就会将CPU ...
- 末学者笔记--NFS服务和DHCP服务讲解
NFS服务端概述 一.概念: NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS:NFS允许一个系统在网络上与他 ...
- OpenCV-Python:霍夫变换
霍夫变换常用来在图像中提取直线和圆等几何形状.如下图: 我们下面来看看如何使用霍夫变换来检测直线.一条直线可以用数学表达式 y = mx + 或者 ρ = xcosθ + y sinθ表示(极坐标) ...
- [原创]基于Zybo SDIO WiFi模块调试
采用的是RTL8189 SDIO 模块,介绍如下 The Realtek RTL8189ES-VB-CG is a highly integrated single-chip 802.11n Wire ...
- C#学习-显式接口
显式的接口实现解决了命名冲突问题. 在使用显式的接口实现方式时,需要注意以下几个问题. 若显式实现接口,方法不能使用任何访问修饰符,显式实现的成员都默认为私有: 现式实现的成员默认是私有的,所以这些成 ...
- spring boot mybatis打印SQL语句
在logback-spring.xml 文件中添加 <logger name="com.ibatis" level="DEBUG" /> <l ...
- 基于flask+gunicorn+nginx来部署web App
基于flask+gunicorn&&nginx来部署web App WSGI协议 Web框架致力于如何生成HTML代码,而Web服务器用于处理和响应HTTP请求.Web框架和Web服务 ...