紫书:P150 uva122

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

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 给出树的节点以及它的位置,最后要求按照从上至下,从左至右的顺序访问节点,很显然,最后按照bfs来遍历这棵二叉树就好,复杂的在于输入以及树的建立
关于输入:scanf(“%s”)可以排除空格的干扰,sscanf()可以很方便地取出其中的数字,因为它可以把字符串中的某个部分取出然后自动转化成你想要的类型
strcmp()则可以判断输入是否结束
关于树的建立:不要只会递归地先序地建立树,其实树的建立也可以很灵活,还有一个重点,对于树中的节点,有可能在其中做某个标记,那么在初始化分配内存需要附上一个初值
那么就要学会写构造函数
如:
struct node
{
int data;
int vis;
struct node*lchild;
struct node*rchild;
node():vis(0),lchild(NULL),rchild(NULL){}; //这就是构造函数,通过它可以直接定义struct node u=new node();在之后的建树过程减少了叶子节点
} //的左右孩子赋空带来的麻烦,同时也满足vis置为0的要求
//如果某个节点没有孩子节点时,一定要把后置指针置空!!!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef struct node
{
int data;
int vis;
struct node *lchild;
struct node *rchild;
node():vis(),lchild(NULL),rchild(NULL) {}
}*BItree,bt; bool faliure;
void AddNode(int v,char* s,BItree root)
{
int n=strlen(s);
BItree u=root;
for(int i=;i<n;i++)
{
if(s[i]=='L')
{
if(u->lchild==NULL)
u->lchild=new node();
u=u->lchild;
}
else if(s[i]=='R')
{
if(u->rchild==NULL)
u->rchild=new node();
u=u->rchild;
}
}
if(u->vis==) faliure=true;
u->vis=;
u->data=v;
} bool bfs(vector<int>& ans,BItree root)
{
queue<BItree> que;
ans.clear();
que.push(root);
while(!que.empty())
{
BItree u=que.front();
que.pop();
if(!u->vis) return false;
ans.push_back(u->data);
if(u->lchild!=NULL) que.push(u->lchild);
if(u->rchild!=NULL) que.push(u->rchild);
}
return true;
} char s[];
BItree root;
bool input()
{
faliure=false;
root=new node();
while()
{
if(scanf("%s",s)!=) return false;
if(!strcmp(s,"()")) break;
int v;
sscanf(&s[],"%d",&v);
AddNode(v,strchr(s,',')+,root);
}
return true;
} int main()
{
while(input())
{
vector<int>ans;
if(bfs(ans,root)&&!faliure)
{
for(int i=;i<ans.size();i++)
{
if(i) cout<<" ";
cout<<ans[i];
}
puts("");
}
else cout<<"not complete"<<endl;
}
return ;
}

Trees on the level (二叉链表树)的更多相关文章

  1. C语言递归实现二叉树(二叉链表)的三种遍历和销毁操作(实验)

    今天写的是二叉树操作的实验,这个实验有三个部分: ①建立二叉树,采用二叉链表结构 ②先序.中序.后续遍历二叉树,输出节点值 ③销毁二叉树 二叉树的节点结构定义 typedef struct BiTNo ...

  2. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

  3. 二叉树的二叉链表存储结构及C++实现

    前言:存储二叉树的关键是如何表示结点之间的逻辑关系,也就是双亲和孩子之间的关系.在具体应用中,可能要求从任一结点能直接访问到它的孩子. 一.二叉链表 二叉树一般多采用二叉链表(binary linke ...

  4. 【转载】区间信息的维护与查询(一)——二叉索引树(Fenwick树、树状数组)

    在网上找到一篇非常不错的树状数组的博客,拿来转载,原文地址. 树状数组 最新看了一下区间的查询与修改的知识,最主要看到的是树状数组(BIT),以前感觉好高大上的东西,其实也不过就这么简单而已. 我们有 ...

  5. 二叉索引树BIT

    定义     二叉索引树,binary index tree,又名树状数组,或Fenwick Tree,因为本算法由Fenwick创造.     对于数组A,定义Query(i,j) = Ai +Ai ...

  6. C++实用数据结构:二叉索引树

    看下面这个问题(动态连续和查询): 有一个数组A(长度为n),要求进行两种操作: add(i,x):让Ai增大x: query(a,b):询问Aa+Aa+1+...+Ab的和: 若进行模拟,则每次qu ...

  7. POJ 3321 Apple Tree dfs+二叉索引树

    题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...

  8. C#实现二叉树--二叉链表结构

    二叉树的简单介绍 关于二叉树的介绍请看这里 : 二叉树的简单介绍 http://www.cnblogs.com/JiYF/p/7048785.html 二叉链表存储结构: 二叉树的链式存储结构是指,用 ...

  9. NYOJ 116 士兵杀敌(二)(二叉索引树)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=116 题意: 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的 ...

随机推荐

  1. FreeMarker:模板开发指南

    ylbtech-FreeMarker:模板开发指南 1.返回顶部 1. Section Contents 入门 模板 + 数据模型 = 输出 数据模型一览 模板一览 数值,类型 基本内容 类型 模板 ...

  2. 35. extjs MessageBox里fn:是什么意思

    function的缩写,用来指定回调函数,就是你点击确定或取消按钮之类的按钮以后触发的事件Ext.Msg.show({ title:'自定义消息框', msg:'这是一个自定义消息框,想怎么搞就怎么搞 ...

  3. openssh常用命令记录

    command description date ssh [user@]hostname[:port] 登录远程机器 2017-03-21 scp <local_file> <use ...

  4. 利用XStream实现对象XML话

    使用Java原生的序列化的方式来表示一个对象.总结一下这种对象表示方式的优缺点: 1.纯粹的Java环境下这种方式可以很好地工作,因为它是Java自带的,也不需要第三方的Jar包的支持 2.多语言环境 ...

  5. Rails5 任务注释

     任务注释  格式  # TODO: ...  # FIXME: ...  # OPTIMIZE ...  查看   rails notes  个别查看  rails notes:todo  rail ...

  6. 10.23NOIP模拟题

    叉叉题目描述现在有一个字符串,每个字母出现的次数均为偶数.接下来我们把第一次出现的字母 a 和第二次出现的 a 连一条线,第三次出现的和四次出现的字母 a 连一条线,第五次出现的和六次出现的字母 a ...

  7. mysql百万数据分页查询速度

    百万数据测试 ,; 受影响的行: 时间: .080ms ,; 受影响的行: 时间: .291ms ,; 受影响的行: 时间: .557ms ,; 受影响的行: 时间: .821ms ,; 受影响的行: ...

  8. [转]c++中的string常用函数用法总结

    标准c++中string类函数介绍 注意不是CString之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为 ...

  9. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles

    There are nn rectangles on the plane. The problem is to find the area of the union of these rectangl ...

  10. Agar.io 简单但是有趣的网页游戏

    攻略,进阶 上榜第一次 (有点水,九百多分)  上榜第二次 (完成四杀,逆袭上榜) 上榜第三次 (忘写名字,自己补上) 上榜第四次 (人生巅峰!) 上榜第五次 (踩了狗屎运,上榜这么容易了?收了一个小 ...