zoukankan      html  css  js  c++  java
  • 二叉搜索树

    刚学,只简单地写了。处理的比较幼稚,待改进。

    递归 ,版慢一点

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    using namespace std;
    struct tree{
        int key;//域值 
        int l;//编号 
        int lm;//左长 
        int r;
        int rm;
        int f;//编号 
    }a[999999];
    int root=0,tot=0;
    void add(int x,int now)
    {
        if(a[now].lm ==0&&x<a[now].key )
        {
            a[++tot].key=x;
            a[now].l=tot;
            a[tot].f=now;
            a[now].lm++;
            return ;
        }
        if(a[now].rm ==0&&x>=a[now].key )
        {
            a[++tot].key=x;
            a[now].r=tot;
            a[tot].f=now;
            a[now].rm++;
            return ;
        }    
        //if(x==a[now].key)    return ;
        if(x<a[now].key)    add(x,a[now].l),a[now].lm++;else
        if(x>=a[now].key)    add(x,a[now].r),a[now].rm++;
        return ;
    }
    void balance()
    {
        while(a[root].lm >a[root].rm+tot/4)
        {
            a[root].f=a[root].l;
            a[a[root].l ].r=root;
            root=a[root].l;
        }else
        while(a[root].lm <=a[root].rm+tot/4)
        {
            a[root].f=a[root].r;
            a[a[root].r ].l=root;
            root=a[root].r;
        }    
        return ;
    } 
    void delet(int x)
    {
        int now=findw(x);
        if(now==root)
        {
            if(a[now].lm >a[now].rm )
            {
                
                a[a[root].l].r=a[root].r;
                root=a[root].l;
            }
        }         
    }
    int find3(int x,int now)
    {
        if(a[now].lm ==x-1)    return a[now].key ;
        if(a[now].lm >x-1)    return find3(x,a[now].l );
        if(a[now].lm<x-1) return find3(x-a[now].lm-1,a[now].r );
    }
    int find4(int x,int now)
    {
        if(a[now].key==x)    return a[now].lm+1;
        if(x<a[now].key)    return    find4(x,a[now].l);
        if(x>a[now].key)    return find4(x,a[now].r)+a[now].lm+1;
    }
    int find5(int x,int now)
    {
        int w=a[now].key ;
        if(w==x)    return w;
        if(w>x)    
        {
            if(a[now].lm ==0)    return w;
            return find5(x,a[now].l );
        }
        if(w<x)
        {
            if(a[now].rm ==0)    return a[a[now].f ].key;
            return find5(x,a[now].r );
        }
    }
    int findw(int x,int now)
    {
        if(a[now].key==x)    return now;    
        if(x<a[now].key)    return    findw(x,a[now].l);
        if(x>a[now].key)    return findw(x,a[now].r);
    }
    int find6(int x)
    {
        int now=findw(x,root);    
        if(a[now].lm!=0)
        return a[a[now].l].key;
        return a[a[now].f].key;
         
    }
    int find7(int x)
    {
        int now=findw(x,root);
        if(a[now].rm!=0)
        return a[a[now].r].key;
        return a[a[now].f].key;    
    }
    int main()
    {
        int p,x,n;
        int pp[20],z[20];
        root=0,tot=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        scanf("%d%d",&pp[i],&z[i]);    
        for(int i=1;i<=n;i++)
        {
        
            p=pp[i];x=z[i];    
            if(p==1)
            {
                if(tot==0)    a[++tot].key=x,root=tot;
                else
                add(x,root);            
            }
            if(p==3)    printf("%d
    ",find3(x,root));
            if(p==4)    printf("%d
    ",find4(x,root));
            if(p==5)    printf("%d
    ",find5(x,root));
            if(p==6)    printf("%d
    ",find6(x));        
            if(p==7)    printf("%d
    ",find7(x));
        }    
        return 0;
    }     

     迭代快一些吧。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    using namespace std;
    struct tree{
        int key;//域值 
        int l;//编号 
        int lm;//左长 
        int r;
        int rm;
        int f;//编号 
    }a[999999];
    int root=0,tot=0;
    void add(int x,int now)
    {
        if(a[now].lm ==0&&x<a[now].key )
        {
            a[++tot].key=x;
            a[now].l=tot;
            a[tot].f=now;
            a[now].lm++;
            return ;
        }
        if(a[now].rm ==0&&x>=a[now].key )
        {
            a[++tot].key=x;
            a[now].r=tot;
            a[tot].f=now;
            a[now].rm++;
            return ;
        }    
        //if(x==a[now].key)    return ;
        if(x<a[now].key)    add(x,a[now].l),a[now].lm++;else
        if(x>=a[now].key)    add(x,a[now].r),a[now].rm++;
        return ;
    }
    void balance()
    {
        while(a[root].lm >a[root].rm+tot/4)
        {
            a[root].f=a[root].l;
            a[a[root].l ].r=root;
            root=a[root].l;
        }
        while(a[root].lm <=a[root].rm+tot/4)
        {
            a[root].f=a[root].r;
            a[a[root].r ].l=root;
            root=a[root].r;
        }    
        return ;
    } 
    int findw(int x,int now)
    {
        while(a[now].key !=x)
        {
            if(x<a[now].key)
                now=a[now].l;else        
            if(x>a[now].key)
                now=a[now].r;
        }    
        return now;
    }
    void delet(int x)
    {
        int now=findw(x,root);
        if(now==root)
        {
            if(a[now].lm >a[now].rm )
            {
                
                a[a[root].l].r=a[root].r;
                root=a[root].l;
            }
        }         
    }
    void find3(int x,int now)//查询第x个数 
    {    
        while(a[now].lm!=x-1)
        {
            if(a[now].lm >x-1)
                now=a[now].l;else
            if(a[now].lm<x-1)
                x=x-a[now].lm-1,now=a[now].r ;
        }
        printf("%d
    ",a[now].key );
        return ;
    }
    void find4(int x,int now)//查询x的排名 
    {
        int ans=0;
        while(a[now].key!=x)
        {
            if(x<a[now].key)
                now=a[now].l;else
            if(x>a[now].key )
                now=a[now].r,ans+=a[now].lm+1;            
        }
        printf("%d
    ",a[now].lm+1+ans);
        return ;
    }
    int find5(int x,int now)//不小于x的最小元素 
    {
        int w=a[now].key ;
        if(w==x)    return w;
        if(w>x)    
        {
            if(a[now].lm ==0)    return w;
            return find5(x,a[now].l );
        }
        if(w<x)
        {
            if(a[now].rm ==0)    return a[a[now].f ].key;
            return find5(x,a[now].r );
        }
        
    }
    int find6(int x)//前驱 
    {
        int now=findw(x,root);    
        if(a[now].lm!=0)
            return a[a[now].l].key;
        return a[a[now].f].key;     
    }
    int find7(int x)//后驱 
    {
        int now=findw(x,root);
        if(a[now].rm!=0)
        return a[a[now].r].key;
        return a[a[now].f].key;    
    }
    int main() 
    {
        int p,x,n;
        int pp[20],z[20];
        root=0,tot=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        scanf("%d%d",&pp[i],&z[i]);    
        for(int i=1;i<=n;i++)
        {
        
            p=pp[i];x=z[i];    
            if(p==1)
            {
                if(tot==0)    a[++tot].key=x,root=tot;
                else
                add(x,root);            
            }
            if(p==3)    find3(x,root);
            if(p==4)    find4(x,root);
            if(p==5)    printf("%d
    ",find5(x,root));
            if(p==6)    printf("%d
    ",find6(x));        
            if(p==7)    printf("%d
    ",find7(x));
        }    
        return 0;
    }     
  • 相关阅读:
    Unknown custom element: <el-container1>
    jQuery手机对话框插件
    告别2013,迎接2014
    淘宝开放平台主动通知的实现
    搭建JavaWeb服务器
    深入理解JavaScript执行上下文和执行栈
    为什么要选择学习Java?适合零基础的初学者的文章
    成为一名优秀的Java程序员9+难以置信的公式
    深入理解JavaScript作用域和作用域链
    JavaScript数据类型转换
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/6819466.html
Copyright © 2011-2022 走看看