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
 
Source
 
#include<stdio.h>
#include<malloc.h>
typedef struct tree
{
struct tree *lchilde,*rchilde;
int w;
}tree,*Tree;
void creaRoot(Tree &root)//创建根节点
{
root=(Tree)malloc(sizeof(tree));
root->lchilde=root->rchilde=NULL;
}
void printTree(Tree root,int k)//遍历树并输出该节点的值
{
if(root==NULL) return ;//为空时反回
if(k==0)
printf("%d",root->w);
else
printf(" %d",root->w);
printTree(root->lchilde,k+1);//先搜左
printTree(root->rchilde,k+1);//后搜右
}
void InTree(Tree &node,int x)//建立二叉树,比当前节点小的放左边,否则放右边
{
Tree T;
if(node==NULL)//为空时,就把该值放到该节点
{
T=(Tree)malloc(sizeof (tree));
T->w=x; T->lchilde=T->rchilde=NULL;//把左右孩子置为空
node=T;
}
else//不为空,继续搜
{
if(x<=node->w)
InTree(node->lchilde,x);
else
InTree(node->rchilde,x);
}
}
int main()
{
Tree root;
int n,i,x;
while(scanf("%d",&n)>0)
{
if(n==0)continue;
creaRoot(root);
scanf("%d",&x);
root->w=x;
for(i=2;i<=n;i++)
{
scanf("%d",&x);
InTree(root,x);
}
printTree(root,0);
printf("\n");
}
}

hdu3999The order of a Tree (二叉平衡树(AVL))的更多相关文章

  1. Algorithms: 二叉平衡树(AVL)

    二叉平衡树(AVL):   这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有.  后面两月又要忙了. ...

  2. 二叉平衡树AVL的插入与删除(java实现)

    二叉平衡树 全图基础解释参考链接:http://btechsmartclass.com/data_structures/avl-trees.html 二叉平衡树:https://www.cnblogs ...

  3. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

  4. HDU 3999 The order of a Tree 二叉搜索树 BST

    建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...

  5. (4) 二叉平衡树, AVL树

    1.为什么要有平衡二叉树? 上一节我们讲了一般的二叉查找树, 其期望深度为O(log2n), 其各操作的时间复杂度O(log2n)同时也是由此决定的.但是在某些情况下(如在插入的序列是有序的时候), ...

  6. 树-二叉平衡树AVL

    基本概念 AVL树:树中任何节点的两个子树的高度最大差别为1. AVL树的查找.插入和删除在平均和最坏情况下都是O(logn). AVL实现 AVL树的节点包括的几个组成对象: (01) key -- ...

  7. java项目---用java实现二叉平衡树(AVL树)并打印结果(详)(3星)

    package Demo; public class AVLtree { private Node root; //首先定义根节点 private static class Node{ //定义Nod ...

  8. 各种查找算法的选用分析(顺序查找、二分查找、二叉平衡树、B树、红黑树、B+树)

    目录 顺序查找 二分查找 二叉平衡树 B树 红黑树 B+树 参考文档 顺序查找 给你一组数,最自然的效率最低的查找算法是顺序查找--从头到尾挨个挨个遍历查找,它的时间复杂度为O(n). 二分查找 而另 ...

  9. 判断一颗二叉树是否为二叉平衡树 python 代码

    输入一颗二叉树,判断这棵树是否为二叉平衡树.首先来看一下二叉平衡树的概念:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.因此判断一颗二叉平衡树的关键在于 ...

随机推荐

  1. GridView用法的修改和删除

    (前台) <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...

  2. 安卓开发37:自定义的HorizontalScrollView类,使其pageScroll的时候焦点不选中

    自定义一个HorizontalScrollView类,主要为了让这个HorizontalScrollView不能鼠标点击,不能左右按键,并且没有焦点. public class ImageMoveHo ...

  3. poj 1579 Function Run Fun(记忆化搜索+dp)

    题目链接:http://poj.org/problem?id=1579 思路分析:题目给出递归公式,使用动态规划的记忆搜索即可解决. 代码如下: #include <stdio.h> #i ...

  4. ADS2008 安装方法详解及文件下载

    一.我的安装的过程及方法 正常安装的方法: 1.- Install the program. 2.- Copy "license.lic" into "C:\ADS200 ...

  5. OracleDBConsoleorcl无法启动

    OracleDBConsoleorcl无法启动 向左转|向右转 提问者采纳  2010-10-13 19:40 我前几天刚解决了这个问题 这个错误原因是因为你的ip是动态获取的,你在安装Oracle时 ...

  6. setInterval定义与调用

    以下是一个倒计时,可以定义定时器为全局变量(ti2),或局部变量(ti). <script type="text/javascript" src="js/jquer ...

  7. 设置outlook自动回复

    当有同事需要出差时,或者不能即时回复邮件时,可用此功能. 下面列出设置步骤: 1.  首先,在桌面新建一个用以保存模板的文件夹,例如:emaii. 2.  新建一封邮件,输入你要自动回复的内容.另存为 ...

  8. 【Hibernate】set排序

    使用hibernate进行一对多操作的时候,普遍使用HashSet进行操作.但是HashSet是无序集合,对此可以使用TreeSet进行排序. 1.将HashSet改为TreeSet private ...

  9. 【SVN】is out of date

    右击项目(team->update 或者 team->freash/cleanup),再操作,提交就可以了.

  10. 2080夹角有多大II

    寻人启事:2014级新生看过来! 夹角有多大II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...