您的位置:首页 > 其它

UVA 122 - Trees on the level

2013-03-05 16:37 351 查看

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

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

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.

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


Solution1:

#define RUN
#ifdef RUN

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int MAXN = 256;

typedef struct TNode{
int have_value;	//是否被赋值过
int v;			//节点值
struct TNode* left, *right;	//左子树和右子树
} Node;

Node* root;

//创建节点
Node* newnode() {
Node* u = (Node*) malloc(sizeof(Node));
//初始化
if(u != NULL) {
u->have_value = 0;
u->left = u->right = NULL;
}
return u;
}

int failed;
//添加一个节点
void addnode(int v, char* s) {
int n = strlen(s);
Node* u = root;		//从根节点开始向下走
for(int i = 0; i < n; i++)
if(s[i] == 'L') {
//节点不存在则建立新节点
if(u->left == NULL) u->left = newnode();
//向左走
u = u->left;
}
else if(s[i] == 'R') {
if(u->right == NULL) u->right = newnode();
u = u->right;
}//忽略其他情况,即最后那个多余的右括号

//已经赋过值,表示输入有误
if(u->have_value) failed = 1;
u->v = v;
u->have_value = 1;	//标记赋过值
}

//删除树
void remove_tree(Node* u) {
if(u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
free(u);
}

//保存读入节点
char s[MAXN + 10];

//输入部分
int read_input() {
failed = 0;

//防止内存泄露
remove_tree(root);

//创建根节点
root = newnode();
for(;;) {
if(scanf("%s", s) != 1) return 0;
// 读到结束标志(),退出循环
if(!strcmp(s, "()")) break;

int v;
//读入节点值
sscanf(&s[1], "%d", &v);
//查找逗号的位置,并插入节点
addnode(v, strchr(s, ',')+1);
}
return 1;
}

//节点总数和输出序列
int n = 0, ans[MAXN];
//BFS输出
int bfs() {
int front = 0, rear = 1;
Node* q[MAXN];
//初始时只有一个根节点
q[0] = root;
while(front < rear) {
Node* u = q[front++];
//有节点没有被赋值过,表示输入有误
if(!u->have_value) return 0;
//增加到输出序列的尾部
ans[n++] = u->v;
//如果有左儿子,就放进队列
if(u->left != NULL) q[rear++] = u->left;
//如果有右儿子,就放进队列
if(u->right != NULL) q[rear++] = u->right;
}
return 1;
}

int main() {

#ifndef ONLINE_JUDGE
freopen("122.in", "r", stdin);
freopen("122.out", "w", stdout);
#endif

while(read_input()) {

if(!bfs()) failed = 1;
if(failed) printf("not complete\n");
else {
for(int i = 0; i < n; i++){
if(i==n-1){
printf("%d", ans[i]);
}
else{
printf("%d ", ans[i]);
}
}

printf("\n");
}
n = 0;
}
return 0;
}

#endif


Solution2:

#define RUN
#ifdef RUN

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int MAXN = 256;
const int root = 1;
int cnt, vis[MAXN], val[MAXN], left[MAXN], right[MAXN];

int newnode() { int u = ++cnt; left[u] = right[u] = 0; return u; }
void newtree() { left[root] = right[root] = 0; cnt = root; }

int failed;
void addnode(int v, char* s) {
int n = strlen(s), u = root;
for(int i = 0; i < n; i++)
if(s[i] == 'L') {
if(!left[u]) left[u] = newnode(); u = left[u];
} else if(s[i] == 'R') {
if(!right[u]) right[u] = newnode(); u = right[u];
}
if(vis[u]) failed = 1;
val[u] = v;
vis[u] = 1;
}

char s[MAXN + 10];
int read_input() {
failed = 0;
newtree();
for(;;) {
if(scanf("%s", s) != 1) return 0;
if(!strcmp(s, "()")) break;
int v;
sscanf(&s[1], "%d", &v);
addnode(v, strchr(s, ',')+1);
}
return 1;
}

int n = 0, ans[MAXN];
int bfs() {
int front = 0, rear = 1;
int q[MAXN];
q[0] = root;
while(front < rear) {
int u = q[front++];
if(!vis[u]) return 0;
ans[n++] = val[u];
if(left[u]) q[rear++] = left[u];
if(right[u]) q[rear++] = right[u];
}
return 1;
}

int main() {

#ifndef ONLINE_JUDGE
freopen("122.in", "r", stdin);
freopen("122.out", "w", stdout);
#endif

while(read_input()) {

if(!bfs()) failed = 1;
if(failed) printf("not complete\n");
else {
for(int i = 0; i < n; i++){
if(i == n-1){
printf("%d", ans[i]);
}
else{
printf("%d ", ans[i]);
}
}
printf("\n");
}

n = 0;
cnt = 0;
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
memset(val, 0, sizeof(val));
memset(left, 0, sizeof(left));
memset(right, 0, sizeof(right));
}
return 0;
}

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: