紫书: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. 0627-TP整理三(对表的操作,数据的显示)

    一.对表的操作 直接sql语句:(query/execute) 1.查询: 查询所有:M('表名')->select(); 查询一条数据:M('表名')->find(); 条件查询: 动态 ...

  2. 微信小程序setData的使用,通过[...]进行动态key赋值

    首先先介绍一下微信小程序Page.prototype.setData(Object data, Function callback)的讲解: setData函数用于将数据从逻辑层发送到视图层(异步), ...

  3. ROS-USB摄像头

    前言:演示使用usb摄像头功能,推荐使用方法二. 首先要有一个usb摄像头,本次使用的是罗技(Logitech)摄像头. 一.使用软件库里的uvc-camera功能包 1.1 检查摄像头 lsusb ...

  4. 295 Find Median from Data Stream 数据流的中位数

    中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...

  5. 为什么,博主我要写下这一系列windows实用网络?

    发现,随着自身一路过来所学,无论在大数据领域.还是linux  or  windows里,菜鸟的我慢慢在长大.把自己比作一个园,面积虽在增加,涉及面增多,但圆外的东西,还是那么多. 现在,正值在校读研 ...

  6. mysql的简单优化【简单易学】

    1.选取最适用的字段属性: 表字段尽量设小,不要给数据库增加没必要的空间:如:值为'01'.'02',给char(2)即可: 2.使用连接(JOIN)来代替子查询(Sub-Queries): 使用jo ...

  7. [ SPOJ Qtree1 ] Query on a tree

    \(\\\) Description 给定 \(n\) 个点的树,边按输入顺序编号为\(1,2,...n-1\) . 现要求按顺序执行以下操作(共 \(m\) 次): \(CHANGE\ i\ t_i ...

  8. 2017-12-04HTML布局_div布局

    HTML布局_div布局 <!doctpye> <html> <head> <meta charset = 'utf-8'> <title> ...

  9. 计算机二级C语言冲刺笔记。

    2018-03-0618:32:26 风萧萧兮易水寒,壮士一去...... 四级依旧没过,计算机二级接踵而至, default语句在switch语句中可以省略,所以B错误:switch语句中并非每个c ...

  10. git Eclipse项目不显示当前分支

    问题: 在Eclipse中,导入新的git项目,在项目上不再显示当前所处的分支,也不再显示修改了哪些文件 解决: 右键选中项目  -->  Team  -->  Share Project ...