• Trees on the level UVA


    就是树的层次遍历,BFS算法。要判断给出的数据能不能构成一颗正确的树(不能缺漏或重叠)。

    要写一个树的结构。可以有两种写法:

    ①:用纯指针,结构体里要有左右结点指针,结点存储的值,还有结点相关的必要信息。要注意使用后要释放结点的内存,用指针访问比数组更快。

    ②:用数组来表示,比较方便。将左右结点和值信息等等全都单独建一个数组。数组中某个序号代表某一结点 比如left[5]代表结点5的left子树的序号的值。不用管理内存。

    下边分别是用指针和数组的方法

      1 #include<iostream>
      2 #include<string>
      3 #include<queue>
      4 using namespace std;
      5 bool complete = true;
      6 
      7 
      8 struct node {
      9     int val; bool has_valuaed; node *left; node *right;
     10     node() :val(-1), has_valuaed(false), left(NULL), right(NULL) {}
     11 };
     12 node * newtree() { return new node(); }
     13 node *t = newtree();
     14 
     15 bool addnode(int v, string S)
     16 {
     17     node *p = t;
     18     while (S.size())
     19     {
     20         if (S[0] == 'L')
     21         {
     22             if (p->left == NULL) p->left = new node();
     23             p = p->left;
     24         }
     25         if (S[0] == 'R')
     26         {
     27             if (p->right == NULL) p->right = new node();
     28             p = p->right;
     29         }
     30         if (S[0] == ')')
     31         {
     32             if (p->val != -1) { complete = false; return false; }
     33             p->val = v; p->has_valuaed = true;
     34         }
     35         S.erase(S.begin());
     36     }
     37     return true;
     38 }
     39 void remove_tree(node* t)
     40 {
     41     if (t == NULL) return;
     42     remove_tree(t->left);
     43     remove_tree(t->right);
     44     delete t;
     45 }
     46 
     47 
     48 void bfs(queue<int>&Q)
     49 {
     50     queue<node*> output;
     51     output.push(t);
     52     while (!output.empty())
     53     {
     54         node *f = output.front();
     55         if (f->val < 0) {
     56             complete = false; break;
     57         }
     58         else Q.push(f->val);
     59         if (f->left != NULL) output.push(f->left);
     60         if (f->right != NULL) output.push(f->right);
     61         output.pop();
     62     }
     63 }
     64 
     65 
     66 int main()
     67 {
     68     string S;
     69     while (cin >> S)
     70     {
     71 
     72         if (S == "()")
     73         {
     74             queue<int> outputval;
     75             bfs(outputval);
     76             //输出
     77             if (complete) 
     78             {
     79                 while(outputval.size())
     80                 {
     81                     cout << outputval.front();
     82                     if (outputval.size() != 1)cout << " ";
     83                     outputval.pop();
     84                 }
     85                 cout << endl;
     86             }
     87             else cout << "not complete" << endl;
     88             //初始化
     89             remove_tree(t);
     90             t = newtree(); complete = true;
     91         }
     92         else
     93         {//输入
     94             string::size_type idx;
     95             string Temp = S.substr(1, S.find(',') - 1);
     96             int v = stoi(Temp, &idx);
     97             S = S.substr(S.find(',') + 1, S.size() - 1);
     98             addnode(v, S);
     99         }
    100     }
    101     return 0;
    102 }
     1 #include<iostream>
     2 #include<string>
     3 #include<queue>
     4 #define maxl 250
     5 #define mms(a,b) memset(a,b,sizeof(a))
     6 using namespace std;
     7 bool complete = true;
     8 
     9 int left_[maxl], right_[maxl], val[maxl], have_value[maxl], cnt = 0;
    10 
    11 void newtree() { val[0]=have_value[0] = right_[0] = left_[0] = 0; }
    12 int newnode() { 
    13     int n = ++cnt; val[n]=have_value[n] = right_[n] = left_[n] = 0;
    14     return n; 
    15 }
    16 bool addnode(int v, string S)
    17 {
    18     int p = 0;
    19     while (S.size())
    20     {
    21         if (S[0] == 'L')
    22         {
    23             if (left_[p] <1) left_[p] = newnode();
    24             p = left_[p];
    25         }
    26         if (S[0] == 'R')
    27         {
    28             if (right_[p] <1) right_[p] = newnode();
    29             p = right_[p];
    30         }
    31         if (S[0] == ')')
    32         {
    33             if (have_value[p] == 1) { complete = false; return false; }
    34             val[p] = v; have_value[p]++;
    35         }
    36         S.erase(S.begin());
    37     }
    38     return true;
    39 }
    40 
    41 void bfs(queue<int>&Q)
    42 {
    43     queue<int> output;
    44     output.push(0);
    45     while (!output.empty())
    46     {
    47         int f = output.front();
    48         if (have_value[f]!=1) {
    49             complete = false; break;
    50         }
    51         else Q.push(val[f]);
    52         if (left_[f] > 0) output.push(left_[f]);
    53         if (right_[f] > 0) output.push(right_[f]);
    54         output.pop();
    55     }
    56 }
    57 
    58 void remove_tree()
    59 {
    60     mms(left_, -1); mms(right_, -1); mms(have_value, -1); mms(val, -1);
    61 }
    62 
    63 int main()
    64 {
    65     string S; remove_tree(); newtree();
    66     while (cin >> S)
    67     {
    68         if (S == "()")
    69         {
    70             queue<int> outputval;
    71             bfs(outputval);
    72             //输出
    73             if (complete)
    74             {
    75                 while (outputval.size())
    76                 {
    77                     cout << outputval.front();
    78                     if (outputval.size() != 1)cout << " ";
    79                     outputval.pop();
    80                 }
    81                 cout << endl;
    82             }
    83             else cout << "not complete" << endl;
    84             //初始化
    85             remove_tree();
    86             newtree(); complete = true;
    87         }
    88         else
    89         {//输入
    90             string::size_type idx;
    91             string Temp = S.substr(1, S.find(',') - 1);
    92             int v = stoi(Temp, &idx);
    93             S = S.substr(S.find(',') + 1, S.size() - 1);
    94             addnode(v, S);
    95         }
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    python Asyncore.dispatcher 理解
    COCOA® PROGRAMMING FOR MAC® OS X (2)- Speak Line
    COCOA® PROGRAMMING FOR MAC® OS X (1)- Get Start
    Objetive-C initialize研究
    Objetive-C +load方法研究
    Python创建多进程,用Queue传递信息
    Python 中的GIL
    IOS CrackMe 破解学习
    越狱手机发送短信
    Vue 子组件向父组件传参
  • 原文地址:https://www.cnblogs.com/worldcreator-zh/p/10539320.html
Copyright © 2020-2023  润新知