zoukankan      html  css  js  c++  java
  • E

     Trees on the level 

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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <stack>
    12 using namespace std;
    13 const int INF = 0x7fffffff;
    14 const double EXP = 1e-8;
    15 const int MS = 260;
    16 struct node
    17 {
    18     string value;
    19     string path;
    20     //node(string v = "", string pa = "") :value(va), path(pa){}
    21     bool operator < (const node &b)
    22     {
    23         if (path.length() != b.path.length())
    24             return path.length() < b.path.length();
    25         return path < b.path;
    26     }
    27 }nodes[MS];
    28 map<string, int> m1, m2;
    29 int get_comma(string &s)
    30 {
    31     int len = s.length();
    32     for (int i = 0; i < len; i++)
    33         if (s[i] == ',')
    34             return i;
    35 }
    36 
    37 int main(int argc, char *argv[])
    38 {
    39     string str;
    40     bool flag = true;
    41     int cnt = 0;
    42     ios_base::sync_with_stdio(false);
    43     while (cin >> str)
    44     {
    45         if (str != "()")
    46         {
    47             int i = get_comma(str);
    48             nodes[cnt].value = str.substr(1, i - 1);
    49             nodes[cnt].path = str.substr(i + 1, str.length() - i - 2);
    50             if (m1[nodes[cnt].path])   //m1.count(nodes[cnt].path)!=0
    51                 flag = false;
    52             else
    53                 m1[nodes[cnt].path] = 1;
    54             cnt++;
    55         }
    56         else
    57         {
    58             if (flag)
    59             {
    60                 sort(nodes, nodes + cnt);
    61                 if (nodes[0].path.length() == 0)   //可能没有根节点
    62                 {
    63                     m2[nodes[0].path] = 1;
    64                     for (int i = 1; i < cnt&&flag; i++)
    65                     {
    66                         if (m2[nodes[i].path.substr(0, nodes[i].path.length() - 1)] == 0)
    67                             flag = false;
    68                         else
    69                             m2[nodes[i].path] = 1;
    70                     }
    71                 }
    72                 else
    73                     flag = false;
    74             }
    75             if (flag)
    76                 for (int i = 0; i < cnt; i++)
    77                 {
    78                     if (i)
    79                         cout << " ";
    80                     cout << nodes[i].value;
    81                 }
    82             else
    83                 cout << "not complete";
    84             cout << endl;
    85             m1.clear();
    86             m2.clear();
    87             cnt = 0;
    88             flag = true;
    89         }
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    Java的栈和队列
    Spring @Scheduled 在tomcat容器里面执行两次
    Java calendar获取月份注意事项
    mysql 查询今天,昨天,上个月sql语句 注解
    MySQL 查询最近几天的记录 最近7天的记录 本周内的记录
    关于mybatis 注解sql sum(参数)传参写法
    tomcat 部署war项目
    maven项目生成war包
    Cron表达式
    ### 获取当前日期的函数
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4273163.html
Copyright © 2011-2022 走看看