Trees on the level (二叉链表树)
紫书: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 (二叉链表树)的更多相关文章
- C语言递归实现二叉树(二叉链表)的三种遍历和销毁操作(实验)
今天写的是二叉树操作的实验,这个实验有三个部分: ①建立二叉树,采用二叉链表结构 ②先序.中序.后续遍历二叉树,输出节点值 ③销毁二叉树 二叉树的节点结构定义 typedef struct BiTNo ...
- [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...
- 二叉树的二叉链表存储结构及C++实现
前言:存储二叉树的关键是如何表示结点之间的逻辑关系,也就是双亲和孩子之间的关系.在具体应用中,可能要求从任一结点能直接访问到它的孩子. 一.二叉链表 二叉树一般多采用二叉链表(binary linke ...
- 【转载】区间信息的维护与查询(一)——二叉索引树(Fenwick树、树状数组)
在网上找到一篇非常不错的树状数组的博客,拿来转载,原文地址. 树状数组 最新看了一下区间的查询与修改的知识,最主要看到的是树状数组(BIT),以前感觉好高大上的东西,其实也不过就这么简单而已. 我们有 ...
- 二叉索引树BIT
定义 二叉索引树,binary index tree,又名树状数组,或Fenwick Tree,因为本算法由Fenwick创造. 对于数组A,定义Query(i,j) = Ai +Ai ...
- C++实用数据结构:二叉索引树
看下面这个问题(动态连续和查询): 有一个数组A(长度为n),要求进行两种操作: add(i,x):让Ai增大x: query(a,b):询问Aa+Aa+1+...+Ab的和: 若进行模拟,则每次qu ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- C#实现二叉树--二叉链表结构
二叉树的简单介绍 关于二叉树的介绍请看这里 : 二叉树的简单介绍 http://www.cnblogs.com/JiYF/p/7048785.html 二叉链表存储结构: 二叉树的链式存储结构是指,用 ...
- NYOJ 116 士兵杀敌(二)(二叉索引树)
http://acm.nyist.net/JudgeOnline/problem.php?pid=116 题意: 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的 ...
随机推荐
- "git rm" 和 "rm" 的区别(转载)
转自:http://yang3wei.github.io/blog/2013/02/03/git-rm-he-rm-de-qu-bie/ 这是一个比较肤浅的问题,但对于 git 初学者来说,还是有必要 ...
- sublime text2 配置php本地环境时遇到的错误。
首先,将PHP加到电脑的环境变量中如图(D:\PHPEnv\PHP5是我PHP的安装目录): 第二步:添加编译系统配置 第三步:配置详情: { "cmd": ["php. ...
- Maven构建的生命周期
什么是构建生命周期 构建生命周期是一组阶段的序列(sequence of phases),每个阶段定义了目标被执行的顺序.这里的阶段是生命周期的一部分.举例说明,一个典型的 Maven 构建生命周期是 ...
- 下载tortoisegit
https://download.tortoisegit.org/tgit/ 藏经阁技术资料分享群二维码
- 转 MySQL数据库基础
http://lib.csdn.net/article/mysql/57883 1 数据库基础 一.数据库与数据库管理系统 1.数据库(DB):存放数据的仓库,从广义来说,数据不仅包括数字,还包括了文 ...
- JavaScript 把函数作为参数进行传值
JavaScript 响应式编程模式有点类似 WebForm 中的事件驱动模式(传相应的处理函数给委托,通过事件来触发来进行相关的处理),在 AngularJs 2.x 框架中,应用了 RxJS AP ...
- 392 Is Subsequence 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...
- kafka的topic命名技巧
不多说,直接上干货! 比如,我们给kafka的topic命名为user_r2p10 表示user这个topic的副本因子(r)是2,分区数(p)是10. 这样后期在写消费者代码的时候,根据top ...
- js jquery 获取服务器控件的三种方法
由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,google了下,总结有以下3种方法: 服务器控件代码:<asp:TextBox ID=" ...
- RabbitMQ三:Rabbit的安装
本章文章,摘自 园友 章为忠 的文章,查找了很多资料,他总结的最细,最全面,我就直接拿过来了 他的原文 http://www.cnblogs.com/zhangweizhong/p/5689209.h ...