zoj1167 Trees on the Level 数据结构
来源:互联网 发布:网络教育可以考研吗 编辑:程序博客网 时间:2024/05/17 23:35
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.
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
import java.io.BufferedInputStream;import java.io.PrintWriter;import java.util.Map;import java.util.Scanner;import java.util.Set;import java.util.TreeMap;public class Main {public static void main(String[] args) {new Task().solve();}}class Task {Scanner in = new Scanner(new BufferedInputStream(System.in)) ;PrintWriter out = new PrintWriter(System.out);void calc(String line){TreeMap<Integer , String> hash = new TreeMap<Integer, String>() ;String[] str = line.split(" ") ;int n = 0 ;for(String s : str){s = s.replace("(", "").replace(")", "") ;if(s == null || s.length() == 0){continue ;} n++ ; String[] v = s.split(",") ; if(v.length == 2){ int sum = 1 ; for(char c : v[1].toCharArray()){ if(c == 'L'){ sum = sum * 2 ; } else{ sum = sum * 2 + 1 ; } } if(hash.containsKey(sum)){ out.println("not complete");return ; } hash.put(sum , v[0]) ; } else{ hash.put(1, v[0]) ; }}Set<Integer> nodes = hash.keySet() ;int[] node = new int[n] ;String[] result = new String[n] ;int i = 0 ;for(Map.Entry<Integer, String> e : hash.entrySet()){ result[i] = e.getValue() ; node[i] = e.getKey() ; i++ ;}if(node[0] != 1){out.println("not complete");return ; }for(int t : node){int now = t ;while(now > 1){now >>= 1 ;if(! nodes.contains(now)){out.println("not complete");return ; }}}out.print(result[0]) ;for(int k = 1 ; k < n ; k++){out.print(" " + result[k]);}out.println() ;}void solve() {String line = "" ;while(in.hasNext()){line += " " + in.nextLine() ;if(line.endsWith("()")){calc(line);line = "" ;}//out.flush();}out.flush();}}
- zoj1167 Trees on the Level 数据结构
- TREES ON THE LEVEL
- Trees on the level
- Trees on the level
- Trees on the level
- 数据结构学习之-------树 Trees on the level HDU 1622
- UVa122 Trees on the level
- uva122-Trees on the level
- uva122 trees on the level
- 122 - Trees on the level
- 122 - Trees on the level
- UVA122 - Trees on the level
- UVA_122: Trees on the level
- HDU1622 Trees on the level
- UVA122 Trees on the level
- 122 - Trees on the level
- UVa122:Trees on the level
- 122 Trees on the level
- python正则表达式
- git 版本回退
- leetcode:504. Base 7
- Mybatis
- JavaIO之字符流和字节流
- zoj1167 Trees on the Level 数据结构
- 一个非常有用的函数——COALESCE
- bzoj 2440 完全平方数(容斥)
- Jquery基础之DOM操作
- 【“盛大游戏杯”第15届上海大学程序设计联赛 H】【水题预处理】调和序列
- 基于C语言的几道算法例题
- DELL R730 iDRAC与IPMI的使用
- Linux学习笔记 --- xxx is not in the sudoers file
- hbase集群安装