0
点赞
收藏
分享

微信扫一扫

ZOJ 1167 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.

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

题目大意:在这个问题中,一个二叉树是由一对对(n,s)序列指定的,其中n是从根的路径中由字符串S给出的值。路径是L和r的序列,L表示左分支,R表示右分支。在树图上,值为13的节点由(13,RL),和值为2的节点由(2,LLR)。根节点由(5,)指定,其中空字符串表示从根到自身的路径。如果树中所有根到节点的路径上的每个节点都精确地给定一次值,则考虑完全指定二叉树。如果给的二叉树完整,则安层输出遍历结果,若不完整,如书中的某些节点没有给出或者节点数据重复,输出not complete

存储在一维数组中,注意需要对每次读入字符进行判断。

二叉树映射为一维数组后,输出时只需要遍历数组,其实只要遍历到最大结点编号处就可以了,输出非零元素的值即可。

#include <stdio.h>
#include <memory.h>
#define maxx 14000
int tree[maxx];
int flag;
char ch;
int binarytree( )
{
int i;
int data;
int p;
while(scanf("%c",&ch)!=EOF)
{
if(ch=='(')
{
data=0;
while(1)
{
scanf("%c",&ch);
//空括号
if(ch==')'&&data==0)
{
if(tree[1]==0)
flag=0;
if(!flag)
printf("not complete\n");
else
{
printf("%d",tree[1]);
for(i=2; i<p+5; i++)
if(tree[i]>0)
printf(" %d",tree[i]);
printf("\n");
}
return 1;
}
if(ch==','&&data==0)
flag=0;
if(ch==',')
break;
data=data*10+ch-'0';
}
p=1;
while(1)
{
scanf("%c",&ch);
if(ch==')')
break;
if(ch=='L')
p*=2;
else if(ch=='R')
p=p*2+1;
}
if(tree[p]>0)
flag=0;
tree[p]=data;
}
}
}
int main()
{
while(1)
{
memset(tree,0,sizeof(tree));
flag=1;
if(!binarytree())
break;
}
return 0;
}

ZOJ 1167   Trees on the Level_二叉树


举报

相关推荐

0 条评论