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:

  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。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 #include <iterator>
 5 #include <iostream>
 6 #include <algorithm>
 7 // bits/stdc++.h会编译错误
 8 using namespace std;
 9 typedef char typeData;
10 struct node{
11     typeData data;
12     node *leftChild;
13     node *rightChild;
14     node(){
15         leftChild = NULL;
16         rightChild = NULL;
17     }
18 };
19 vector<typeData> data;
20 string temp, str;
21 void insertBST(node *&root, typeData x){ //二叉搜索树插入函数
22      //注意函数要进行插入操作,根结点要传引用
23     if(root == NULL){   //找到空位置即使插入位置
24         root = new node();  //新建结点权值为x
25         root->data = x;
26         return;
27     }
28     if(x == root->data){    //要插入结点已存在直接返回
29         return;
30     }else if(root->data > x){   //x比根结点数据域小 需要插在左子树
31         insertBST(root->leftChild, x);   //往左子树搜索
32     }else if(root->data < x){    //x比根结点数据域大 需要插在右子树
33         insertBST(root->rightChild, x); //往右子树搜索
34     }
35 }
36 node *createBST(){  //建树
37     node *root = NULL;
38     for(string::iterator it = str.begin(); it != str.end(); it++){
39         insertBST(root, *it);   //以str为数据组建树
40     }
41     return root;    //返回根结点
42 }
43 void preorder(node *root){
44     if(root == NULL)//到达空树为递归边界
45         return;
46     printf("%c", root->data);  //访问根结点输出权值
47     preorder(root->leftChild); //访问左子树
48     preorder(root->rightChild);  //访问右子树
49 }
50 int main()
51 {
52     while(cin >> temp){  //输入字符串
53         if(temp != "*" && temp != "$"){
54             //如果输入的字符串不是组结束符或结尾符
55             str += temp;    //将出入的字符串加到str字符串尾部
56             continue;
57         }
58         //如果输入的时组结束符或结尾符
59         reverse(str.begin(),str.end()); //将str数组反转
60         node *root = createBST();   //建树
61         str = "";   //将str清空
62         preorder(root); //输出前序遍历
63         printf("\n");
64         if(temp == "$")//如果是结尾符跳出循环
65             break;
66     }
67     return 0;
68 }

原文地址:https://www.cnblogs.com/suvvm/p/9873951.html

时间: 2024-10-08 08:44:40

POJ 1577 Falling Leaves 二叉搜索树的相关文章

POJ 1577 Falling Leaves 二叉树题解

给出按最底层叶子节点到根节点的数据,然后要求重建树,前序输出最终建的树. 都是两个基本操作解决: 1 二叉树插入操作 2 前序遍历 简单题目了. #include <stdio.h> #include <string> #include <algorithm> #include <vector> using std::vector; using std::string; const int MAX_B = 1024; char buf[MAX_B]; int

POJ 1577 Falling Leaves 二叉树操作

Description 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

POJ 1577 Falling Leaves

题意:给出一些字符串,从上到下的建树,输出其前序遍历 像前面那一题一样,先建树,然后再递归前序遍历 不过想像上一题那样用数组建树,建树和上题一样的办法,可是应该怎么输出前序遍历呢= = 还是看的题解= = 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<algorithm> 6 using namespace std

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

<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each inpu

用JS实现二叉搜索树

二叉树的节点最多只能有两个子节点,一个左侧子节点,一个右侧子节点. 二叉搜索树(BST),是二叉树的一种,但只允许在左侧节点存储比父节点小的值,在右侧节点存储比父节点大或等于父节点的值. 1.创建BST 1.1创建BST类 首先申明BST类的基本结构 function BinarySearchTree() { var Node = function(key){ this.key = key; this.left = null; this.right = null; }; var root = n

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

二叉搜索树

#include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> using namespace std; struct TreeNode { TreeNode* p; TreeNode* l; TreeNode* r; int key; TreeNode() { p = 0; l = 0; r = 0; key = -1; } }; const int RANDMOD = 30

04-树4 是否同一棵二叉搜索树

给定一个插入序列就可以唯一确定一棵二叉搜索树.然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到.例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果.于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树. 输入格式: 输入包含若干组测试数据.每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要检查的序列个数.第2行给出N个以空格分隔的正整数,作为初始插入序列.最后L行,每行给出N个插入的元素,属于

二叉搜索树建立、插入、删除、前继节点、后继节点之c++实现

一.前言 一直以来,都对树有关的东西望而却步.以前每次说要看一看,都因为惰性,时间就那么荒废掉了.今天下个决心,决定好好的数据结构中的东西看一下.不知道看这篇文章的你,是不是和我有同样的感受,空有一颗努力的心,却迟迟没有付出行动.如果是的话,如果也想好好的把树的知识巩固一下的话,就让我们一起好好儿地把知识点过一遍吧.本文争取让看完的每一个没有基础的同学,都能有所收获.在正文开始前,先给自己加个油.加油(^ω^) 二.二叉搜索树的定义 二叉搜索树是指,对于某一个节点而言,它左边的节点都小于或等于它