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. 基于原生js的图片延迟加载

    当页面图片比较多的时候,我们通常会做一个延迟加载,避免页面打开时一下子的请求数太多,加载过慢影响用户体验. 如果项目用了jquery框架,则可以直接用 jquery.lazyload.可在jquery ...

  2. 转:前端冷知识(~~some fun , some useful)

    前端不为人知的一面——前端冷知识集锦 前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Qu ...

  3. IF的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. week4_motion_of_ball_1(小球运动)——最基本

    # Ball motion with an explicit timer import simplegui # Initialize globals width = 600 height = 600 ...

  5. oracle常用函数以及调用入参为record的存储过程的方法,

    转自:http://www.cnblogs.com/zhangronghua/archive/2007/08/20/862812.html SQL中的单记录函数1.ASCII返回与指定的字符对应的十进 ...

  6. centos 安装lua

    yum install readline-develwget http://www.lua.org/ftp/lua-5.1.4.tar.gztar -xzvf lua-5.1.4.tar.gz3.编译 ...

  7. zabbix 邮件告警配置

    使用外部邮箱账号发送报警邮件设置 一.关闭sendmail或者postfix service sendmail stop #关闭 chkconfig sendmail off #禁止开机启动 serv ...

  8. SSH整合,"sessionFactory " or "hibernateTemplate " is required异常

    首先遇到的问题就是HibernateDaoSupport引起的,程序中所有的DAO都继承自HibernateDaoSupport,而HibernateDaoSupport需要注入sessionfact ...

  9. OpenBlas编译方法(体验msys下使用MingW)

    OpenBlas是一个优化的Blas库,基于GotoBlas21.13 BSD版,安装步骤如下: Windows下安装: 1. 在SourgeForge下载最新的OpenBlas库:http://so ...

  10. Bugzilla使用手册及解决方案

    Bugzilla使用手册 Bugzilla 是一个开源的缺陷跟踪系统(Bug-Tracking System),它可以管理软件开发中缺陷的提交(new),修复(resolve),关闭(close)等整 ...