splay

本文介绍了一种使用二叉搜索树实现的数据结构,并详细展示了插入和删除节点的操作过程及旋转调整方法。通过具体代码实例,展示了如何构建和维护一棵平衡的二叉搜索树。

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

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN = 100000 + 55;
int L[MAXN], R[MAXN], F[MAXN];
int tre[MAXN];
int tot, root;
inline void clear()
{
    memset(L, -1, sizeof(L));
    memset(R, -1, sizeof(R));
    memset(F, -1, sizeof(F));
    tot = root = 0;
}
void insert(int t, int x)
{
    if (tot == 0)
    {
        root = ++tot;
        tre[root] = x;
        F[root] = root;
        return;
    }
    if (x >=  tre[t])
    {
        if (R[t] > 0)
        {
            insert(R[t], x);
        }
        else
        {
            R[t] = ++tot;
            tre[R[t]] = x;
            F[R[t]] = t;
        }
    }
    else
    {
        if (L[t] > 0)
        {
            insert(L[t], x);
        }
        else
        {
            L[t] = ++tot;
            tre[L[t]] = x;
            F[L[t]] = t;
        }
    }
}
void rotate(int x)
{
    if (F[x] == x)
        return;
    int fa = F[x];
    int zu = F[F[x]];
    if (R[fa] == x)//是右
    {
        R[fa] = L[x];
        F[L[x]] = fa;
        L[x] = fa;
        F[fa] = x;
        if (zu != fa)//父亲not是root
        {
            if (fa == R[zu])
                R[zu] = x;
            else
                L[zu] = x;
            F[x] = zu;
        }
        else
        {
            F[x] = x;
            root = x;
        }
    }
    else
    {
        L[fa] = R[x];
        F[R[x]] = fa;
        R[x] = fa;
        F[fa] = x;
        if (zu != fa)//父亲not是Loot
        {
            if (fa == L[zu])
                L[zu] = x;
            else
                R[zu] = x;
            F[x] = zu;
        }
        else
        {
            F[x] = x;
            root = x;
        }
    }
}
void output(int t)
{
    if (L[t] > 0)
    {
        output(L[t]);
    }
    if (t != 0)
        printf("%d\n", tre[t]);
    if (R[t] > 0)
    {
        output(R[t]);
    }
}
int find(int t,int x)
{
    if (x == tre[t])
        return t;
    if (R[t]>0 &&x > tre[t])
        return find(R[t], x);
    return find(L[t], x);
}
void splay(int x)
{
    while (x != root)
    {
        rotate(x);
    }
}
void delete_splay(int t, int x)
{
    splay(find(t,x));
    int del = root;
    if (L[del] > 0 && R[del] > 0)
    {
        int temp = R[del];
        root = R[del];
        while (L[temp]>0)
        {
            temp = L[temp];
        }
        L[temp] = L[del];
        F[L[del]] = temp;
        F[root] = root;
        tre[del] = 0;
        L[del] = R[del] = F[del] = -1;
        return;
    }
    else
    {
        if (L[root] > 0)
        {
            root = L[root];
            F[root] = root;tre[del] = 0;
            L[del] = R[del] = F[del] = -1;
            return;
        }
        else if(R[root]>0)
        {
            root = R[root];
            F[root] = root;tre[del] = 0;
            L[del] = R[del] = F[del] = -1;
        }
        else
        {
            clear();
        }
    }
}


int main()
{
    clear();
    int n;
    scanf("%d", &n);
    tot = 0;
    root = 0;
    for (int i = 0; i < n; i++)
    {
        int a;
        scanf("%d", &a);
        insert(root, a);
        splay(tot);
    }
    delete_splay(root, 2);
    output(root);
}
/*
9
1 2 4 3 7 6 5 9 8
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值