描述

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. 使用jQuery获取Bootstrap Switch的值

    $('#switcher').bootstrapSwitch('state'); // true || false $('#switcher').bootstrapSwitch('toggleStat ...

  2. Bootstrap--全局css样式之图片

    好久没有更新博客了,在这里跟大家分享一下生活的小乐趣,作为程序员,整天对着电脑是很不爽的,加班也是常有的,所以连续工作对身体是很不爽的,而且随着年龄的增加,程序员身体状况会越来越差,还是建议大家要常去 ...

  3. JDK Tools - xjc: 将 XML Schema 编译成 Java 类

    xjc 是 JAXB 将 xsd 生成 Java 类的工具. 命令格式 xjc [ options ] schema file/URL/dir/jar ... [-b bindinfo ] ... 命 ...

  4. 【JavaScript权威指南(第五版)】笔记之第一部分 核心javascript (第1章~第12章)

    第一章 javascript概述 ①.javascript是一种松散类型语言;也是一种解释型语言;         第二章 词法结构 ①.大小写敏感         第三章 数据类型和值 ①.isFi ...

  5. C# IO操作(二)File类和Directory类的常用方法

    本篇主要介绍一些常用的IO操作,对文件和目录的操作:留给自己复习之用. 1.创建文件 string sPath1=Path.GetDirectoryName(Assembly.GetExecuting ...

  6. .net远程连接oracle数据库不用安装oracle客户端的方法

    .net远程连接oracle数据库不用安装oracle客户端的方法步骤: 1.添加Sytem.Data.OracleClient命名空间. 2.连接时需要ConnectionString字符串,出现在 ...

  7. 使用FOR循环语句在屏幕上输出一个由星号组成的直角三角形

    题目要求: 请用C++的信息输出方式,使用循环语句在屏幕上输出一个由星号组成的直角三角形,形状如下: * ** *** **** ***** 要求: 完全使用C++的信息输出方式,即cout以及流插入 ...

  8. 九度OJ 1513 二进制中1的个数

    题目地址:http://ac.jobdu.com/problem.php?pid=1513 题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样 ...

  9. Headfirst设计模式的C++实现——状态模式(State)

    state.h #ifndef _STATE_H_ #define _STATE_H_ class GumballMachine; class State { public: ; ; ; ; Stat ...

  10. LeetCode FindMinimuminRotatedSorteArray &&FindMinimuminRotatedSorteArray2

    LeetCode上这两道题主要是使用二分搜索解决,是二分搜索算法的一个应用,根据条件每次舍弃一半,保留一半. 首先第一题: FindMinimuminRotatedSorteArray(时间复杂度为二 ...