跳到正文

个人记录

[Update] Splay 指標 + 模板類(正體中文)

Vanadianfan约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 12 分钟

版本

  1. v1当前

Powered by Vanadianfan.

正體中文再版序

這篇文章是筆者當年頗為得意之作,今歷經兩年有餘學成傳統漢字,偷閒作成其譯作。

恕不保證本文所有的詞彙均能正確地轉換為正體中文道地表達。


背景

不久之前,蒟蒻寫了一篇題為「好用好理解的基礎Splay模板教程」的教程。

幾個月過去了,蒟蒻自學了一些便捷的文法,便把珍藏的 Splay\texttt{Splay} 模板拿出來修改了一下。

介紹

目前該 template + class + -> 的 Splay\texttt{Splay} 資料結構,支持任意已有明確小於定義的資料型態,例如可以與 string 、class Bign {}; (封裝高精度操作的類別) 等任意型態直接連用。

type 為模板型態名,外部提供調用的函式如下:

  • size_t size() :返回節點總數。
  • void Clear() :清除所有節點。
  • void OrderDisplay() :以升序輸出所有節點的鍵值。
  • size_t CalculateRank(val) :返回鍵值為 val 的節點的排名。
  • Node *RankLocate(k) :返回排名為 k 的節點。
  • Node *ForewardNode(val) :返回鍵值為 val 的節點的前驅節點。
  • Node *BackwardNode(val) :返回鍵值為 val 的節點的後繼節點。
  • type ForwardValue(val) :返回鍵值為 val 的節點的前驅節點的鍵值。
  • type BackwardValue(val) :返回鍵值為 val 的節點的後繼節點的鍵值。
  • void Insert(val) :無論是否已存在相同鍵值的節點,插入節點 val 。
  • void EraseViaValue(val) :若鍵值為 val 的節點存在,刪去 1\texttt{1} 個。

「前驅」嚴格定義為鍵值比參數鍵值小的節點;「後繼」則反之。

C++
#include <cstring>
#include <iostream>
using namespace std;

#define MAXN 100023
#define trial (val < cur->key ? cur->lst : cur->rst)

template <typename type>
class SplayTree {
private:
    type pINF, nINF;  // positive infinity, negative infinity.
	struct Node {
		type key;
        size_t weight, size;
		Node *lst, *rst, *pre;
		bool isLsubT(){return this == this->pre->lst;}
		void InfoUpdate(){this->size = lst->size + rst->size + this->weight;}
	};
	Node MemoryPool[MAXN];
	Node *root, *tail, *null;
	Node *bin[MAXN]; int bin_top;
	Node *NewNode(type key){
		Node *x = bin_top ? bin[--bin_top] : tail++;
		x->key = key;
		x->weight = x->size = 1;
		x->pre = x->lst = x->rst = null;
		return x;
	}
    void PointerPrepare(void){
		tail = MemoryPool;
		null = tail++;
		null->weight = null->size;
		null->lst = null->rst = null;
		root = null, bin_top = 0;
    }

	void Midorder(Node *cur){
		if (cur == null) return;
		if (cur->key == nINF || cur->key == pINF) return;
		Midorder(cur->lst);
		for(int i = 0; i < cur->weight; ++i)
			cout << cur->key << endl;
		Midorder(cur->rst);
	}
	void Clear(Node *x){
		if (x == null) return;
		Clear(x->lst);
		Clear(x->rst);
		bin[bin_top++] = x;
	}

protected:
	void Rotate(Node *x){
		Node *y = x->pre, *z = y->pre;
		bool k = x->isLsubT();
		if (y->isLsubT()) z->lst = x; else z->rst = x;
		x->pre = z;
		if (k) y->lst = x->rst, x->rst->pre = y;
		else y->rst = x->lst, x->lst->pre = y;
		if (k) x->rst = y; else x->lst = y;
		y->pre = x; y->InfoUpdate();
	}
	void Splay(Node *x, Node *ter){
		while (x->pre != ter){
			Node *y = x->pre;
			if (y->pre != ter)
				Rotate(x->isLsubT() == y->isLsubT() ? y : x);
			Rotate(x);
		}
		x->InfoUpdate();
		if (ter == null) root = x;
	}
	void Search(type val){
		if (root == null) return;
		Node *cur = root;
		while (val != cur->key){
			Node *Next = trial;
			if (Next == null) break;
			cur = Next;
		}
		Splay(cur, null);
	}

public:
	SplayTree(void){  // default to int or long long int.
        PointerPrepare();
        memset(&pINF, 0x3f, sizeof(pINF));
        memset(&nINF, -0x3f, sizeof(nINF));
        Insert(pINF), Insert(nINF);
	}
    SplayTree(type _MIN, type _MAX) : pINF(_MAX), nINF(_MIN) {  // for other type, custom limits.
        PointerPrepare();
        Insert(pINF), Insert(nINF);
    }

	size_t size(void){
		return root->size - 2;
	}
	void Clear(void){
		Clear(root); root = null;
		Insert(nINF), Insert(pINF);
	}
	void OrderDisplay(void){
		Midorder(root);
		cout << endl;
	}

	size_t CalculateRank(type val){
		Search(val);
		return root->lst->size;
	}
	Node *RankLocate(size_t k){
		++k;
		Node *cur = root;
		while (true){
			if (k <= cur->lst->size) cur = cur->lst;
			else if (k > cur->lst->size + cur->weight){
				k -= cur->lst->size + cur->weight;
				cur = cur->rst;
			}
			else return cur;
		}
	}

	Node *ForewardNode(type val){
		Search(val);
		if (root->key < val) return root;
		Node *cur = root->lst;
		while (cur->rst != null) cur = cur->rst;
		return cur;
	}
	type ForwardValue(type val){
		Search(val);
		if (root->key < val) return root->key;
		Node *cur = root->lst;
		while (cur->rst != null) cur = cur->rst;
		return cur->key;
	}
	Node *BackwardNode(type val){
		Search(val);
		if (root->key > val) return root;
		Node *cur = root->rst;
		while (cur->lst != null) cur = cur->lst;
		return cur;
	}
	type BackwardValue(type val){
		Search(val);
		if (root->key > val) return root->key;
		Node *cur = root->rst;
		while (cur->lst != null) cur = cur->lst;
		return cur->key;
	}

	void Insert(type val){
		Node *cur = root, *f = null;
		while (cur != null && val != cur->key)
			f = cur, cur = trial;
		if (cur != null) ++cur->weight;
		else {
			cur = NewNode(val);
			if (f != null){
				val > f->key ? f->rst : f->lst = cur;
				cur->pre = f;
			}
		}
		Splay(cur, null);
	}
	void EraseViaValue(type val){
		Node *lst = ForewardNode(val);
        Node *nxt = BackwardNode(val);
		Splay(lst, null), Splay(nxt, lst);
		Node *cur = nxt->lst;
		if (cur->weight > 1){
			cur->weight--;
			Splay(cur, null);
		}
		else {
			Clear(nxt->lst); // or " bin[bin_top++] = nxt->lst; "
			nxt->lst = null;
			nxt->size--, lst->size--;
		}
	}
};

#undef MAXN
#undef trial

SplayTree<int> SPT;  // 使用時直接以「< >」的形式聲明你所需類型的平衡樹。
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int m; cin >> m;
	for (; m; m--){
		int op, x; cin >> op >> x;
		if (op == 1) SPT.Insert(x);
		if (op == 2) SPT.EraseViaValue(x);
		if (op == 3) cout << SPT.CalculateRank(x) << "\n";
		if (op == 4) cout << SPT.RankLocate(x)->key << "\n";
		if (op == 5) cout << SPT.ForwardValue(x) << "\n";
		if (op == 6) cout << SPT.BackwardValue(x) << "\n";
		// SPT.OrderDisplay();
	}
	return 0;
}

使用說明

可以複製到新的 .h 文件中進行調用。

聲明平衡樹時使用 < > 描述所需類型即可。(已在代碼中註明)`

前篇部落格我們提到了用提前插入最大值和最小值的方式避免邊界問題。

如果是 int 或者 long long 等整數型態,若不給生成函式傳入變數,默認用將每個字節賦為 0x3f。

如果不是整型或想要自訂,請注意依次傳入最小值和最大值,例如 string 可以寫作

C++
SplayTree<string> SPT("aaaaaaaaaaaaa", "zzzzzzzzzzzzz");

總之具體視情況而言。

為了防止不熟悉 template 的同學不太理解,再舉一例

C++
SplayTree<long long> SPT;
SplayTree<long long> SPT(-998244353, 998244353);  // 因 long long 是整型,兩者均可以。
SplayTree<string> SPT("A", "Z");  // 最小值和最大值由你來決定。

結尾

作者蒟蒻,如有錯誤請多多指教。

2019.11.13 結尾撒花

原文