1101-Trees on the Level
描述
Trees are fundamental in many branches of computer science. Current state-of-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 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
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.
输入
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.
输出
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.
样例输入
(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
样例输出
5 4 8 11 13 4 7 2 1
not complete
#include <iostream>
#include <queue>
using namespace std; typedef struct tNode{
int data;
struct tNode *lchild, *rchild;
tNode()
{
lchild = rchild = NULL;
data = -9999999;
}
}*Node;
bool flag;
class Tree{
private:
Node root;
public:
Tree()
{
root = new tNode();
}
void insert(int data, char path[]); //????????????
void print(int d); //??????
void destory(); //????????????
bool isBitTree(); //????????????????????????
}; void Tree::insert(int data, char path[])
{
int len = strlen(path);
int i;
Node cur = root;
for(i = 0; i < len - 1; i++)
{
if(path[i] == 'L')
{
if(cur->lchild == NULL)
cur->lchild = new tNode();
cur = cur->lchild;
}else if(path[i] == 'R')
{
if(cur->rchild == NULL)
cur->rchild = new tNode();
cur = cur->rchild;
}
}
if(cur->data == -9999999)
cur->data = data;
else
flag = false;
}
bool Tree::isBitTree()
{
queue<Node> q;
q.push(root);
while(!q.empty())
{
Node cur = q.front();
q.pop();
if(cur->data == -9999999)
return false;
if(cur->lchild)
q.push(cur->lchild);
if(cur->rchild)
q.push(cur->rchild);
}
return true;
}
void Tree::destory()
{
queue<Node> q;
q.push(root);
while(!q.empty())
{
Node cur = q.front();
q.pop();
if(cur->lchild)
q.push(cur->lchild);
if(cur->rchild)
q.push(cur->rchild);
delete cur;
}
root = new tNode();
}
void Tree::print(int d)
{
queue<Node> q;
q.push(root);
int w = 0;
while(!q.empty())
{
Node cur = q.front();
q.pop();
if(w == d - 1)
cout<<cur->data;
else
cout<<cur->data<<" ";
if(cur->lchild)
q.push(cur->lchild);
if(cur->rchild)
q.push(cur->rchild);
w++;
}
cout<<endl;
}
int main()
{
char snode[600];
int data, d = 0;
Tree t;
flag = true;
while(cin>>snode)
{
if(strcmp(snode, "()") == 0)
{
if(t.isBitTree() && flag)
t.print(d);
else
cout<<"not complete"<<endl;
t.destory();
d = 0;
flag = true;
continue;
}
sscanf(snode + 1, "%d", &data);
t.insert(data, strchr(snode, ',') + 1);
d++;
}
return 0;
}
1101-Trees on the Level的更多相关文章
- E - Trees on the level
Trees on the level Background Trees are fundamental in many branches of computer science. Current ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1622 Trees on the level(二叉树的层次遍历)
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- uva 122 trees on the level——yhx
题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
- Trees on the level (二叉链表树)
紫书:P150 uva122 Background Trees are fundamental in many branches of computer science. Current state- ...
随机推荐
- [转]Windows Shell 编程 第四章 【来源 http://blog.csdn.net/wangqiulin123456/article/details/7987933】
第四章 文件的本质 以前,所有文件和目录都有一个确定的属性集:时间,日期,尺寸,以及表示‘只读的’,‘隐藏的,‘存档的’,或‘系统的’状态标志.然而,Windos95(及后来的WindowsNT4.0 ...
- 怎么在ubuntu上使用pidgin登陆QQ
1.Ubuntu pidgin安装webqq 首先安装webqq的通讯协议: sudo add-apt-repository ppa:lainme/pidgin-lwqq sudo apt-get u ...
- 40个Java集合面试问题和答案【上】【转载】
1.Java集合框架是什么?说出一些集合框架的优点? 每种编程语言中都有集合,最初的Java版本包含几种集合类:Vector.Stack.HashTable和Array.随着集合的广泛使用,Java1 ...
- Objective-C 【多态】
------------------------------------------- 多态的概念.实现以及注意事项 程序中的多态:不同的对象 以自己的方式去 响应 相同方法名(父类同 ...
- HDU 3943 K-th Nya Number(数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3943 题目大意:求出区间 (P,Q] 中找到第K个满足条件的数,条件是该数包含X个4和Y个7 Samp ...
- ESP8266开发课堂之 - 建立一个新项目
项目架构 ESP8266项目开发并非使用IDE自动管理工程文件,而是使用了诸多第三方程序如Python,以及使用了Makefile管理依赖与控制编译,所以项目的创建与日常维护较为复杂,本篇将详述创建一 ...
- Update files embedded inside CAB file.
References: https://community.flexerasoftware.com/showthread.php?182791-Replace-a-single-file-embedd ...
- N的N次方(高校俱乐部)
最近一直在刷字符串和线段树,也越来越少玩高校俱乐部,无聊看到一题N的N次方的问题,脑海中各种打表就涌现出来了. 弄了不一会儿,就写完了,马上提交,但是系统好像出了问题,提示"哦哦,出了点状况 ...
- Percona-Server-5.5.15源码安装
[root@localhost rpm]# ll total 19148 -rw-r--r-- 1 root root 562628 Jan 18 2007 bison-2.3-2.1.x86_ ...
- Windows server2008/2012 安装oracle 11 创建实例HANG住在百分之2
Windows server2008/2012 安装oracle 11.2.0.1的时候,可能会在创建数据库实例的时候卡在百分之2的地方. 这个时候可以 1.点击开始菜单,在“搜索程序和文件”中输入“ ...