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