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 ...
随机推荐
- OpenglEs开篇
1.,但博客有接近一年没有写了.虽然有学到东西,但没有记录感觉是是空空的,最近在学习Opengles, 现在开始重操旧业(写博客了).
- 深度学习之TensorFlow的介绍与安装
TensorFlow是一个采用数据流图(data flow graphs)用于数值计算的开源软件库.它最初是由Google大脑小组的研发人员设计开发的,用于机器学习和神经网络方面的研究.但是这个系统的 ...
- 使用pscp/pslurp批量并发分发/回收文件
pssh pssh -h ip文件 本地文件 远程目录或文件 pslurp pslurp -h ip文件 -L 本地目录 远程文件 本地文件名称
- WPF透明窗体不支持缩放解决方案
方案一 WPF中的无边框透明窗体,由于没有边并且透明,窗体无法进行缩放操作,今天来讲解如何解决这个问题. 先说一下思路,我们先手为该窗体添加4个边,4个角用于缩放操作,然后再为他们写事件,完成拖放操作 ...
- 【转】如何成为一名优秀的web前端工程师(前端攻城师)?
[转自]http://julying.com/blog/how-to-become-a-good-web-front-end-engineer/ 程序设计之道无远弗届,御晨风而返.———— 杰佛瑞 · ...
- iOS Objective-C 中 bool 与 BOOL 的你不一定知道的事
测试一下这段代码: - (void)test { NSLog(@"this is an attribut: %d", anAttribute); ; i < ; i++) { ...
- python--函数名的使用,闭包,迭代器
1.函数名的使用和第一类对象 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数 函数对象可以像变量一样进行赋值,还可以作为列表的元素进行使用,可以作为返回值返回,可以作为参数进行传递 1 ...
- java—在dbutils中处理事务与不确定条件的查询(46)
在dbutils中处理事务 事务是指用户的一次操作.这一次操作有可能是一个表,也有可能是多个表,也有可能是对一个表的多次操作. 只要是: 1:对数据数据库进行多次操作. 2:多个表,还是 ...
- js滚动距离
滚动距离 onclick="$('html,body').animate({scrollTop:$('.J_user_evaluate').offset().top-110},500)&qu ...
- 创建maven自定义archetype项目
1.安装Nexus这里是用homebrew安装, brew nexus 安装成功后,默认的访问端口为8081, 我这里的访问地址是http://192.168.99.100:8081 默认用户:adm ...