【可运行,刘汝佳代码】Trees on the level UVA - 122
立志用最少的代碼做最高效的表達
Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateof-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 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 on the right 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
分析
輸入數據,將數字和字符篩選出來,字符根據函數生成子樹,同時將數字賦給該節點。
在建樹的時候設置一個bool類型,置0,遍歷到就置1。
兩種錯誤類型:
1、若遍歷到1,就說明已經遍歷過了,表明輸入了重復的根節點。
2、最后輸出時判斷,若有節點但沒被賦值過,表明輸入了重復的非根節點。
一道很好的層序遍歷練習題
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std;const int maxn = 256+5; char s[maxn]; //保存讀入的節點 bool failed;//二叉樹的節點定義和操作。定義一個Node結構體,對應整棵二叉樹根root //如果要定義一顆二叉樹,一般是定義struct,然后保存樹根的指針(Node* root) /* 每次需要一個新的Node時,都要用new運算符申請內存,并執行構造函數 下面把申請新節點的操作封裝到newnode中 */ struct Node {bool have_value; //是否被賦值int v; //節點值Node *left, *right; Node() : have_value(false), left(NULL), right(NULL) {} }; Node* root; //以后的遍歷操作都是基于這個根 //可以使用new運算符申請空間并執行構造函數,如果返回值為null,說明 //空間不足,申請失敗 Node* newnode() { //申請新節點 return new Node(); } /* 接下來是在read_input中調用的addnode函數。它按照移動序列行走,目標 不存在時調用newnode創建新節點 */ 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 = true; //如果已經賦過值,則表明輸入有誤u->v = v;u->have_value = true; }//以上,輸入、建樹結束,接下來BFS層序遍歷 bool bfs(vector<int>& ans) { //ans存儲層序遍歷的節點值 queue<Node*>q;ans.clear();q.push(root);while(!q.empty()) {Node* u = q.front(); q.pop();if(!u->have_value) return false; //有節點沒有被賦值過,表明輸入有誤ans.push_back(u->v);if(u->left != NULL) q.push(u->left); //把左子節點放進隊列if(u->right != NULL) q.push(u->right); } return true; //輸入正確 }/* */ bool read_input() {failed = false;root = newnode(); //創建根節點 //C語言的靈活性,可以靈活選取字符串的值作為“首地址”傳輸 for(;;) {if(scanf("%s", s) != 1) return false; //輸入結束if(!strcmp(s, "()")) break; //讀到結束標志,退出循環int v;sscanf(&s[1], "%d", &v); //讀入節點值(從1開始查找)addnode(v, strchr(s, ',')+1); //查找逗號,然后插入節點 } return true; } /* void remove_tree(Node* u) {if(u == NULL) return; //提前判斷比較穩妥 remove_tree(u->left); //遞歸釋放左子樹的空間 remove_tree(u->right); //遞歸釋放右子樹的空間 delete u; //調用u的析構函數并釋放u節點本身的內存 } */int main() {while(read_input()) {if(failed == true) { printf("not complete\n");continue;} vector<int>v;if(bfs(v)) {for(int i = 0; i < v.size(); i++) {printf("%d%s", v[i], i==v.size()-1?"\n":" ");}} else {printf("not complete\n");}}return 0; }
總結
以上是生活随笔為你收集整理的【可运行,刘汝佳代码】Trees on the level UVA - 122的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于华为P40登录谷歌闪退的问题
- 下一篇: 【已解决】TypeError: bind