描述

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. dede-搜索

    <form name="formsearch" action="{dede:global.cfg_cmsurl/}/plus/search.php"> ...

  2. placeholder

    html: <div style="position:relative;">   <input type="password" id=&quo ...

  3. 预算oracle

    select * from ( )) CODE_VERSION from ( SELECT tb_cube_fc05.pk_entity pk_org,/*主体pk*/ org_orgs.code o ...

  4. MVC下设置默认页为index.html

    将RouteConfig代码修改为如下 public class RouteConfig { public static void RegisterRoutes(RouteCollection rou ...

  5. nopCommerce添加支付插件

    之前完成了nopCommerce和汉化以及配置,今天继续对nopCommerce的研究,为了能够完成购物,我们就要将伟大的支付宝添加至其中了.支付宝插件下载 将Nop.Plugin.Payments. ...

  6. 帝国CMS附件大小限制

    做文件的上传下载,在我们本地测试总是顺利通过,一上传到服务器各种问题都来了. 帝国CMS,我们先看网站配制中附件大小限制. 附件存放目录设置: 单击菜单“系统”>“系统设置”>“系统参数设 ...

  7. Windows 键盘操作快捷方式积累

    复制.粘贴: CTRL+C 复制被选择的项目到剪贴板 CTRL+V 粘贴剪贴板中的内容到当前位置 CTRL+X 剪切被选择的项目到剪贴板 Alt+ space + E + P CMD 窗口快速粘贴 关 ...

  8. 工厂方法(Factory Pattern)

    工厂方法模式定义:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类.(注:“决定”不是指模式允许子类本身在运行时做决定,而是指在编写创建者类时,不需要知道实际 ...

  9. Integer ,==,int 的使用

    面试比较常见的题目:自己也经常忘记,所以就记下来了 上代码: Integer a = ,b=; Integer c = ,d=; System.out.println(a==b); System.ou ...

  10. 实习笔记-2:sql 分组不一定要group by

    今天在公司写代码的时候,遇到一个sql语句构建问题. 情形是这样的: 我需要获取不同小组下前N条记录. select top 10 * from dbo.Topic where GroupID in ...