初涉Splay Tree

Splay Tree 支持的之中操作。

插入,删除,求前驱和后即,区间更新与查询。


先来一发Splay Tree最基础的操作——伸展,顺便求前驱和后即。[HNOI2002]营业额统计。

白书上讲的貌似要讨论目标节点,父节点,父节点的父节点,这三个节点之间的关系。

没太看懂,直接按照自己想得来了。


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long LL
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 1000000007

using namespace std;

const int MAXN = 100100;

struct N
{
    int data,son[2],pre;
}st[MAXN*2];

int Top;

void Rotate(int root,int pre,int dir)
{
    N temp = st[pre];
    temp.son[dir] = st[root].son[!dir];
    st[pre] = st[root];
    st[pre].son[!dir] = root;
    st[root] = temp;

    st[pre].pre = temp.pre;

    st[st[pre].son[0]].pre = pre;
    st[st[pre].son[1]].pre = pre;
    st[st[root].son[0]].pre = root;
    st[st[root].son[1]].pre = root;
}


//将目标节点旋转到goal下面,若goal == -1,则表示为将目标节点旋转到根节点。
void Splay(int root,int goal)
{
    while(st[root].pre != goal)
    {
        Rotate(root,st[root].pre,st[st[root].pre].son[0] == root ? 0 : 1);
        root = st[root].pre;
    }
}

bool Insert(int &root,int x,int pre)
{
    if(root == -1 || st[root].data == -1)
    {
        if(root == -1)
            root = Top++;

        st[root].pre = pre;
        st[root].data = x;
        st[root].son[0] = -1;
        st[root].son[1] = -1;

        if(x != -1)
        {
            Insert(st[root].son[0],-1,root);
            Insert(st[root].son[1],-1,root);
            Splay(root,-1);
        }

        return true;
    }

    if(x == st[root].data)
        return false;

    if(x < st[root].data)
        return Insert(st[root].son[0],x,root);
    else
        return Insert(st[root].son[1],x,root);
}

int Search_Min(int root)
{
    if(st[st[root].son[0]].data == -1)
        return st[root].data;
    return Search_Min(st[root].son[0]);
}

int Search_Max(int root)
{
    if(st[st[root].son[1]].data == -1)
        return st[root].data;
    return Search_Max(st[root].son[1]);
}

int main()
{
    //freopen("data.txt","r",stdin);
    int n,x,i;

    int root;

    int sum;

    int MAX = ((1<<30)-1)*2+1;

    scanf("%d",&n);
    sum = 0;
    root = -1;

    for(i = 1; i <= n; ++i)
    {
        if(scanf("%d",&x) == EOF)
            x = 0;

        if(Insert(root,x,-1) == false)
            continue;

        int temp = MAX;

        if(st[st[root].son[0]].data != -1)
            temp = min(temp,abs(Search_Max(st[root].son[0])-st[root].data));
        if(st[st[root].son[1]].data != -1)
            temp = min(temp,abs(Search_Min(st[root].son[1])-st[root].data));
        sum += (temp == MAX ? x : temp);

    }

    printf("%d\n",sum);

    return 0;
}

### 关于Splay Tree(伸展树)的数据结构实现 #### Splay Tree简介 Splay Tree 是一种自调整二叉搜索树,在每次访问之后会将被访问的节点旋转至根部位置。这种特性使得频繁访问的节点能够更快地再次获取。 #### 基本操作原理 当执行查找、插入或删除等基本操作时,如果找到了目标键,则将其通过一系列zigzag/zig-zig类型的单旋转变换移动到树顶;如果没有找到,则把最后比较的那个叶子结点提到顶部[^2]。 #### Python代码示例 下面是一个简单的Python版本的Splay Tree实现: ```python class TreeNode: def __init__(self, key=None): self.key = key self.left = None self.right = None self.parent = None def zig(node): # 右旋 parent = node.parent grandparent = parent.parent # Perform rotation parent.left = node.right if node.right is not None: node.right.parent = parent node.right = parent parent.parent = node # Update parent reference of the rotated subtree root node.parent = grandparent if grandparent is not None: if grandparent.left == parent: grandparent.left = node else: grandparent.right = node return node def zag(node): # 左旋 parent = node.parent grandparent = parent.parent # Perform rotation parent.right = node.left if node.left is not None: node.left.parent = parent node.left = parent parent.parent = node # Update parent reference of the rotated subtree root node.parent = grandparent if grandparent is not None: if grandparent.left == parent: grandparent.left = node else: grandparent.right = node return node def splay(root, target_key): if root is None or root.key == target_key: return root current_node = root while True: if current_node.key < target_key: if current_node.right is None: break elif current_node.right.key < target_key: current_node.right = zig(current_node.right) if current_node.right is None: break if current_node.parent is not None: current_node = zag(current_node) elif current_node.key > target_key: if current_node.left is None: break elif current_node.left.key > target_key: current_node.left = zag(current_node.left) if current_node.left is None: break if current_node.parent is not None: current_node = zig(current_node) return current_node ``` 此段程序定义了一个`TreeNode`类表示树中的每一个节点,并实现了两个辅助函数`zig()`和`zag()`来进行必要的旋转操作。核心部分在于`splay()`函数,该函数接收当前子树的根以及要寻找的目标关键字作为参数,最终返回经过调整后的新的子树根节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值