二叉排序树--cf675d Tree construction

来源:互联网 发布:百川环评工程师软件 编辑:程序博客网 时间:2024/06/05 15:20

给定一个数列,依次建立二叉排序树,求除根结点外,每个结点的父亲。

//二叉排序树所有根结点都比他左儿子大,都比他右儿子小

//最后的树在地上的映射就是排好序的,所以一个点的父亲要么是排序后,在他左边的,要么事在他右边

//并且应该是两个中添加时间最晚的


#include <iostream>

#include <cstdio>

#include <set>

#include <iterator>

#include <map>

using namespacestd;

const int maxn =1e5 + 5;

int main()

{

    int n;

    cin >> n;

    set<int> st;

    map<int,int> tm;

    set<int>::iterator it1,it2;

    int tmp;

    scanf("%d",&tmp);

    int te =0;

    st.insert(tmp);

    tm[0] = te ++;

    for (int i =1; i < n; i ++) {

        scanf("%d",&tmp);

        it1 = st.lower_bound(tmp);

        if(it1 == st.begin())printf("%d",*it1);

        elseif(it1 == st.end())printf("%d",*(--it1));

        else {

            it2 = it1;it1 --;

            if(tm[*it1] > tm[*it2])printf("%d",*(it1));

            elseprintf("%d",*(it2));

        }

        if(i == n -1) printf("\n");

        elseprintf(" ");

        st.insert(tmp);

        tm[tmp] = te ++;

    }

    return0;

}