uva 122 - Trees on the level

来源:互联网 发布:oracle数据备份与恢复 编辑:程序博客网 时间:2024/05/21 06:38

                                                              Trees on the level

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=58

             Time limit: 3.000 seconds

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 onfat 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 levelk are printed before all nodes at levelk+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) wheren is the value at the node whose path from the root is given by the strings. A path is given be a sequence ofL'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 becompletely 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 1not complete
//代码
#include <iostream>#include<cstring>#include<cstdlib>#include<queue>#include<cstdio>using namespace std;struct node{    bool hv=false;    int v;    node *left=NULL;    node *right=NULL;};char s[50];node *root;bool com;     //记录输入是否正确void add(int v,char *s)        //建树   {    int l=strlen(s);    node *u=root;    for(int i=0;i<l-1;++i)    {        if(s[i]=='L')        {            if(!u->left)            {                u->left=new node;            }            u=u->left;        }        else        {             if(!u->right)            {                u->right=new node;            }            u=u->right;        }    }        if(u->hv)        {           // cout<<"重值"<<endl;            com=false;        //有重复节点,输入错误            return;        }        else        {            u->v=v;            u->hv=true;        }}void bfs(queue <int> &ans)    //遍历树{    queue <node*> q;    while(!ans.empty())    {        ans.pop();            //清空队列    }    while(!q.empty())    {        q.pop();    }    q.push(root);    while(!q.empty())    {        node *u=q.front();        q.pop();        if(!u->hv)        {            //cout<<"缺值"<<endl;            com=false;     //缺少节点   输入错误            return;        }        else        {            ans.push(u->v);        }        if(u->left)        q.push(u->left);        if(u->right)        q.push(u->right);    }}bool input()    //读入数据{    root= new node;    com=true;    while(true)    {        if(scanf("%s", s)!=1)            return false;    //读到EOF  整个输入结束        if(!strcmp(s,"()"))                return true;    //读到()本次测试结束        int v;        sscanf(&s[1], "%d", &v);  //将字符串中的数字转化存入v中          add(v,strchr(s,',')+1);   //传’,‘后面的字符串    }    return true;}int main(){    //cout << "Hello world!" << endl;    while(input())    {        if(!com)   //重值            printf("%s\n","not complete");        else            {                queue <int> ans;                bfs(ans);                if(!com)   //缺值                    printf("%s\n","not complete");                else                {                    while(!ans.empty())                    {                        cout<<ans.front();                        ans.pop();                        if(!ans.empty())                            cout<<' ';      //最后一个空格不输出                    }                    cout<<endl;                }            }    }    return 0;}


0 0