码迷,mamicode.com
首页 > 编程语言 > 详细

hdu3999 二叉排序树

时间:2019-09-14 13:27:43      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:add   ali   div   node   turn   highlight   验证   bec   new   

The order of a Tree

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
 
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int pre[N], in[N], post[N];  //先序、中序、后序
int k;
struct node{
    int value;
    node *l, *r;
    //node(int value = 0, node *l = NULL, node *r = NULL):value(value), l(l), r(r){}
};
//void buildtree(int l, int r, int &t, node* &root) { //建树
//    int flag = -1;
//    for(int i = l; i <= r; i++) //先序的第一个是根,找到对应的中序的位置
//        if(in[i] == pre[t]){
//            flag = i; break;
//        }
//    if(flag == -1) return;       //结束
//    root = new node(in[flag]);   //新建结点
//    t++;
//    if(flag > l)  buildtree(l, flag - 1, t, root ->l);
//    if(flag < r)  buildtree(flag + 1, r, t, root ->r);
//}
void add(node * &root ,int & t ){
	if(root == NULL){
		root = new node; 
		root->value=t;
		root->l=root->r=NULL;
	}else{
		if( t > root->value) 
			add( root->r,t);
		else 
			add(root->l,t);
	}
	
}
void preorder (node *root){       //求先序序列
    if(root != NULL){
        post[k++] = root ->value;  //输出
        preorder (root ->l);
        preorder (root ->r);
    }
}
void inorder (node *root){        //求中序序列
    if(root != NULL){
        inorder (root ->l);
        post[k++] = root ->value;  //输出
        inorder (root ->r);
    }
}
void postorder (node *root){      //求后序序列
    if( root != NULL){
        postorder (root ->l);
        postorder (root ->r);
        post[k++] = root ->value;  //输出
    }
}
void remove_tree(node *root){      //释放空间
    if(root == NULL) return;
    remove_tree(root->l);
    remove_tree(root->r);
    delete root;
}
int main(){
    int n;   
	
    while(~scanf("%d", &n)){
		node *root=NULL;

		int x;
        for(int i=1;i<=n;i++){
        	 scanf("%d", &x);
        	 add(root,x );
        }
        k = 0;           //记录结点个数
        preorder(root);
        for(int i=0;i<k;i++) printf("%d%c",post[i], i==k-1? ‘\n‘ : ‘ ‘);
          //作为验证,这里可以用preorder()和inorder()检查先序和中序遍历
        remove_tree(root);
    }
    return 0;
}

  

 
 
 

hdu3999 二叉排序树

标签:add   ali   div   node   turn   highlight   验证   bec   new   

原文地址:https://www.cnblogs.com/lyj1/p/11518881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!