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: 

  1. It may be empty.
  2. 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: 

  1. Empty trees are omitted completely.
  2. 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: 

  1. If the tree is empty, then the preorder traversal is empty.
  2. 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 input will contain one or more data sets. Each data set is a sequence of one or more lines of capital letters.

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

For each input data set, there is a unique binary search tree that would produce the sequence of leaves. The output is a line containing only the preorder traversal of that tree, with no blanks.

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 二叉搜索树的更多相关文章

  1. 【二叉搜索树】poj 1577 Falling Leaves

    http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字 ...

  2. POJ 1577 Falling Leaves(二叉搜索树)

    思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个  二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...

  3. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...

  4. POJ 1577 Falling Leaves (子母二叉树,给出叶子节点的删除序列,求前序遍历)

    题意:给出一棵字母二叉树删除叶子节点的序列,按删除的顺序排列.让你输出该棵二叉树额前序遍历的序列.思路:先把一棵树的所有删除的叶子节点序列存储下来,然后从最后一行字符串开始建树即可,最后遍历输出.   ...

  5. POJ 1577 Falling Leaves

    题意:给出一些字符串,从上到下的建树,输出其前序遍历 像前面那一题一样,先建树,然后再递归前序遍历 不过想像上一题那样用数组建树,建树和上题一样的办法,可是应该怎么输出前序遍历呢= = 还是看的题解= ...

  6. POJ 2309 BST(二叉搜索树)

    思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...

  7. 二叉搜索树 POJ 2418 Hardwood Species

    题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

随机推荐

  1. 通过代码去执行testNG用例

    背景 用testNG去编写的测试用例,通过@Test去执行用例,一般本地都是通过IDE去执行相应的方法,持续集成的话,都是通过maven来执行或指定testNG.xml执行,但是如果想通过接口/界面去 ...

  2. 配置git使用socks5代理

    git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'socks5://1 ...

  3. (转)最全正则表达式总结:验证QQ号、手机号、Email、中文、邮编、身份证、IP地址等

    什么是 RegExp? RegExp 是正则表达式(Regular expression)的缩写,作用是对字符串执行模式匹配. 通常用于格式验证.正则替换.查找子串等 各种编程语言的正则表达式基本相同 ...

  4. Android 获取模拟器与真机数据库

    模拟器: localuser:~ localhost$ adb shell shell@android:/ $ su // 数据库复制到 Download 下 shell@android:/ # cp ...

  5. CF834D The Bakery

    题目链接:戳我 题意:将一个长度为n的序列分为k段,使得总价值最大.一段区间的价值表示为区间内不同数字的个数 \(n<=35000,k<=50\) 开始想的转移方程是这个样子的--\(dp ...

  6. 洛谷P2179 [NOI2012]骑行川藏(拉格朗日乘数法)

    题面 传送门 题解 看\(mashirosky\)大佬的题解吧--这里 //minamoto #include<bits/stdc++.h> #define R register #def ...

  7. BZOJ 3239--Discrete Logging(BSGS)

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 635  Solved: 413[Submit][Statu ...

  8. 网站架构:PHP针对并发访问如何优化?

    1.拆表:大表拆小表(垂直拆,水平拆:分表,分区partition,分片sharding),可以在应用层实现,也可以在数据库层面实现一部分:提高系统性能. 2.分库:把表放到不同的数据库,这也是分布式 ...

  9. Oracle ltrim() rtrim() 函数详细用法

    今天在论坛里看了一篇帖子,讨论ltrim() 函数的详细用法,下面我借几个高手的回答总结一下: 先看几个实例: SQL> select ltrim('109224323','109') from ...

  10. 回归到jquery

    最近在做一个公司的老产品的新功能,使用原来的技术框架,jquery和一堆插件,使用jquery的话,灵活性是有了,但是对于一个工作了3年多的我来说,很low,没什么成就感,技术本身比较简单,但是业务的 ...