POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树
Figure 1
Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and go right to The problem.
A binary tree of letters may be one of two things:
- It may be empty.
- It may have a root node. A node has a letter as data and refers to a left and a right subtree. The left and right subtrees are also binary trees of letters.
In the graphical representation of a binary tree of letters:
- Empty trees are omitted completely.
- Each node is indicated by
- Its letter data,
- A line segment down to the left to the left subtree, if the left subtree is nonempty,
- A line segment down to the right to the right subtree, if the right subtree is nonempty.
A leaf in a binary tree is a node whose subtrees are both empty. In the example in Figure 1, this would be the five nodes with data B, D, H, P, and Y.
The preorder traversal of a tree of letters satisfies the defining properties:
- If the tree is empty, then the preorder traversal is empty.
- If the tree is not empty, then the preorder traversal consists of the following, in order
- The data from the root node,
- The preorder traversal of the root's left subtree,
- The preorder traversal of the root's right subtree.
The preorder traversal of the tree in Figure 1 is KGCBDHQMPY.
A tree like the one in Figure 1 is also a binary search tree of letters. A binary search tree of letters is a binary tree of letters in which each node satisfies:
The root's data comes later in the alphabet than all the data in the nodes in the left subtree.
The root's data comes earlier in the alphabet than all the data in the nodes in the right subtree.
The problem:
Consider the following sequence of operations on a binary search tree of letters
Remove the leaves and list the data removed
Repeat this procedure until the tree is empty
Starting from the tree below on the left, we produce the sequence of trees shown, and then the empty tree
by removing the leaves with data
BDHPY
CM
GQ
K
Your problem is to start with such a sequence of lines of leaves from a binary search tree of letters and output the preorder traversal of the tree.
Input
The lines contain the leaves removed from a binary search tree in the stages described above. The letters on a line will be listed in increasing alphabetical order. Data sets are separated by a line containing only an asterisk ('*').
The last data set is followed by a line containing only a dollar sign ('$'). There are no blanks or empty lines in the input.
Output
Sample Input
BDHPY
CM
GQ
K
*
AC
B
$
Sample Output
KGCBDHQMPY
BAC
解题思路:
本题给出多组字符串每组以*为结尾以$为结束条件要求输出每一组数据所建立的二叉搜索树的先序遍历
如例中输入:
BDHPY
CM 插入顺序为 -> K -> K -> K
GQ G Q G Q G Q
K C M C H M Y
* B D P
其先序遍历为:KGCBDHQMPY。
#include <cstdio>
#include <cstring>
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
// bits/stdc++.h会编译错误
using namespace std;
typedef char typeData;
struct node{
typeData data;
node *leftChild;
node *rightChild;
node(){
leftChild = NULL;
rightChild = NULL;
}
};
vector<typeData> data;
string temp, str;
void insertBST(node *&root, typeData x){ //二叉搜索树插入函数
//注意函数要进行插入操作,根结点要传引用
if(root == NULL){ //找到空位置即使插入位置
root = new node(); //新建结点权值为x
root->data = x;
return;
}
if(x == root->data){ //要插入结点已存在直接返回
return;
}else if(root->data > x){ //x比根结点数据域小 需要插在左子树
insertBST(root->leftChild, x); //往左子树搜索
}else if(root->data < x){ //x比根结点数据域大 需要插在右子树
insertBST(root->rightChild, x); //往右子树搜索
}
}
node *createBST(){ //建树
node *root = NULL;
for(string::iterator it = str.begin(); it != str.end(); it++){
insertBST(root, *it); //以str为数据组建树
}
return root; //返回根结点
}
void preorder(node *root){
if(root == NULL)//到达空树为递归边界
return;
printf("%c", root->data); //访问根结点输出权值
preorder(root->leftChild); //访问左子树
preorder(root->rightChild); //访问右子树
}
int main()
{
while(cin >> temp){ //输入字符串
if(temp != "*" && temp != "$"){
//如果输入的字符串不是组结束符或结尾符
str += temp; //将出入的字符串加到str字符串尾部
continue;
}
//如果输入的时组结束符或结尾符
reverse(str.begin(),str.end()); //将str数组反转
node *root = createBST(); //建树
str = ""; //将str清空
preorder(root); //输出前序遍历
printf("\n");
if(temp == "$")//如果是结尾符跳出循环
break;
}
return ;
}
POJ 1577 Falling Leaves 二叉搜索树的更多相关文章
- 【二叉搜索树】poj 1577 Falling Leaves
http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字 ...
- POJ 1577 Falling Leaves(二叉搜索树)
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个 二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
- POJ 1577 Falling Leaves (子母二叉树,给出叶子节点的删除序列,求前序遍历)
题意:给出一棵字母二叉树删除叶子节点的序列,按删除的顺序排列.让你输出该棵二叉树额前序遍历的序列.思路:先把一棵树的所有删除的叶子节点序列存储下来,然后从最后一行字符串开始建树即可,最后遍历输出. ...
- POJ 1577 Falling Leaves
题意:给出一些字符串,从上到下的建树,输出其前序遍历 像前面那一题一样,先建树,然后再递归前序遍历 不过想像上一题那样用数组建树,建树和上题一样的办法,可是应该怎么输出前序遍历呢= = 还是看的题解= ...
- POJ 2309 BST(二叉搜索树)
思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...
- 二叉搜索树 POJ 2418 Hardwood Species
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
随机推荐
- gearman服务连接php java
在实际工作中,会碰到两个问题 (1)现有系统想集成一个开发组件,而该组件的SDK却没有现有语言版本. (2)系统中的一项功能非常耗费资源,最好能利用其它机器来处理. 本文介绍gearman的使用,实现 ...
- 利用InfoPath实现SharePoint Server 2013列表的级联选择(Cascading Drop Down List)
最近在利用SharePoint Server 2013的列表组织和存储数据,发现SharePoint列表原始不支持级联选择的功能. 谷歌百度一通以后,发现了很多通过代码实现的方案,利用第三方的插件sp ...
- Solr中的一些查询参数
fl: 是逗号分隔的列表,用来指定文档结果中应返回的 Field 集.默认为 “*”,指所有的字段. defType: 指定query parser,常用defType=lucene, defType ...
- yum反查某个命令或so库在哪个包里面
yum whatprovides "*/XXX.so.1"
- python, C++, C# 计算速度简单对比
有个简单的运算, ; ; ; i < n ; i ++) { ; j < n; j ++) { lResult += (ulong) ( i * j ); } } return lResu ...
- 如何将JPG格式的图片转化为带地理坐标的TIFF格式
最近有个项目需要用到开源软件GeoServer,数据源是一张高分辨率的2.5维图片,格式是jpg的,由于GeoServer不支持jpg格式的发布,因此考虑到要进行格式转换,将其转换成tiff格式. 1 ...
- WPF 无边框拖动
无边框之后的拖动方法有三种. 我个人是喜欢第一和第三的方法,看个人去需求. 第三种代码比较仓促,有需要者可以立马用,或者稍作整理修改. 对于WIN10 .NET 4.5以上的框架可以使用 WIndow ...
- Learning Rich Features from RGB-D Images for Object Detection and Segmentation论文笔记
相关工作: 将R-CNN推广到RGB-D图像,引入一种新的编码方式来捕获图像中像素的地心姿态,并且这种新的编码方式比单纯使用深度通道有了明显的改进. 我们建议在每个像素上用三个通道编码深度图像:水平视 ...
- 【ocp-12c】最新Oracle OCP-071考试题库(42题)
42.(9-1)choose the best answer: Which statement is true about the Oracle SQL, DELETE and TRUNCATE st ...
- Linux 服务器加入Windows AD
背景信息: Windows AD Version: Windows Server 2012 R2 zh-cn 计算机全名:hlm12r2n1.hlm.com 域:hlm.com 域控管理员:stone ...