UVA 122 Trees on the level (二叉树层次遍历)

来源:互联网 发布:淘宝隐形降权查询 编辑:程序博客网 时间:2024/06/04 19:27

Background
背景

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.
各种树(数据结构)是许多计算机分支学科的基础。当下最快的并行计算机——比如思维机CM-5——就是建立在一种称之为“胖树”(fat trees)的架构上的。而四叉树和八叉树又是许多计算机图形学算法的基础。

This problem involves building and traversing binary trees.
本问题是关于建立和遍例二叉树的。

 

The Problem
问题

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.
给定一个二叉树序列,你要写一个程序将每棵树按层序访问并打印出来。在这个问题中,二叉树的每个节都值都为正整数,且每棵树的节点数都小于256。

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.
在层序遍例一个树时,指定行中所有的结点应按照从左至右的顺序打印,并且在第k行打印完之后才能打印第k+1行。

For example, a level order traversal of the tree
比如,按层序遍例下面的树的结果是:

 

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.
在本问题中,二叉树是由一系列的二元组(n, s)给出的,其中n是节点的值,s是由根到该节点的路径字符串。路径字符串由一系列的“L”和“R”组成,L代表左子树,R代表右子树。在上图所示二叉树中,值为13的节点由(13, RL)表示,值为2的节点由(2, LLR)表示。根节点由(5,)表示,其中空的路径字符串表示的路径就是由根到根。当一个二叉树中,所有从根到节点(已给出的)的路径上的所有的节点都已给出且只给出一次,那么这个二叉树就认为是完整的定义。

 

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

输入是一组按照上文所述给出的二叉树定义。序列中的每棵树都包含数个二元组(n, s),分别以空格隔开。每棵树的最后是()。所有的左右括号中间都不存在空格。

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.
所有节点的值都为正整数。输入的每棵树都包括至少一个节点,且不会超过256个节点。输入由EOF表示结束。


The 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.
对于输入的每行完整定义的二叉树,都要按层序遍例并打印。如果一棵树没有完整的定义,即一些节点没有给出其值,或是一个节点重复给出,则应该打印出字符串“not complete”。

 

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


步骤:

1.输入

2.建树

3.层序遍历

import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;import java.util.Vector;public class Main {public static Scanner scan = new Scanner(System.in);public static Node root;// 二叉树根节点public static Boolean fail = false;//看建树是否成功public static void main(String[] args) {while(scan.hasNext()){root = new Node();read_input();Vector<Integer> v = new Vector<>();if(!fail&&bfs(root,v)){String str = "";for(int i:v){str += i+" ";}System.out.println(str.trim());}else{System.out.println("not complete");}}}//层序遍历public static boolean bfs(Node root, Vector<Integer> v) {Queue<Node> q = new LinkedList<Node>();q.add(root);while (!q.isEmpty()) {Node u = q.poll();if (!u.is_have)return false;v.add(u.v);if (u.left != null)q.add(u.left);if (u.right != null)q.add(u.right);}return true;}//输入数据public static boolean read_input() {fail = false;while (true) {String str = scan.next();if ("()".equals(str))break;int index = str.indexOf(',');int v = Integer.valueOf(str.substring(1, index));String dis = str.substring(index + 1, str.length() - 1);addNode(v, dis);//建树}return true;}//建树public static void addNode(int v, String dis) {int n = dis.length();Node u = root;for (int i = 0; i < n; i++) {if (dis.charAt(i) == 'L') {if (u.left == null) {u.left = new Node();}u = u.left;} else {if (u.right == null) {u.right = new Node();}u = u.right;}}if (u.is_have == false) {u.v = v;u.is_have = true;} else {fail = true;}}//树节点static class Node {int v;boolean is_have;//是否已经被赋值Node left, right;public Node() {is_have = false;left = null;right = null;}}}


0 0
原创粉丝点击