跳到正文

个人记录

AA树实现(允许重复)

elsc约 13 分钟
原文
文章信息与版本

文章信息

发布
2024年2月5日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 13 分钟
获赞
2

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;

#define endl ('\n')
#define inline __attribute__((always_inline))

template<class Key, class Comparator = less<Key>>
class TreeSets {
private:
	struct Node {
		Key key;
		unsigned int level;
		size_t size;
		Node * left;
		Node * right;
		size_t count;
	};
	Node * root;
	Comparator compare;
	
public:
	TreeSets() : root(nullptr), compare(Comparator()) {}
	~TreeSets() {clear();}
	inline bool empty() const {return root == nullptr;}
	inline size_t size() const {return size(root);}
	inline size_t count(Key k) const {return count(root, k);}
	inline void clear() {clear(root); root = nullptr;}
	inline void insert(Key k) {insert(root, k);}
	inline void erase(Key k) {erase(root, k);}
	inline Key eraseMin() {Key k = min(); erase(k); return k;}
	inline Key eraseMax() {Key k = max(); erase(k); return k;}
	inline unsigned int rank(Key k) const {return rank(root, k, 1);}
	inline Key select(unsigned int x) const {return select(root, x - 1)->key;}
	inline Key predecessor(Key x) const {return predecessor(root, x)->key;}
	inline Key successor(Key x) const {return successor(root, x)->key;}
	inline Key min() const {return min(root);}
	inline Key max() const {return max(root);}
	inline bool contains(Key k) const {return contains(root, k);}
	inline void print(ostream & out = cout) const {print(out, root);}
	friend ostream & operator<<(ostream & cout, const TreeSets<Key, Comparator>& T) {T.print(cout); return cout;}
	
private:
	inline size_t size(Node * h) const {return (h == nullptr ? 0 : h->size);}
	size_t count(Node * h, Key k) const {
		if (h == nullptr) return 0;
		else if (compare(k, h->key)) return count(h->left, k);
		else if (compare(h->key, k)) return count(h->right, k);
		else return h->count;
	}
	
	void clear(Node * h) {
		if (h != nullptr) {
			clear(h->left);
			clear(h->right);
			delete h;
		}
	}
	
	inline bool needRotateLeft(Node * h) {
		return (h != nullptr && h->right != nullptr && h->right->right != nullptr && h->level == h->right->right->level);
	}
	inline bool needRotateRight(Node * h) {
		return (h != nullptr && h->left != nullptr && h->level == h->left->level);
	}
	
	inline void rotateLeft(Node * & h) {
		if (needRotateLeft(h)) {
			Node * x = h->right;
			h->right = x->left;
			x->left = h;
			x->size = h->size;
			h->size = h->count + size(h->left) + size(h->right);
			h = x;
			++(h->level);
		}
	}
	
	inline void rotateRight(Node * & h) {
		if (needRotateRight(h)) {
			Node * x = h->left;
			h->left = x->right;
			x->right = h;
			x->size = h->size;
			h->size = h->count + size(h->left) + size(h->right);
			h = x;
		}
	}
	
	void insert(Node * & h, Key k) {
		if (h == nullptr) {h = new Node({k, 1, 1, nullptr, nullptr, 1}); return;}
		if (compare(k, h->key)) insert(h->left, k); 
		else if (compare(h->key, k)) insert(h->right, k);
		else ++(h->count);
		++(h->size);
		rotateRight(h);
		rotateLeft(h);
	}
	
	inline void maintain(Node * & h) {
		Node * l = h->left, * r = h->right;
		unsigned int lvl0 = (l == nullptr ? 0 : l->level), lvl1 = (r == nullptr ? 0 : r->level);
		if (lvl0 < h->level - 1 || lvl1 < h->level - 1) {
			--(h->level);
			if (r != nullptr && lvl1 > h->level) --(r->level);
			rotateRight(h); if (h->right != nullptr) {rotateRight(h->right); rotateRight(h->right->right);}
			rotateLeft(h); rotateLeft(h->right);
		}
	}
	
	void erase_succ(Node * & h) {
		if (h->left == nullptr) {
			Node * x = h;
			h = h->right;
			delete x;
		} else {
			erase_succ(h->left);
			h->size = h->count + size(h->left) + size(h->right);
			maintain(h);
		}
	}
	
	bool erase(Node * & h, Key k) {
		if (h == nullptr) throw "The key doesn't exist.";
		bool b0 = false;
		if (compare(k, h->key)) b0 = erase(h->left, k);
		else if (compare(h->key, k)) b0 = erase(h->right, k);
		else if (h->count > 1) {--(h->size); --(h->count); return false;}
		else if (h->left == nullptr || h->right == nullptr) {
			Node * x = h;
			h = (h->left == nullptr ? h->right : h->left);
			delete x;
			return true;
		} else {
			Node * x = h->right;
			while (x->left != nullptr) x = x->left;
			h->key = x->key; h->count = x->count;
			erase_succ(h->right);
		}
		--(h->size);
		if (b0) maintain(h);
		return b0;
	}
	
	unsigned int rank(Node * h, Key k, unsigned int r) const {
		if (h == nullptr) return r;
		else if (compare(k, h->key)) return rank(h->left, k, r);
		else if (compare(h->key, k)) return rank(h->right, k, r + size(h->left) + h->count);
		else return r + size(h->left);
	}
	
	Node * select(Node * h, unsigned int x) const {
		if (h == nullptr) return nullptr;
		size_t t = size(h->left), cnt = h->count;
		if (x < t) return select(h->left, x);
		else if (x < t + cnt) return h;
		else return select(h->right, x - t - cnt);
	}
	
	Node * predecessor(Node * h, Key k) const {
		if (h == nullptr) return nullptr;
		else if (compare(h->key, k)) {
			Node * t = predecessor(h->right, k);
			return (t == nullptr ? h : t);
		}
		else return predecessor(h->left, k);
	}
	
	Node * successor(Node * h, Key k) const {
		if (h == nullptr) return nullptr;
		else if (compare(k, h->key)) {
			Node * t = successor(h->left, k);
			return (t == nullptr ? h : t);
		}
		else return successor(h->right, k);
	}
	
	bool contains(Node * h, Key k) const {
		if (h == nullptr) return false;
		else if (compare(k, h->key)) return contains(h->left, k);
		else if (compare(h->key, k)) return contains(h->right, k);
		else return true;
	}
	
	inline Key min(Node * h) const {
		while (h->left != nullptr) h = h->left;
		return h->key;
	}
	
	inline Key max(Node * h) const {
		while (h->right != nullptr) h = h->right;
		return h->key;
	}
	
	void print(ostream & cout, Node * h) const {
		if (h == nullptr) cout << "()";
		else if (h->left == nullptr && h->right == nullptr) cout << '(' << h->key << '#' << h->size << '#' << h->count << ')';
		else {
			cout << '(' << h->key << '#' << h->size << '#' << h->count << ' '; print(cout, h->left); cout << ' '; print(cout, h->right); cout << ')';
		}
	}
};

TreeSets<int> T;
int n;
int op, x;

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	while (cin >> op >> x) {
		switch (op) {
		case 0:
			cout << T.count(x) << endl;
			break;
		case 1:
			T.insert(x);
			break;
		case 2:
			T.erase(x);
			break;
		case 3:
			cout << T.rank(x) << endl;
			break;
		case 4:
			cout << T.select(x) << endl;
			break;
		case 5:
			cout << T.predecessor(x) << endl;
			break;
		case 6:
			cout << T.successor(x) << endl;
			break;
		default:
			cout << T << endl;
			break;
		}
	}
	return 0;
}
原文
存档评论4 条抓取于