• 搜索二叉树


    缺少删除,见下一篇中的treap平衡树
    特点:左大右小

    这里写图片描述

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #define M 99999
    using namespace std;
    int lc[M],rc[M],size[M],key[M],f[M],tot=0;
    int n,m;
    int root=1;
    int add(int v)
    {
        tot++;
        size[tot]=1;
        key[tot]=v;
        return tot;
    }
    void insert(int &t,int v)
    {
        if(!t) t=add(v);
        else 
            if(v<key[t])
                insert(lc[t],v);        
            else
                insert(rc[t],v);    
        size[t]=size[lc[t]]+size[rc[t]]+1;
    }
    
    /*int askmin(int t,int k)//查询第k小 
    {
        if(size[lc[t]]==k-1) return key[t];
        if(size[lc[t]]>k-1) return askmin(lc[t],k);
        else return askmin(rc[t],k-size[lc[t]]-1);//当前点是第size[lc[t]]+1大的值 
    }//与下面while版 askmin()等价 */
    int askmin(int t,int k)//查找第k小的数 
    {
        int b=0;
        while(1){
            if(size[lc[t]]==k-(1+b)) return key[t];
            if(size[lc[t]]>k-1) t=lc[t];
            else {
                b+=size[lc[t]]+1;// b累加 
                t=rc[t];
            }   
        } 
    }
    
    
    /*int askxth(int t,int x)//查询x的排名  
    {
        if(key[t]==x) return size[lc[t]]+1;
        if(key[t]> x) return askxth(lc[t],x);
        if(key[t]< x) return askxth(rc[t],x)+size[lc[t]]+1;
    }//下面是 while*/
    
    int askxth(int t,int x)//查询x的排名 
    {
        int b=0;
        while(1)
        {
            if(key[t]==x) break;
            if(key[t]>x) t=lc[t];
            if(key[t]<x) {
                b+=size[lc[t]]+1;// b累加 
                t=rc[t];
            } 
        }
        return b+size[lc[t]]+1;
    }
    
    
    int askxup(int t,int x)//查询不小于x的最小值 
    {
        if(key[t]>=x&&(key[lc[t]]<x||!lc[t])) return key[t];
        if(key[t]<x) return askxup(rc[t],x);
        else return askxup(lc[t],x);  
    } 
    
    
    int ask_front(int t,int x) 
    {
        return askmin(t,askxth(t,x)-1);
    }
    int ask_behind(int t,int x)
    {
        return askmin(t,askxth(t,x)+1);
    }
    
    
    int main()
    {
        int p,x;    
        scanf("%d",&n);
        scanf("%d%d",&p,&x);
        key[1]=x;
        tot++;
        for(int i=2;i<=n;i++)
        {       
            scanf("%d%d",&p,&x);
            if(p==1) insert(root,x);//建树!
            if(p==3) printf("%d
    ",askmin(root,x));//查询第x小的数
            if(p==4) printf("%d
    ",askxth(root,x));//查询元素x的排名
            if(p==5) printf("%d
    ",askxup(root,x));//查询树中不小于数x的最小的数
            if(p==6) printf("%d
    ",ask_front(root,x));//查询元素x的前驱
            if(p==7) printf("%d
    ",ask_behind(root,x));//后继
        }   
        return 0;
    } 
  • 相关阅读:
    SQL2005 SQL2008 远程连接配置方法
    Subvision 安装 部署 TortoiseSVN
    在wpf或winform关闭子窗口或对子窗口进行某个操作后刷新父窗口
    C# 中的委托和事件
    长数字隔三位用逗号","隔开,保留两位小数,指定长度,不足补空格
    C# 柱状图, 折线图, 扇形图
    如何在Visual Studio 2010旗舰版本下安装Window Phone 7 简体中文开发环境
    vs2010发布、打包安装程序(超全超详细)
    java 环境搭建
    SQL2008 转 2000(高版本转换到低版本)
  • 原文地址:https://www.cnblogs.com/dfsac/p/7587946.html
Copyright © 2020-2023  润新知