Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateof-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics. This problem involves building and traversing binary trees.

Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1.

For example, a level order traversal of the tree on the right is: 5, 4, 8, 11, 13, 4, 7, 2, 1.

In this problem a binary tree is specified by a sequence of pairs ‘(n,s)’ where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of ‘L’s and ‘R’s where ‘L’ indicates a left branch and ‘R’ indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.

Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs ‘(n,s)’ as described above separated by whitespace. The last entry in each tree is ‘()’. No whitespace appears between left and right parentheses.

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.

Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ‘not complete’ should be printed.

Sample Input

(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1
not complete

HINT

这个题目可以不适用二叉树,可以使用map排序来解决,但学到数据结构了就使用二叉树来解决的。

程序设计思路是每行进行读取,然后读取这一行中的内容,直到遇到()结束这一组数据。用指针数组来存储数据。只要遇到一组数据就插入到二叉树里面,如果二叉树对应的结点已经插入了数据,就输出错误,如果二叉树查找对应结点的时候遇到了中间还没有插入的结点,那么就先建立一个空的结点,存储的数据string num的大小为0,(不为0说明已经存入了数据)。输出采用的是层序遍历,当发现有结点的数据域的长度为0那么就说明这个点没有插入输出错误。

这个题目程序有很多细节需要注意,针对自己的程序的总结如下:

  1. 使用 new后一定要初始化结点。
  2. 每次输出结果都要将二叉树删除,并将头指针指空。
  3. 删除结点递归的时候一定要先判断左右孩子是否为空,先序遍历也一样。

Accepted

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<sstream> using namespace std;
struct TREE{
string num;
TREE* right;
TREE* lift;
}; void remove(TREE* head){ //删除结点空间
if (!head)return;
if(head->lift) remove(head->lift);
if(head->right) remove(head->right);
delete(head);
} bool insert(TREE* head, string var, string s) { //插入
TREE* p = head,*temp;
for (int i = 0;i < s.size()-1;i++) {
temp = s[i] == 'R' ? p->right : p->lift;
if (temp==NULL){
temp = new TREE; //如果是空的就申请空间
temp->lift = temp->right = NULL;
}
if (s[i] == 'R')p->right = temp;
else p->lift = temp;
p=temp; //向下指
}
if (p->num.size())return 0;
else { p->num = var;return 1; }
} void print(TREE* head) { //首先层序遍历,然后输出,以内要先判断是否合法
vector<TREE *>list; //因为不需要边输出边层序遍历,所以不用使用队列
int i = 0;
if(head) list.push_back(head);
while (i++ < list.size()) { //遍历
if (!list[i-1]->num.size()) { cout << "not complete" << endl;return; }
if (list[i-1]->lift)list.push_back(list[i-1]->lift);
if (list[i-1]->right)list.push_back(list[i-1]->right);
}
for (int i = 0;i < list.size();i++) { //输出
if (i)cout << ' ' << list[i]->num;
else cout << list[i]->num;
}
cout << endl;
} using namespace std;
int main(){
TREE* head=NULL;
string s,svar;
while(getline(cin,s)){ //读取每一行
if (!head) {
head = new TREE; //申请头地址
head->lift = head->right = NULL;
}
stringstream ss(s);
while (ss >> s ) { //读取每一个点
if (s == "()") { //清空并输出。
print(head);
remove(head);
head = NULL;
break;
}
int i = s.find(','); //拆分
svar = s.substr(1, i-1); //数值位
s = s.substr(i + 1, s.size()-1);//路径
if (!insert(head, svar, s)) {
cout << "not complete" << endl;
remove(head);head = NULL;
while (ss >> s)if (s == "()")break;//清空本组数据
while (s != "()")cin >> s;
break; //调出循环,进行下一组
}
}
}
}

Trees on the level UVA - 122的更多相关文章

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

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

  2. 【紫书】Trees on the level UVA - 122 动态建树及bfs

    题意:给你一些字符串,代表某个值被插入树中的位置.让你输出层序遍历. 题解:动态建树. 由于输入复杂,将输入封装成read_input.注意输入函数返回的情况 再将申请新节点封装成newnode(). ...

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

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

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

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

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

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

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

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

  7. E - Trees on the level

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

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

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

  9. Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。

    /** 题目:Trees in a Wood. UVA 10214 链接:https://vjudge.net/problem/UVA-10214 题意:给定a,b求 |x|<=a, |y|&l ...

随机推荐

  1. Nifi组件脚本开发—ExecuteScript 使用指南(三)

    上一篇:Nifi组件脚本开发-ExecuteScript 使用指南(二) Part 3 - 高级特征 本系列的前两篇文章涵盖了 flow file 的基本操作, 如读写属性和内容, 以及使用" ...

  2. SpringCloud之服务调用

    1.Ribbon 1.1负载均衡LB 全称Load Balance,将用户的请求平摊到多个服务器上,从而达到系统的HA.集中式LB:在服务消费者和服务提供者之间使用独立的LB设施,如硬件,由该设施负责 ...

  3. redis数据结构和对象一

    1. SDS:简单动态字符串(simple dynamic string) Redis没有直接使用C语言的字符串,而是自己构建了一种名为简单动态字符串类型,并将SDS用作Redis的默认字符串. SD ...

  4. 后端程序员之路 23、一个c++的api framework

    在"21.一个cgi的c++封装"中,我们封装了cgi,在这之上,我们可以再来封装一个webapi的framework.当然,前文的Casablanca是个不错的选择,但是它比较庞 ...

  5. arch 安装笔记

    arch- 第一次装archLinux时,照着别人的安装教程来安装,由于不懂有些命令的意思,装了好几次才成功,这次趁着热乎,把安装的步骤写下来,为自己踩踩坑(桌面是xfce,下面也有换桌面的方法,我第 ...

  6. 大家最常用的编程论坛是哪个呢,欢迎评论!!掘金16 juejin 简书41 jianshu 博客85 csdn137 csdn

    软件编程交流论坛 掘金  16 juejin 简书  41 jianshu 博客  85 cnblogs csdn  137 csdn stackoverflow 0 思否 github 大家最常用的 ...

  7. HDOJ-3001(TSP+三进制状态压缩)

    Traving HDOJ-3001 这题考察的是状态压缩dp和tsp问题的改编 需要和传统tsp问题区分的事,这题每个点最多可以经过两次故状态有3种:0,1,2 这里可以模仿tsp问题的二进制压缩方法 ...

  8. ACM STU week3

    STU ACM训练week3(2.5-2.15) By@Xiezeju 训练计划的CP4配套资源库 训练时间安排 定期任务 任务 每日 进行1小时的盲打训练锻练手速 打字网站,最好注册账号以保存进度 ...

  9. Linux速通06 系统的初始化服务和监控

    Linux系统引导的顺序 # 掌握 Linux系统引导的顺序 * BIOS的工作是检查计算机的硬件设备,如CPU.内存和风扇速度等 * MBR会在启动盘的第一个块中,大小为512B,其中前446B是引 ...

  10. P1200_你的飞碟在这儿(JAVA语言)

    题目描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者. 不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带 ...