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 ...
随机推荐
- 利用InfoPath实现SharePoint Server 2013列表的级联选择(Cascading Drop Down List)
最近在利用SharePoint Server 2013的列表组织和存储数据,发现SharePoint列表原始不支持级联选择的功能. 谷歌百度一通以后,发现了很多通过代码实现的方案,利用第三方的插件sp ...
- c# 调用微信小程序
//微信也不给个c#调用的例子 只好自己造咯:ps:大佬勿喷 1 public string GetWx(string code, string iv, string encryptedData) { ...
- 利用backgroundwork----递归读取网页源代码,并下载href链接中的文件
今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个 ...
- mysql--mysql的安装与目录介绍
一.mysql的下载安装 1.下载安装 1.windows10下安装 我们采用绿色免安装版, 打开你的mysql文件夹中的bin目录,我的是这个样子的 将这个路径添加入系统环境变量,首先右键此电脑-- ...
- JZOJ6096 森林
题目传送门 Description 我们定义对一棵树做一次变换的含义为:当以 1 号节点为根时,交换两个互相不为祖先的点的子树: 一棵树的权值为对它进行至多一次变换能得到的最大直径长度: 初始时 ...
- AngularJS源码解析2:注入器的详解
上一课,没有讲createInjector方法,只是讲了它的主要作用,这一课,详细来讲一下这个方法.此方法,最终返回的注册器实例对象有以下几个方法: invoke, instantiate, get, ...
- 安装openssl-devel
安装openssl-devel 0.操作系统为 RHEL6.7 1.描述:当开发人员需要调用openssl的库文件时,需要安装openssl-devel包 2.当根目录(即挂载点为 )的利用率为10 ...
- H5入门基础(一)
我们还是围绕这几个问题来学习: 1.什么是H5? 2.为什么要用H5? 3.怎么用H5? 1.什么是H5? ♦HTML是指超文本标记语言(Hyper Text Markup Language). ♦H ...
- Python的科学计算包matplotlib setup
回想起大学四年 专业一直使用matlab,然而我却没在PC上装成功过,以前懒于思考这种数学工具的作用,直到最近,大学同学研究生要毕业了,几经交流,和自己阅读了一些机器学习的教材之后,发觉科学计算包和画 ...
- Set的总结
Set最重要的操作是查找,为查找而设计.存入HashSet的元素必须定义hashCode(); Set不保存重复的元素,元素必须唯一.通过equals()方法一确保对象的唯一性. Set中最常被用于归 ...