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 onfat 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 alevel-ordertraversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at levelkare printed before all nodes at levelk+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) wherenis the value at the node whose path from the root is given by the strings. A path is given be a sequence ofL's andR's whereLindicates a left branch andRindicates 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 becompletely specifiedif 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
相关推荐
开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…
标题中的"UVaOJ-401(Palindromes)"表明这是一个关于解决UVa Online Judge(UVa OJ)上编号为401的编程挑战,该挑战的主题是"Palindromes",即回文串。回文串是指一个字符串无论从前读到后还是从后读到前都是相同的,...
### Uva 1510 - Neon Sign #### 问题背景与描述 在题目“Uva 1510 - Neon Sign”中,我们面对的是一个霓虹灯招牌设计问题。该霓虹灯招牌由一系列位于圆周上的角点组成,并通过发光管连接这些角点。发光管有两种...
【标题】"uva705-Slash-Maze-.rar_Slash_uva705" 指向的是一个在UVa Online Judge (UVa OJ) 上提交并通过的编程问题,具体为问题编号705,名为"Slash Maze"。这个压缩包很可能包含了该问题的解决方案源代码。 ...
4. **UVA152 - The Stock Maximal Sequence**:此题属于动态规划,要求找到股票价格序列中的最大收益。关键在于找到合适的状态转移方程,考虑买卖股票的最佳时机。 5. **UVA107 - Robot Walk**:该题考察图论和深度...
《UVA133 - 救济金发放问题:The Dole Queue》 在计算机科学领域,算法是解决问题的关键工具,特别是在处理复杂数据结构和优化问题时。UVA(University of Virginia)在线判题系统提供了丰富的算法题目供程序员挑战...
"Algorithm-UVA-Solutions-in-Python.zip"这个压缩包文件正是针对UVA竞赛中问题的Python 3解决方案集合。 Python作为一门易学且功能强大的编程语言,因其简洁的语法和丰富的库支持,成为了许多算法爱好者和开发者的...
《UVA532 Dungeon Master:解密游戏编程的深度探索》 在计算机科学与编程领域,UVA(University of Virginia)在线判题系统是一个深受程序员喜爱的平台,它提供了丰富的算法题目供学习者挑战。其中,编号为532的...
"tpcw-nyu-uva-client 客户端"是一个专为TPCW(Transaction Processing Performance Council Workloads)设计的应用程序,由纽约大学(NYU)和弗吉尼亚大学(UVA)共同开发。这个客户端软件主要用于模拟和评估数据库...