zoukankan      html  css  js  c++  java
  • hdu 3999 二叉查找树

    The order of a Tree

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


    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
     
     
     1 /*
     2 4
     3 1 3 4 2
     4 
     5 1 3 2 4
     6 */
     7 #include<iostream>
     8 #include<stdio.h>
     9 #include<cstring>
    10 #include<cstdlib>
    11 using namespace std;
    12 
    13 struct node
    14 {
    15     int rp;
    16     struct node *lchild;
    17     struct node *rchild;
    18 };
    19 
    20 int n,len;
    21 
    22 void mem(struct node *p)
    23 {
    24     p->rp=0;
    25     p->lchild=NULL;
    26     p->rchild=NULL;
    27 }
    28 void insert_ecs(struct node **p,int x)
    29 {
    30     if((*p)==NULL)
    31     {
    32         (*p)=(struct node*)malloc(sizeof(struct node));
    33         mem(*p);
    34         (*p)->rp=x;
    35         return;
    36     }
    37     if( (*p)->rp > x)
    38         insert_ecs( &(*p)->lchild,x);
    39     else insert_ecs( &(*p)->rchild,x);
    40 }
    41 void serch(struct node *p)
    42 {
    43     printf("%d",p->rp);
    44     len++;
    45     if(len!=n) printf(" ",p->rp);
    46     else printf("
    ");
    47 
    48     if( p->lchild!=NULL )
    49         serch(p->lchild);
    50     if( p->rchild!=NULL )
    51         serch(p->rchild);
    52 }
    53 void deal(struct node *p)
    54 {
    55     if(p->lchild!=NULL)
    56         deal(p->lchild);
    57     if(p->rchild!=NULL)
    58         deal(p->rchild);
    59     free(p);
    60 }
    61 int main()
    62 {
    63     int i,x;
    64     while(scanf("%d",&n)>0)
    65     {
    66         struct node *root=NULL;
    67         for(i=1;i<=n;i++)
    68         {
    69             scanf("%d",&x);
    70             insert_ecs(&root,x);
    71         }
    72         len=0;
    73         serch(root);
    74         free(root);
    75     }
    76     return 0;
    77 }
     
  • 相关阅读:
    FPGA-中值滤波
    FPGA-中值滤波
    FPGA-shift_ram代码
    FPGA实现-shift_ram_3x3矩阵实现
    图像处理-中值滤波
    python-str
    ignore-certificate-errors(chrome)
    selenium-python-Cookie跳过登录验证码
    Angular2
    Angular2
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3574772.html
Copyright © 2011-2022 走看看