描述

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

输入

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.

输出

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.

样例输入

(11,LL) (7,LLL) (8,R)

(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()

(3,L) (4,R) ()

样例输出

5 4 8 11 13 4 7 2 1

not complete

#include <iostream>
#include <queue>
using namespace std; typedef struct tNode{
int data;
struct tNode *lchild, *rchild;
tNode()
{
lchild = rchild = NULL;
data = -9999999;
}
}*Node;
bool flag;
class Tree{
private:
Node root;
public:
Tree()
{
root = new tNode();
}
void insert(int data, char path[]); //????????????
void print(int d); //??????
void destory(); //????????????
bool isBitTree(); //????????????????????????
}; void Tree::insert(int data, char path[])
{
int len = strlen(path);
int i;
Node cur = root;
for(i = 0; i < len - 1; i++)
{
if(path[i] == 'L')
{
if(cur->lchild == NULL)
cur->lchild = new tNode();
cur = cur->lchild;
}else if(path[i] == 'R')
{
if(cur->rchild == NULL)
cur->rchild = new tNode();
cur = cur->rchild;
}
}
if(cur->data == -9999999)
cur->data = data;
else
flag = false;
}
bool Tree::isBitTree()
{
queue<Node> q;
q.push(root);
while(!q.empty())
{
Node cur = q.front();
q.pop();
if(cur->data == -9999999)
return false;
if(cur->lchild)
q.push(cur->lchild);
if(cur->rchild)
q.push(cur->rchild);
}
return true;
}
void Tree::destory()
{
queue<Node> q;
q.push(root);
while(!q.empty())
{
Node cur = q.front();
q.pop();
if(cur->lchild)
q.push(cur->lchild);
if(cur->rchild)
q.push(cur->rchild);
delete cur;
}
root = new tNode();
}
void Tree::print(int d)
{
queue<Node> q;
q.push(root);
int w = 0;
while(!q.empty())
{
Node cur = q.front();
q.pop();
if(w == d - 1)
cout<<cur->data;
else
cout<<cur->data<<" ";
if(cur->lchild)
q.push(cur->lchild);
if(cur->rchild)
q.push(cur->rchild);
w++;
}
cout<<endl;
}
int main()
{
char snode[600];
int data, d = 0;
Tree t;
flag = true;
while(cin>>snode)
{
if(strcmp(snode, "()") == 0)
{
if(t.isBitTree() && flag)
t.print(d);
else
cout<<"not complete"<<endl;
t.destory();
d = 0;
flag = true;
continue;
}
sscanf(snode + 1, "%d", &data);
t.insert(data, strchr(snode, ',') + 1);
d++;
}
return 0;
}

  

1101-Trees on the Level的更多相关文章

  1. E - Trees on the level

     Trees on the level  Background Trees are fundamental in many branches of computer science. Current ...

  2. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  4. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

  5. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

  6. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  7. uva 122 trees on the level——yhx

    题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...

  8. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  9. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

  10. Trees on the level (二叉链表树)

    紫书:P150 uva122 Background Trees are fundamental in many branches of computer science. Current state- ...

随机推荐

  1. symonfy 项目根目录下没有 bin/console 文件的解决方法

    “Could not open input file: bin/console” Error comes when try to Run the Symfony Application 在采纳的答案中 ...

  2. (原创)openvswitch实验连载2-cisco模拟器IOU-Web安装及网络环境配置

    IOU的使用可以选择自己到官网https://docs.google.com/file/d/0B2AgRhS2cfxCQ01tN2NrQ0pvNUk/edit下载iou-web 软件,然后部署在lin ...

  3. 用Cookie记住上次登录用户名

    一个登录页面设置查看Cookie中的数据(Cookie不为null);然后将页面提交到另一个页面去,在这个页面中的到是否选择记住用户名,如果选择了, 就创建Cookie对象,将用户名存储起来,下次登录 ...

  4. 比较Activiti中三种不同的表单及其应用

    http://www.kafeitu.me/activiti/2012/08/05/diff-activiti-workflow-forms.html 开篇语 这个恐怕是初次接触工作流最多的话题之一了 ...

  5. Android寒假实训云笔记总结——欢迎页

    欢迎页使用的是viewpager,需要适配器. 注意点: 1.判断是否是第一次进入这个app. 2.欢迎页小圆点的逻辑. 实现原理: 首先在activity_welcome放入viewpager和固定 ...

  6. OPENSSL中RSA私钥文件(PEM格式)解析【一】

    http://blog.sina.com.cn/s/blog_4fcd1ea30100yh4s.html 在PKCS#1 RSA算法标准中定义RSA私钥语法为: RSAPrivateKey ::= S ...

  7. C++ DLL 获取 MSI Property

    VS2010 创建  C++, Win32 DLL工程C-TEST. Stdafx.h中,在<windows.h>之后 添加引用. #include <msi.h> #incl ...

  8. [翻译][MVC 5 + EF 6] 6:创建更复杂的数据模型

    原文:Creating a More Complex Data Model for an ASP.NET MVC Application 前面的教程中,我们使用的是由三个实体组成的简单的数据模型.在本 ...

  9. linux系统制作简单流程

    制作嵌入式平台使用的Linux内 核, 方法和制作PC平台 的Linux内 核基本一致, 下面使用 对比的方式介绍如何制作用 于6410开发板的内 核. 1. 清除原有配置与中间文件x86: make ...

  10. 创建安全的ashx文件,ashx编译

    <%@ WebHandler Language="C#" Class="Handler2" %> using System; using Syste ...