UVA122

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

 

思路:不能用数组存储树,因为当每个节点形成一条链的时候,最后一个节点的编号将是巨大的

使用指针模拟树即可

package test;

import java.util.ArrayList;
import java.util.Queue;
import java.util.Scanner;



public class Test{	
	
	public static class Node{
		int value = -1;
		boolean isRead = false;
		Node left;
		Node right;
		
		@Override
		public String toString() {
			return value+"";
		}
	}
	
	static Node root = new Node();
	
	static public void addNode(int value,char[] type){
		Node temp = root;
		for(int i=0,len=type.length;i<len;i++){
			if('l'==type[i]){
				if(temp.left==null){
					Node lnode = new Node();
					temp.left  = lnode;				
				}
				temp = temp.left;
			}else{
				if(temp.right==null){
					Node rnode = new Node();
					temp.right  = rnode;				
				}
				temp = temp.right;				
			}
		}
		if(!temp.isRead){
			temp.value = value;
			temp.isRead = true;
		}else{
			temp.value = -1;
		}
	}
	
	public static void main(String[] args) {		
		Scanner sc = new Scanner(System.in);
		String line = null;
		while(!"".equals((line=sc.nextLine()))){
			String[] temp = line.split(",");
			if("".equals(temp[1])){
				root.value = Integer.valueOf(temp[0]);
			}else{
				addNode(Integer.valueOf(temp[0]),temp[1].toCharArray());
			}
		}
		
		ArrayList<Node> queue = new ArrayList<Node>();
		queue.add(root);
		while(queue.size()!=0){
			System.out.println(queue);
			ArrayList<Node> temp = new ArrayList<Node>();
			for(Node node : queue){
				if(node.left!=null)
					temp.add(node.left);
				if(node.right!=null)
					temp.add(node.right);
			}
			queue.clear();
			queue.addAll(temp);
		}
	}
}

下面是使用数组存储树的方式:

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;



public class Test{	
	static int[] left;
	static int[] right;
	static boolean[] have_value;
	static int[] values;
	static int root = 1;
	static int cnt;
	
	public static void newTree(){
		left[root] = right[root] = 0;
		have_value[root] = false;
		cnt = root;
	}
	
	public static int newNode(){
		int u = ++cnt;
		left[u] = right[u] = 0;
		have_value[u] = false;
		return u;
	}
	
	public static class Node{
		int value = -1;
		boolean isRead = false;
		Node left;
		Node right;
		
		@Override
		public String toString() {
			return value+"";
		}
	}
	
	
	static public void addNode(int value,char[] type){
		int temp = root;
		for(int i=0,len=type.length;i<len;i++){
			if('l'==type[i]){
				if(left[temp]==0){					
					left[temp] = newNode();
				}
				temp = left[temp];
			}else{
				if(right[temp]==0){					
					right[temp]  = newNode();				
				}
				temp = right[temp];				
			}
		}
		if(!have_value[temp]){
			values[temp] = value;
			have_value[temp] = true;
		}else{
			values[temp] = -1;
		}
	}
	
	public static void main(String[] args) {		
		left = new int[100];
		right = new int[100];
		values = new int[100];
		have_value = new boolean[100];
		newTree();
		Scanner sc = new Scanner(System.in);
		String line = null;
		while(!"".equals((line=sc.nextLine()))){
			String[] temp = line.split(",");
			if("".equals(temp[1])){
				values[root] = Integer.valueOf(temp[0]);
			}else{
				addNode(Integer.valueOf(temp[0]),temp[1].toCharArray());
			}
		}
		
		System.out.println(Arrays.toString(values));				
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值