hdu3999 二叉排序树

Posted lyj1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu3999 二叉排序树相关的知识,希望对你有一定的参考价值。

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 二叉排序树的主要内容,如果未能解决你的问题,请参考以下文章

二叉树及特殊二叉树(满二叉树完全二叉树二叉排序树平衡二叉树)的定义和性质(附详细推理过程)

数据结构与算法系列研究五——树二叉树三叉树平衡排序二叉树AVL

二叉排序树

二叉树二叉查找树

二叉排序树

二叉排序树和平衡二叉树