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 ...
随机推荐
- SQLServer中数据加密方法
对SQLServer中的数据进行加密,有三种方法, 1. 在程序语言中先对数据进行加密后再把加密后的数据保存在SQLServer数据库中: 2. 利用SQLServer未公开的加密密码函数,在SQ ...
- [python01] python列表,元组对比Erlang的区别总结
数据结构是通过某种方式组织在一起的数据元素的集合,这些数据元素可以是数字,字符,甚至可以是其他的数据结构. python最基本的数据结构是sequence(序列):6种内建的序列:列表,元组,字符串, ...
- Mathout In Action(中文)
http://download.csdn.net/detail/zxnm55/5593881
- VUE环境安装和创建项目
1.首先要安装nodejs和npm. 下载nodejs安装,下载地址:https://nodejs.org/en/ 安装很简单一路next即可. 安装完成后可以在cmd窗口输入node -v 和 np ...
- python网络编程--粘包解决方案 和 subprocess模块
1.缓冲区:作用:将程序和网络解耦分为输入缓冲区, 输出缓冲区 每个 socket 被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区.write()/send() 并不立即向网络中传输数据,而是先 ...
- 【OCP 12c】最新CUUG OCP-071考试题库(62题)
62.(13-17)choose the best answer: You need to list the employees in DEPARTMENT_ID 30 in a single row ...
- vue 路由传参
mode:路由的形式 用的哪种路由 1.hash 路由 会带#号的哈希值 默认是hash路由 2.history路由 不会带#的 单页面开发首屏加载慢怎么解决?单页面开发首屏加载白屏怎 ...
- luoguP5074 Eat the Trees
https://www.luogu.org/problemnew/show/P5074 插头 $ dp $ 入门题 如果你还不会插头 $ dp $ 请右转 洛谷插头dp题解 虽然是入门题但还是逃不过分 ...
- python 取出字典的键或者值/如何删除一个字典的键值对/如何遍历字典
先定义一个字典并直接进行初始化赋值 my_dict = dict(name="lowman", age=45, money=998, hourse=None) 1.取出该字典所有的 ...
- 3XX重定向
3XX响应结果表明浏览器需要执行某些特殊的处理以正确处理请求 301 Moved Permanently 永久性重定向 该状态码表示请求的资源已经被分配了新的URI,以后应使用资源现 ...