紫书: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. E20180121

    signature  n. 签名; 署名; 识别标志,鲜明特征; [医] 药的用法说明;

  2. bzoj1880: [Sdoi2009]Elaxia的路线(spfa,拓扑排序最长路)

    1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1944  Solved: 759[Submit][St ...

  3. Quartz定时调度jar包的执行Demo分享

    1.Quartz简介 ​ Quartz框架的核心是调度器.调度器负责管理Quartz应用运行时环境.调度器不是靠自己做所有的工作,而是依赖框架内一些非常重要的部件.Quartz不仅仅是线程和线程管理. ...

  4. C - GCD LCM

    Description The GCD of two positive integers is the largest integer that divides both the integers w ...

  5. ViewPager(3)用viewpager实现tabhost

    1.示例 2.代码 2.1 TabViewPagerMain.java import android.graphics.drawable.Drawable; import android.os.Bun ...

  6. 301 Remove Invalid Parentheses 删除无效的括号

    删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...

  7. Storm编程入门API系列之Storm的定时任务实现

    概念,见博客 Storm概念学习系列之storm的定时任务 Storm的定时任务,分为两种实现方式,都是可以达到目的的. 我这里,分为StormTopologyTimer1.java   和  Sto ...

  8. python2 'str' object has no attribute 'decode'

    '.decode('hex') 上述代码,报错: 'str' object has no attribute 'decode' 查找原因: https://stackoverflow.com/ques ...

  9. JProfiler 9.1.1部署及使用

    软件准备: 官网下载地址:http://www.ej-technologies.com/download/jprofiler/files 软件部署: windows安装双击即可. 注册号: L-Lar ...

  10. 微信服务号获取openId流程(订阅号)

    微信公众平台官网:https://mp.weixin.qq.com/ 微信测试开发平台官网:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandb ...