The order of a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1749    Accepted Submission(s):
908

Problem Description
As we know,the shape of a binary search tree is greatly
related to the order of keys we insert. To be precisely:
1.  insert a key k
to a empty tree, then the tree become a tree with
only one
node;
2.  insert a key k to a nonempty tree, if k is less than the root
,insert
it to the left sub-tree;else insert k to the right sub-tree.
We
call the order of keys we insert “the order of a tree”,your task is,given a oder
of a tree, find the order of a tree with the least lexicographic order that
generate the same tree.Two trees are the same if and only if they have the same
shape.
 
Input
There are multiple test cases in an input file. The
first line of each testcase is an integer n(n <= 100,000),represent the
number of nodes.The second line has n intergers,k1 to kn,represent the order of
a tree.To make if more simple, k1 to kn is a sequence of 1 to n.
 
Output
One line with n intergers, which are the order of a
tree that generate the same tree with the least lexicographic.
 
Sample Input
4

1 3 4 2

 
Sample Output
1 3 2 4
 
给一个序列,依照序列建二叉排序树,输出建成这棵树的最小字典序列。
不能使用线段树那种表示方式来表示树,因为可能会退化为一条链,这时会溢出。
所以用lson和rson两个数组分别存每个结点的左右儿子(题中规定,数字为1—n)。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; int lson[],rson[],n; void Insert(int rt,int x)
{
if(rt>x)
{
if(lson[rt]==)
lson[rt]=x;
else
Insert(lson[rt],x);
}
else
{
if(rson[rt]==)
rson[rt]=x;
else
Insert(rson[rt],x);
} } int cnt=;
void order(int rt) //深搜
{
if(rt==)
return;
printf("%d",rt);
cnt++;
if(cnt==n)
printf("\n");
else
printf(" ");
order(lson[rt]);
order(rson[rt]);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
cnt=;
int num,root;
memset(lson,,sizeof(lson));
memset(rson,,sizeof(rson));
scanf("%d",&root);
for(int i=; i<n; i++)
{
scanf("%d",&num);
Insert(root,num);
}
order(root);
//cout<<n<<" "<<cnt<<endl;
}
return ;
}
 

HDU_3999_二叉排序树的更多相关文章

  1. 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较

    二分法查找: 『在有序数组的基础上通过折半方法不断缩小查找范围,直至命中或者查询失败.』   二分法的存储要求:要求顺序存储,以便于根据下标随机访问   二分法的时间效率:O(Log(n))   二分 ...

  2. 二叉排序树(BST)创建,删除,查找操作

    binary search tree,中文翻译为二叉搜索树.二叉查找树或者二叉排序树.简称为BST 一:二叉搜索树的定义 他的定义与树的定义是类似的,也是一个递归的定义: 1.要么是一棵空树 2.如果 ...

  3. 数据结构图文解析之:树的简介及二叉排序树C++模板实现.

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  4. 二叉树建立,遍历和二叉排序树的判断【c++】

    // test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  5. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

  6. 二叉排序树(Binary Sort Tree)

    参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...

  7. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...

  8. 二叉排序树(BST)的建立

    给一个非递归的吧. /* 已知,二叉树存储结构定义见bstree.h,请编写一个算法函数bstree creatBstree(int a[],int n), 以数组a中的数据作为输入建立一棵二叉排序树 ...

  9. POJ 2418 各种二叉排序树

    题意很明确,统计各个字符串所占总串数的百分比,暴力的话肯定超时,看了书上的题解后发现这题主要是用二叉排序树来做,下面附上n种树的代码. 简单的二叉排序树,不作任何优化(C语言版的): #include ...

随机推荐

  1. [NOIP2016day1T1] 玩具迷題(toy)

    题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...

  2. systemtap dtrace与 oracle

    https://fritshoogland.wordpress.com/page/3/ http://externaltable.blogspot.com/2013/06/dtrace-explora ...

  3. EC2:将80端口直接转向8080端口

    当安装Tomcat后,需要将服务器的80端口直接指向8080端口,做法如下:1.    确认当前状态.在终端键入:netstat –ntl命令.输入结果应该和下面图片类似.可以看到8080端口处于监听 ...

  4. mysql高可用架构方案之中的一个(keepalived+主主双活)

    Mysql双主双活+keepalived实现高可用           文件夹 1.前言... 4 2.方案... 4 2.1.环境及软件... 4 2.2.IP规划... 4 2.3.架构图... ...

  5. 【概念的辨异】—— ISO C 与 POSIX C(C standard library 与 C POSIX library)

    ISO C 表示 C Standard Library,也就是 C 标准库. 二者的主要区别在于: POSIX 是 C 标准库的超集(也即是从内容上,C 标准库是 POSIX 库的一部分,POSIX ...

  6. codeforces 898F Hash

    F. Restoring the Expression time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. B2460 [BeiJing2011]元素 线性基

    这个题是对刚才线性基的一个补充,就是中间有一些小贪心,贪心就很有意思,先按权值排序,然后就瞎搞就行了. 题干: Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们 ...

  8. AMQP server localhost:5672 closed the connection. Check login credentials: Socket closed

    2016-04-13 09:23:38.755 18850 INFO oslo.messaging._drivers.impl_rabbit [req-fafc8542-9403-4b5a-89d2- ...

  9. rpmbuild

    rpm2cpio xxx.rpm | cpio -div

  10. Java 8 实战 P4 Beyond Java 8

    目录 Chapter 13. Thinking functionally Chapter 14. Functional programming techniques Chapter 15. compa ...