zoukankan      html  css  js  c++  java
  • HDU 3999 The order of a Tree

    The order of a Tree

      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;
    typedef int dataTpye;
    vector<dataTpye> str;
    struct node{
        dataTpye data;
        node *leftChild;
        node *rightChild;
        node(){
            leftChild = NULL;
            rightChild = NULL;
        }
    };
    void insertBST(node *&root, dataTpye x){//二叉搜索树插入函数
        if(root == NULL){   //找到空位置即使插入位置
            root = new node();  //新建结点权值为x
            root->data = x;
            return;
        }
        if(root->data == x) //要插入结点已存在直接返回
            return;
        else if(root->data > x){    //x比根结点数据域小 需要插在左子树
            insertBST(root->leftChild, x);
        }else if(root->data < x){   //x比根结点数据域大 需要插在右子树
            insertBST(root->rightChild, x);
        }
    }
    node *createBST(){   //建树
        node *root = NULL;
        for(vector<dataTpye>::iterator it = str.begin(); it != str.end(); it++){
            insertBST(root, *it);   //以str为数据组建树
        }
        return root;     //返回根结点
    }
    void preorder(node *root, node *flag){
        if(root == NULL)    //到达空树为递归边界
            return;
        if(root != flag)
             printf(" ");
        printf("%d", root->data);   //访问根结点输出权值
        preorder(root->leftChild, flag);    //访问左子树
        preorder(root->rightChild, flag);    //访问右子树
    
    }
    int main()
    {
        int t;
        while(cin >> t){    //输入结点数量
            str.clear();    //清空权值记录容器
            for(int i = 0; i < t; i++){
                dataTpye temp;
                scanf("%d", &temp); //输入权值
                str.push_back(temp);
            }
            node *root = createBST();   //建树
            preorder(root, root);   //输出先序遍历
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    搭建环境遇到的几个问题
    webservice
    Eclipse 反编译 阅读class 文件
    设置navigationBar上面的item
    自定义的UITabbar上面的按钮的x坐标的计算方法
    UIToolbar自定义背景及按钮设置
    UITabBar实现自定义背景及UITabBarItem自定义图片和字体
    IOS APP圆形图片的实现
    如果AlertView输入框为空,则禁止点击确定按钮
    两种局部刷新UITableView的方法的使用条件
  • 原文地址:https://www.cnblogs.com/suvvm/p/9880366.html
Copyright © 2011-2022 走看看