-树-专题

一. 二叉平衡树

问题引入:

  用计算机随机生成了N个0到1000000000(包含0和1000000000)之间的随机整数(N≤5000000),对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再把这些数从小到大排序。
       请你完成“去重”与“排序”的工作

思路:我们可以维护一个二叉平衡树,来依次插入这些数,最后中序遍历输出即可。

/****************************************
* File Name: 1287.cpp
* Author: sky0917
* Created Time: 2014年01月10日 20:16:49
****************************************/
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define clr(x) memset(x, 0, sizeof(x))
using namespace std;
const int maxn = 5000003;
          
int tot;
int s, n, sum;

struct node{
    int r, v;
    node *ch[2];

    bool operator < (const node &rhs) const{
        return r < rhs.r;
    }

    int cmp(int x) const{
        if (x == v) return -1;
        return x < v?0:1;
    }

}tt[maxn*2];

void roat(node* &o, int d){
    node *k = o->ch[d^1];
       o->ch[d^1] = k->ch[d];
       k->ch[d] = o;
       o = k;
}

void add(node* &o, int x){
    if (o == NULL){
        o = &tt[tot++];
        o->ch[0] = o->ch[1] = NULL;
        o->v = x;
        o->r = rand();
        sum++;
    }
    else{
        int d = o->cmp(x);
        if (d != -1){
            add(o->ch[d], x);
            if (o->ch[d]->r > o->r)
                roat(o, d^1);
        }
    }
}

void print(node *rt){
    if (rt == NULL)
        return;
    print(rt->ch[0]);
    printf("%d%c",rt->v,++s==sum?'\n':' ');
    print(rt->ch[1]);
}
int main(){

    while (scanf("%d",&n)!=EOF){
        tot = 0;
        node *root;    
        root = NULL;
        int va;
        sum = 0;
        for (int i=0; i<n; i++){
            scanf("%d",&va);
            add(root, va);    
        }
        s = 0;
        printf("%d\n",sum);
        print(root);
    }    
    return 0;
}

 

 

转载于:https://www.cnblogs.com/sky0917/p/3514561.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值