poj-3481-Double Queue-splay树的水题

本文详细介绍了一种自平衡二叉查找树——伸展树(Splay Tree)的基本概念及其核心操作,并提供了完整的C++实现代码。通过本文,读者可以了解伸展树的数据结构特点、旋转与伸展操作的具体实现方式,以及如何利用伸展树进行插入、查找最大值和最小值等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很水的splay树。

会简单的操作即可。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define maxn 1100000
#define mem(a,b) memset(a,b,sizeof(a))
#define root10 ch[ch[root][1]][0]
#define root1 ch[root][1]
int size[maxn];
int ch[maxn][2];
int pre[maxn];
int root,tot;
int key[maxn];
int val[maxn];
int n;
int pos;
void Treaval(int x) {
    if(x) {
        Treaval(ch[x][0]);
        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2d \n",x,ch[x][0],ch[x][1],pre[x],size[x],key[x]);
        Treaval(ch[x][1]);
    }
}
void debug() {printf("root:%d\n",root);Treaval(root);}
//以上Debug
void init()
{
}
void newnode(int &x,int k,int p,int father)
{
    x=++tot;
    pre[x]=father;
    size[x]=1;
    ch[x][0]=ch[x][1]=0;
    key[x]=p;
    val[x]=k;
}
void push_down(int x)
{
}
void push_up(int x)
{
}
void rot(int x,int kind)
{
    int y=pre[x];
    push_down(y);
    push_down(x);
    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;
    if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    ch[x][kind]=y;
    pre[y]=x;
    push_up(y);
    push_up(x);
}
void splay(int x,int goal)
{
    push_down(x);
    while(pre[x]!=goal)
    {
        if(pre[pre[x]]==goal)
        {
            push_down(pre[x]);
            push_down(x);
            rot(x,ch[pre[x]][0]==x);
        }
        else
        {
            int y=pre[x];
            push_down(pre[y]);
            push_down(y);
            push_down(x);
            int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==x)
            {
                rot(x,!kind);
                rot(x,kind);
            }
            else
            {
                rot(y,kind);
                rot(x,kind);
            }
        }
    }
    push_up(x);
    if(goal==0)root=x;
}

void insert(int k,int p)
{
    int rt=root;
    int r=root;
    while(rt!=0)
    {
        r=rt;
        if(key[rt]<p)rt=ch[rt][1];
        else rt=ch[rt][0];
        //cout<<r<<" "<<rt<<endl;
    }
    newnode(ch[r][key[r]<p],k,p,r);
    splay(ch[r][key[r]<p],root);
    ch[0][0]=ch[0][1]=0;
}

int get_min(int rt)
{
    while(ch[rt][0])rt=ch[rt][0];
    if(rt==root)root=ch[rt][1];
    ch[pre[rt]][0]=ch[rt][1];
    pre[ch[rt][1]]=pre[rt];
    return val[rt];
}
int get_max(int rt)
{
    while(ch[rt][1])rt=ch[rt][1];
    if(rt==root)root=ch[rt][0];
    ch[pre[rt]][1]=ch[rt][0];
    pre[ch[rt][0]]=pre[rt];
    return val[rt];
}
int main()
{
    root=tot=0;
    int op,k,p;
    while(~scanf("%d",&op)&&(op))
    {

        if(op==1)
        {
            scanf("%d%d",&k,&p);
            insert(k,p);
        }
        if(op==2)
        {
            printf("%d\n",get_max(root));
        }
        if(op==3)
        {
            printf("%d\n",get_min(root));
        }
     //   debug();
    }
    return 0;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值