紫书上的原题 正好学数据结构拿出来做一下

不知道为什么bfs的队列一定要数组模拟……

还可以练习一下sscanf……

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
queue<int>ans;
struct Binary_Tree //二叉树
{
int value;
Binary_Tree *lchild,*rchild;
Binary_Tree()
{
value=;
lchild=rchild=NULL;
}
};
bool add(Binary_Tree *&Root,char *s,int value){ //向二叉树中加入点
if(Root==NULL)
Root=new Binary_Tree();
if(*s=='\0')
{
if(Root->value!=)
{
return false;
}
Root->value=value;
return true;
}
if(*s=='L') //寻找路径
{
return add(Root->lchild,s+,value);
}
else if(*s=='R')
{
return add(Root->rchild,s+,value);
}
return false;
}
void Delete(Binary_Tree *Root) //删除二叉树
{
if(Root==NULL) return ;
Delete(Root->lchild);
Delete(Root->rchild);
delete Root;
}
bool bfs(Binary_Tree *Root) //层次遍历
{
Binary_Tree *q[]; //数组模拟队列
int front=;
int rear=;
q[]=Root;
while (front<rear)
{
Binary_Tree *temp=q[front++];
if(!temp->value) //没有值就返回false
{
return false;
}
ans.push(temp->value); //当前节点入ans队
if(temp->lchild) //左孩子入队
{
q[rear++]=temp->lchild;
}
if(temp->rchild) //右孩子入队
{
q[rear++]=temp->rchild;
}
}
return true;
}
int main(){
Binary_Tree *Root=NULL;
char s[];
bool no=false;
while(true)
{
no=false;
Delete(Root);
Root=NULL; //二叉树初始化
while(true)
{
if(scanf("%s",s)==EOF) //读入
{
return ;
}
if(strcmp(s,"()")==)
{
break;
}
int val;
char loc[];
strcpy(loc,"");
sscanf(&s[],"%d",&val);
char *start=strchr(s,',')+;
sscanf(start,"%[A-Z]",&loc);
if(!add(Root,loc,val))
{
no=true;
}
}
if(no)
{
puts("not complete");
}
else
{
if(!bfs(Root))
puts("not complete");
else
{
while(!ans.empty())
{
int ll=ans.front();
ans.pop();
printf("%d%c",ll,ans.empty()?'\n':' ');
}
}
}
}
return ;
}
/* (11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) () */

[ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树的更多相关文章

  1. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  2. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  3. hdu 1622 Trees on the level

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...

  4. UVA - 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  5. E - Trees on the level

     Trees on the level  Background Trees are fundamental in many branches of computer science. Current ...

  6. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

  8. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

  9. hdu 5200 Trees [ 排序 离线 2指针 ]

    传送门 Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 655 ...

随机推荐

  1. ionic for mac 新建与调试

    ionic官网:http://ionicframework.com/ 首先需要下载node.js,建议node管理方式请先详细查看林一篇博客http://www.cnblogs.com/minyc/p ...

  2. 【 VS 插件开发 】二、了解Vs插件结构

    [ VS 插件开发 ]二.了解Vs插件结构

  3. Linq to Sql 左连接查询

    var query = from t0 in context.ExpressSendMaster join t1 in context.Supplier on t0.SupplierCode equa ...

  4. 十分钟学会 tmux

    tmux 是一款终端复用命令行工具,一般用于 Terminal 的窗口管理.在 macOS 下,使用 iTerm2 能应付绝大多数窗口管理的需求. 如上图所示,iTerm2 能新建多个标签页(快捷键 ...

  5. excel 导入 与 导出

    Excel导入 public ActionResult Excel(HttpPostedFileBase file)        {            HttpPostedFileBase fi ...

  6. webkit框架的使用

    // // JSViewController.m // Library // // Created by 朱逸 on 16/7/7. // Copyright © 2016年 朱逸. All righ ...

  7. 图片切换效果,纯js

    公司有个技术很牛x的“老腊肉”,我向他请教,他给了我个网址,上面蛮多效果的,于是乎~我决定照着效果看能不能自己敲出来,我要变大神X3,重要的事说3遍. 它完成的效果是这样的: 好吧,一步一步来,先把框 ...

  8. 浙大 pat 1003 题解

    1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  9. 梅特卡夫法则(Metcalfe's law)

    如果一个网络中有n个人,那么网络对于每个人的价值与网络中其他人的数量成正比,于是网络对于所有人的总价值与n*(n-1)成正比.

  10. UltraISO PE(软碟通) V9.5.5.2960 官方中文版

    软件名称: UltraISO PE(软碟通)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.9MB图片预览: 软件简介 ...