博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 122 - Trees on the level
阅读量:5095 次
发布时间:2019-06-13

本文共 6141 字,大约阅读时间需要 20 分钟。

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

picture28

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 1not complete

Solution1:

#define RUN#ifdef RUN#include
#include
#include
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
#include
#include
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

转载于:https://www.cnblogs.com/AlgoWing/archive/2013/03/05/3189612.html

你可能感兴趣的文章
Coding配合git使用时遇到的问题
查看>>
java 字体+颜色综合使用示例
查看>>
ios开发之级联菜单(两个tableView实现)
查看>>
tft屏图像文字一起显示
查看>>
java 泛型
查看>>
第六章函数和宏定义
查看>>
杨森翔的书法-19
查看>>
css画圆
查看>>
视图时时更新
查看>>
【PMP】财务测量指标ROI、PBP、NPV、IRR、BCR
查看>>
【Shell】Read命令
查看>>
BZOJ1061 NOI2008 志愿者招募 单纯形
查看>>
code forces 996D Suit and Tie
查看>>
PHP操作Mysql
查看>>
win7 64安装msyql
查看>>
vue-cli的webpack使用说明
查看>>
机试指南第二章-经典入门-排序模板
查看>>
验证数字的正则表达式
查看>>
Android的ListView分页功能
查看>>
LeetCode 69.sqrt(x)
查看>>