博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
E - Trees on the level
阅读量:6759 次
发布时间:2019-06-26

本文共 4065 字,大约阅读时间需要 13 分钟。

  

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 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

picture28

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 1not complete
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 const int INF = 0x7fffffff;14 const double EXP = 1e-8;15 const int MS = 260;16 struct node17 {18 string value;19 string path;20 //node(string v = "", string pa = "") :value(va), path(pa){}21 bool operator < (const node &b)22 {23 if (path.length() != b.path.length())24 return path.length() < b.path.length();25 return path < b.path;26 }27 }nodes[MS];28 map
m1, m2;29 int get_comma(string &s)30 {31 int len = s.length();32 for (int i = 0; i < len; i++)33 if (s[i] == ',')34 return i;35 }36 37 int main(int argc, char *argv[])38 {39 string str;40 bool flag = true;41 int cnt = 0;42 ios_base::sync_with_stdio(false);43 while (cin >> str)44 {45 if (str != "()")46 {47 int i = get_comma(str);48 nodes[cnt].value = str.substr(1, i - 1);49 nodes[cnt].path = str.substr(i + 1, str.length() - i - 2);50 if (m1[nodes[cnt].path]) //m1.count(nodes[cnt].path)!=051 flag = false;52 else53 m1[nodes[cnt].path] = 1;54 cnt++;55 }56 else57 {58 if (flag)59 {60 sort(nodes, nodes + cnt);61 if (nodes[0].path.length() == 0) //可能没有根节点62 {63 m2[nodes[0].path] = 1;64 for (int i = 1; i < cnt&&flag; i++)65 {66 if (m2[nodes[i].path.substr(0, nodes[i].path.length() - 1)] == 0)67 flag = false;68 else69 m2[nodes[i].path] = 1;70 }71 }72 else73 flag = false;74 }75 if (flag)76 for (int i = 0; i < cnt; i++)77 {78 if (i)79 cout << " ";80 cout << nodes[i].value;81 }82 else83 cout << "not complete";84 cout << endl;85 m1.clear();86 m2.clear();87 cnt = 0;88 flag = true;89 }90 }91 return 0;92 }

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/4273163.html

你可能感兴趣的文章
cout internal
查看>>
分巧克力 蓝桥杯
查看>>
项目设计阶段的一些事
查看>>
centos 7 进入图形界面
查看>>
UIWebView
查看>>
贝叶斯分类
查看>>
在java中一种中文问题的解决办法
查看>>
Linux常用命令大全
查看>>
C 函数传参问题
查看>>
luoguP1064 金明的预算方案 (有依赖的背包问题)
查看>>
MongoDB聚合
查看>>
2015年度精品 最新力作32位和64位xp,win7,win8,win10系统下载(电脑城专用版)
查看>>
I00040 计算1000以内的勾股数
查看>>
UVA11624:Fire!(BFS + 优化)
查看>>
程序员总结:帮助你早些明白一些道理
查看>>
DI是实现面向切面和面向抽象的前提
查看>>
Server.MapPath和Request.PhysicalApplicationPath的异同
查看>>
lodash
查看>>
AJAX(一)初识AJAX
查看>>
ArcGIS鼠标滚轮方向之注册表篇
查看>>