• -树-专题


    一. 二叉平衡树

    问题引入:

      用计算机随机生成了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?'
    ':' ');
        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
    ",sum);
            print(root);
        }    
        return 0;
    }
  • 相关阅读:
    (十)jQuery对表单、表格的操作
    (九)jQuery中的动画(载)
    (八)jQuery中的事件
    (七)jQuery中的DOM操作
    (六)jQuery选择器
    (五)解决jQuery和其它库的冲突
    (四)DOM对象和jQuery对象
    (三)初识jQuery
    (二)关于jQuery
    (一)关于jQuery的网上资源
  • 原文地址:https://www.cnblogs.com/sky0917/p/3514561.html
Copyright © 2020-2023  润新知