zoukankan      html  css  js  c++  java
  • Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 584    Accepted Submission(s): 195

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622

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

     
    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
     
    Source

     题意: 输入一棵二叉树,按照从上到下从左到右的顺序输出各个节点的值,每个节点都是按照从根节点到他的移动的序列给出的(L表示左,R表示右)在输入中,每个编号左括号和右括号之间没有空格,相邻的节点之间用一个空格隔开,每棵树的输入用括号()表示结束,这个括号本身不代表一个节点,注意,当根到某个叶节点的路径上的节点没有在输入中给出,或者给出超过一次,输出not complete 节点个数不超过256

    题解:学习紫书上的代码,给出完整的用指针和用数组的方法的代码

    一、用指针建树:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<string>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 using namespace std;
     9 #define maxn 260
    10 char s[maxn];
    11 struct Node{
    12   bool have_value;//是否被赋值过
    13   int v;
    14   Node *left,*right;
    15   Node():have_value(false),left(NULL),right(NULL){}//构造函数
    16 };
    17 Node *root;
    18 
    19 Node* newnode(){
    20   return new Node();
    21 }
    22 
    23 bool failed;
    24 
    25 void remove_tree(Node *u){//防止内存泄漏
    26   if(u == NULL) return;
    27   remove_tree(u->left);
    28   remove_tree(u->right);
    29   delete(u);
    30 }
    31 
    32 void addnode(int v, char* s){
    33   int n = strlen(s);
    34   Node* u = root;
    35   for(int i = 0; i < n; i++){
    36     if(s[i]=='L'){
    37       if(u->left == NULL) u->left = newnode();
    38       u = u->left;
    39     }else if(s[i] == 'R'){
    40       if(u->right == NULL) u->right = newnode();
    41       u = u->right;
    42     }
    43   }
    44   if(u->have_value) failed = true;//已经赋值过的认为是错误的输入
    45   u->v = v;
    46   u->have_value = true;//记得做标记
    47 }
    48 
    49 bool read_input(){
    50   failed = false;
    51   remove_tree(root);//释放掉之前建立过得树
    52   root = newnode();
    53   for(;;){
    54     if(scanf("%s",s)!=1) return false;//整个输入结束
    55     if(!strcmp(s,"()")) break;//读到结束标志
    56     int v;
    57     sscanf(&s[1],"%d",&v);//读入节点值
    58     addnode(v,strchr(s,',')+1);//查找逗号,然后插入节点
    59   }
    60   return true;
    61 }
    62 
    63 vector< int > ans;
    64 
    65 bool bfs(vector<int> & ans){
    66   queue<Node*> q;
    67   ans.clear();
    68 
    69   q.push(root);
    70   while(!q.empty()){
    71     Node* u = q.front(); q.pop();
    72     if(!u->have_value) return false;
    73     ans.push_back(u->v);
    74     if(u->left != NULL) q.push(u->left);
    75     if(u->right != NULL) q.push(u->right);
    76   }
    77   return true;
    78 }
    79 
    80 int main()
    81 {
    82   while(read_input()==true){
    83     if(!failed&&bfs(ans)){
    84     vector<int>::iterator it;
    85     for(it = ans.begin(); it != ans.end()-1; it++)
    86     {
    87       printf("%d ", (*it));
    88     }
    89     printf("%d
    ",(*it));
    90   }
    91   else puts("not complete");
    92   }
    93   return 0;
    94 }

    二、用数组代码:

     注意,就算是用数组也要先建立路径中的节点

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<string>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 #define N 260
     9 const int root = 1;
    10 
    11 char s[N];
    12 
    13 int left[N];
    14 int right[N];
    15 bool have_value[N];
    16 int val[N];
    17 
    18 int cnt;
    19 void newtree(){ left[root] = right[root] = 0; have_value[root] = false; cnt = root; }
    20 int newnode(){ int u = ++cnt; left[u] = right[u] = 0; have_value[u] = false; return u; }
    21 
    22 bool failed;
    23 
    24 void addnode(int v, char* s){
    25   int n = strlen(s);
    26  // int u = newnode();
    27   int tm = root;
    28   for(int i = 0; i < n; i++){
    29     if(s[i]=='L'){
    30       if(left[tm]==0) left[tm] = newnode();
    31       tm = left[tm];
    32     }else if(s[i] == 'R'){
    33       if(right[tm]==0) right[tm] = newnode();
    34       tm = right[tm];
    35     }
    36   }
    37   if(have_value[tm]) failed = true;//已经赋值过的认为是错误的输入
    38   val[tm] = v;
    39   have_value[tm] = true;//记得做标记
    40 }
    41 
    42 bool read_input(){
    43   failed = false;
    44   newtree();
    45   for(;;){
    46     if(scanf("%s",s)!=1) return false;//整个输入结束
    47     if(!strcmp(s,"()")) break;//读到结束标志
    48     int v;
    49     sscanf(&s[1],"%d",&v);//读入节点值
    50     addnode(v,strchr(s,',')+1);//查找逗号,然后插入节点
    51   }
    52   return true;
    53 }
    54 
    55 std::vector< int > ans;
    56 
    57 bool bfs(std::vector<int> & ans){
    58   std::queue<int> q;
    59   ans.clear();
    60 
    61   q.push(root);
    62   while(!q.empty()){
    63     int u = q.front(); q.pop();
    64     if(!have_value[u]) return false;
    65     ans.push_back(val[u]);
    66     if(left[u] != 0) q.push(left[u]);
    67     if(right[u] != 0) q.push(right[u]);
    68   }
    69   return true;
    70 }
    71 
    72 using namespace std;
    73 int main()
    74 {
    75   while(read_input()==true){
    76     if(!failed&&bfs(ans)){
    77     vector<int>::iterator it;
    78     for(it = ans.begin(); it != ans.end()-1; it++)
    79     {
    80       printf("%d ", (*it));
    81     }
    82     printf("%d
    ",(*it));
    83   }
    84   else puts("not complete");
    85   }
    86   return 0;
    87 }
  • 相关阅读:
    UIActivityIndicatorView的使用
    几道面试题-考察JS的运用
    第六章-面向对象的程序设计(理解对象)
    webpack学习之——npm的安装依赖情况
    面向对象的编程(五)
    Javascript面向对象编程(四):非构造函数的继承
    面向对象的编程(三)—封装
    面向对象的编程(二)构造函数的继承
    面向对象的编程—封装
    第五章——引用类型
  • 原文地址:https://www.cnblogs.com/shanyr/p/5202982.html
Copyright © 2011-2022 走看看