跳到正文

算法·理论

各种模版

xuziheng0905约 26 分钟
原文
文章信息与版本

文章信息

发布
2026年4月29日
收录
2026年4月29日
更新
2026年10月1日
篇幅
约 26 分钟

版本

  1. v2当前
  2. v1

二叉搜索树 BST(Binary Search Tree)


C++
namespace Tree{
	template<typename _Tp>
		class BST{
		private:
			struct node{
				node *left, *right;
				_Tp value;
				int cnt;
				node(const _Tp &_v = _Tp()) : left(nullptr), right(nullptr), value(_v), cnt(1) {}
			};
			static_assert(
    			std::is_convertible<decltype(
    				std::declval<_Tp>() < std::declval<_Tp>()
    			), bool>::value,
				"The template type must support the operator< comparison."
			);
			node *root;
			inline void InlinePreorder(node *rt, const char* pattern) const{  // 内置先序遍历 
				if (rt == nullptr) return;
				printf(pattern, rt -> value);
				InlinePreorder(rt -> left, pattern);
				InlinePreorder(rt -> right, pattern);
			}
			inline void InlineInorder(node *rt, const char* pattern) const{  // 内置中序遍历 
				if (rt == nullptr) return;
				InlineInorder(rt -> left, pattern);
				printf(pattern, rt -> value);
				InlineInorder(rt -> right, pattern);
			}
			inline void InlinePostorder(node *rt, const char* pattern) const{  // 内置后序遍历 
				if (rt == nullptr) return;
				InlinePostorder(rt -> left, pattern);
				InlinePostorder(rt -> right, pattern);
				printf(pattern, rt -> value);
			}
			inline node* InlineFind(const _Tp &_v) const{ // 内置查找 
				node *u = root;
				while (u != nullptr)
					if (_v < u -> value) u = u -> left;
					  else if (_v > u -> value) u = u -> right;
						else return u;
				return nullptr;
			}
			inline node* InlinePrecursor(const _Tp &_v) const{  // 内置前驱 
				node *u = InlineFind(_v);
				if (u == nullptr || u -> left == nullptr) return nullptr;
				u = u -> left;
				while (u -> right != nullptr) u = u -> right;
				return u;
			}
			inline node* InlineSuccessor(const _Tp &_v) const{  // 内置后继 
				node *u = InlineFind(_v);
				if (u == nullptr || u -> right == nullptr) return nullptr;
				u = u -> right;
				while (u -> left != nullptr) u = u -> left;
				return u;
			}

		public:
			BST() : root(nullptr) {}
			BST(const BST&) = delete;
			BST& operator=(const BST&) = delete;
			~BST(){
				if (root == nullptr) return;
				queue<node*> q;
				q.push(root);
				while (!q.empty()){
					node *u = q.front();
					q.pop();
					if (u -> left != nullptr) q.push(u -> left);
					if (u -> right != nullptr) q.push(u -> right);
					delete u;
				}
				root = nullptr;
			}
			inline void insert(const _Tp &_v){  // 插入 
				if (root == nullptr){
					root = new (std::nothrow) node(_v);
					if (root == nullptr) throw std::bad_alloc();
					return;
				}
				node *u = root, *fa = nullptr;
				while (u != nullptr){
					fa = u;
					if (_v < u -> value) u = u -> left;
					  else if (_v > u -> value) u = u -> right;
						else{
							++(u -> cnt);
							return;
						}
				}
				if (_v < fa -> value){
					fa -> left = new (std::nothrow) node(_v);
					if (fa -> left == nullptr) throw std::bad_alloc();
				}
				else{
					fa -> right = new (std::nothrow) node(_v);
					if (fa -> right == nullptr) throw std::bad_alloc();
				}
			}
			inline bool find(const _Tp &_v) const{ // 公开查找 
				node *u = InlineFind(_v);
				return u != nullptr;
			}
			inline void preorder(const char* pattern) const{  // 公开先序遍历 
				InlinePreorder(root, pattern);
			}
			inline void inorder(const char* pattern) const{  // 公开中序遍历 
				InlineInorder(root, pattern);
			}
			inline void postorder(const char* pattern) const{  // 公开后序遍历 
				InlinePostorder(root, pattern);
			}
			inline std::pair<bool, _Tp> precursor(const _Tp &_v) const{  // 公开前驱 
				node *u = InlinePrecursor(_v);
				if (u == nullptr) return std::make_pair(false, _v);
				return std::make_pair(true, u -> value);
			}
			inline std::pair<bool, _Tp> successor(const _Tp &_v) const{  // 公开后继 
				node *u = InlineSuccessor(_v);
				if (u == nullptr) return std::make_pair(false, _v);
				return std::make_pair(true, u -> value);
			}
			inline std::pair<bool, _Tp> min() const{  //最小值
				node *u = root;
				if (u == nullptr) return std::make_pair(false, _Tp());
				while (u -> left != nullptr) u = u -> left;
				return std::make_pair(true, u -> value);
			}
			inline std::pair<bool, _Tp> max() const{  //最大值
				node *u = root;
				if (u == nullptr) return std::make_pair(false, _Tp());
				while (u -> right != nullptr) u = u -> right;
				return std::make_pair(true, u -> value);
			}
			inline void DeleteNode(const _Tp &_v){ // 删除 
				node *u = root, *fa = nullptr;
				while (u != nullptr){
					fa = u;
					if (_v < u -> value) u = u -> left;
					  else if (_v > u -> value) u = u -> right;
						else break;
				}
				if (u == nullptr) return; 
				if (u -> cnt > 1){
					--(u -> cnt);
					return;
				}
				if (u -> left == nullptr && u -> right == nullptr){
					if (fa == nullptr){
						delete root;
						root = nullptr;
						return;
					}
					else if (fa -> left == u) fa -> left = nullptr;
					  else fa -> right = nullptr;
					delete u;
					return;
				}
				if (u -> left == nullptr){
					if (fa == nullptr) root = u -> right;
					  else if (fa -> left == u) fa -> left = u -> right;
						else fa -> right = u -> right;
					delete u;
					return;
				}
				if (u -> right == nullptr){
					if (fa == nullptr) root = u -> left;
					  else if (fa -> left == u) fa -> left = u -> left;
						else fa -> right = u -> left;
					delete u;
					return;
				}
				node *v = u -> left, *f = u;
				while (v -> right != nullptr){
					f = v;
					v = v -> right;
				}
				u -> value = v -> value;
				u -> cnt = v -> cnt;
				if (f == u) u -> left = v -> left;
				  else f -> right = v -> left;
				delete v;
			}
		};
}
using namespace Tree;

树堆 Treap


C++
namespace Tree{
	std::mt19937 __rand(chrono::steady_clock::now().time_since_epoch().count());  //随机函数
	template<typename _Tp,
			 typename cmp1 = std::less<_Tp>,
			 typename cmp2 = std::less<int>> // 支持自定义比较函数
		class Treap{ // 树堆 Treap
		private:
			struct node{
				node *left, *right;
				_Tp value;
				int cnt, size, priority;
				node(const _Tp &_v = _Tp(), const int &_p = __rand()) :
					left(nullptr), right(nullptr), value(_v), cnt(1), size(1), priority(_p) {}
			};
			static_assert(
				std::is_convertible<typename std::result_of<cmp1(_Tp, _Tp)>::type, bool>::value,
				"Cmp1 must be a callable taking two value type parameters and returning bool."
			);
			static_assert(
				std::is_convertible<typename std::result_of<cmp2(int, int)>::type, bool>::value,
				"Cmp2 must be a callable taking two int parameters and returning bool."
			);
			node *root;
			cmp1 cmp_value;
			cmp2 cmp_priority;
			inline void InlinePreorder(node *rt, const char* pattern) const{  // 内置先序遍历 
				if (rt == nullptr) return;
				printf(pattern, rt -> value);
				InlinePreorder(rt -> left, pattern);
				InlinePreorder(rt -> right, pattern);
			}
			inline void InlineInorder(node *rt, const char* pattern) const{  // 内置中序遍历 
				if (rt == nullptr) return;
				InlineInorder(rt -> left, pattern);
				printf(pattern, rt -> value);
				InlineInorder(rt -> right, pattern);
			}
			inline void InlinePostorder(node *rt, const char* pattern) const{  // 内置后序遍历 
				if (rt == nullptr) return;
				InlinePostorder(rt -> left, pattern);
				InlinePostorder(rt -> right, pattern);
				printf(pattern, rt -> value);
			}
			inline node* InlineFind(const _Tp &_v) const{ // 内置查找 
				node *u = root;
				while (u != nullptr)
					if (cmp_value(_v, u -> value)) u = u -> left;
					  else if (cmp_value(u -> value, _v)) u = u -> right;
						else return u;
				return nullptr;
			}
			inline node* InlinePrecursor(const _Tp &_v) const{  // 内置前驱 
				node *u = root, *fa = nullptr;
				while (u != nullptr)
					if (cmp_value(u -> value, _v)){
						fa = u;
						u = u -> right;
					}
					else u = u -> left;
				return fa;
			}
			inline node* InlineSuccessor(const _Tp &_v) const{  // 内置后继 
				node *u = root, *fa = nullptr;
				while (u != nullptr)
					if (cmp_value(_v, u -> value)){
						fa = u;
						u = u -> left;
					}
					else u = u -> right;
				return fa;
			}
			inline void InlineUpdateSize(node *&rt){
				if (rt == nullptr) return;
				rt -> size = rt -> cnt;
				if (rt -> left != nullptr) rt -> size += rt -> left -> size;
				if (rt -> right != nullptr) rt -> size += rt -> right -> size;
			}
			inline void InlineLeftRotate(node *&rt){  // 内置左旋
				node *r = rt -> right;
				rt -> right = r -> left;
				r -> left = rt;
				InlineUpdateSize(rt -> right);
				InlineUpdateSize(rt);
				rt = r;
			}
			inline void InlineRightRotate(node *&rt){  // 内置右旋
				node *l = rt -> left;
				rt -> left = l -> right;
				l -> right = rt;
				InlineUpdateSize(rt -> left);
				InlineUpdateSize(rt);
				rt = l;
			}
			inline void InlineInsert(node *&rt, const _Tp &_v, const int &_p = __rand()){  // 内置插入
				if (rt == nullptr){
					rt = new (std::nothrow) node(_v, _p);
					if (rt == nullptr) throw std::bad_alloc();
					return;
				}
				if (cmp_value(_v, rt -> value)){
					InlineInsert(rt -> left, _v, _p);
					if (cmp_priority(rt -> left -> priority, rt -> priority))
						InlineRightRotate(rt);
				}
				else if (cmp_value(rt -> value, _v)){
					InlineInsert(rt -> right, _v ,_p);
					if (cmp_priority(rt -> right -> priority, rt -> priority))
						InlineLeftRotate(rt);
				}
				else ++(rt -> cnt);
				InlineUpdateSize(rt);
			}
			inline node* InlineDeleteNode(node *&rt, const _Tp &_v){ // 内置删除 
				if (rt == nullptr) return nullptr;
				if (cmp_value(_v, rt -> value)) rt -> left = InlineDeleteNode(rt -> left, _v);
				else if (cmp_value(rt -> value, _v)) rt -> right = InlineDeleteNode(rt -> right, _v);
				else if (rt -> cnt > 1) --(rt -> cnt);
				else if (rt -> left == nullptr && rt -> right == nullptr){
					delete rt;
					return rt = nullptr;
				}
				else if (rt -> left == nullptr || (rt -> right != nullptr && cmp_priority(
					rt -> right -> priority, rt -> left -> priority))){
					InlineLeftRotate(rt);
					rt -> left = InlineDeleteNode(rt -> left, _v);
				}
				else{
					InlineRightRotate(rt);
					rt -> right = InlineDeleteNode(rt -> right, _v);
				}
				InlineUpdateSize(rt);
				return rt;
			}

		public:
			Treap() : root(nullptr) {}
			Treap(const Treap&) = delete;
			Treap& operator=(const Treap&) = delete;
			~Treap(){
				if (root == nullptr) return;
				queue<node*> q;
				q.push(root);
				while (!q.empty()){
					node *u = q.front();
					q.pop();
					if (u -> left != nullptr) q.push(u -> left);
					if (u -> right != nullptr) q.push(u -> right);
					delete u;
				}
				root = nullptr;
			}
			inline bool find(const _Tp &_v) const{ // 公开查找 
				node *u = InlineFind(_v);
				return u != nullptr;
			}
			inline void preorder(const char* pattern) const{  // 公开先序遍历 
				InlinePreorder(root, pattern);
			}
			inline void inorder(const char* pattern) const{  // 公开中序遍历 
				InlineInorder(root, pattern);
			}
			inline void postorder(const char* pattern) const{  // 公开后序遍历 
				InlinePostorder(root, pattern);
			}
			inline std::pair<bool, _Tp> precursor(const _Tp &_v) const{  // 公开前驱 
				node *u = InlinePrecursor(_v);
				if (u == nullptr) return std::make_pair(false, _v);
				return std::make_pair(true, u -> value);
			}
			inline std::pair<bool, _Tp> successor(const _Tp &_v) const{  // 公开后继 
				node *u = InlineSuccessor(_v);
				if (u == nullptr) return std::make_pair(false, _v);
				return std::make_pair(true, u -> value);
			}
			inline std::pair<bool, _Tp> min() const{  //最小值
				node *u = root;
				if (u == nullptr) return std::make_pair(false, _Tp());
				while (u -> left != nullptr) u = u -> left;
				return std::make_pair(true, u -> value);
			}
			inline std::pair<bool, _Tp> max() const{  //最大值
				node *u = root;
				if (u == nullptr) return std::make_pair(false, _Tp());
				while (u -> right != nullptr) u = u -> right;
				return std::make_pair(true, u -> value);
			}
			inline void insert(const _Tp &_v, const int &_p = __rand()){  // 公开插入 
				InlineInsert(root, _v, _p);
			}
			inline void DeleteNode(const _Tp &_v){ // 公开删除 
				InlineDeleteNode(root, _v);
			}
			inline int size() const{ // 公开大小
				return root != nullptr ? root -> size : 0;
			}
			inline std::pair<bool, int> ValueToRank(const _Tp &_v) const{ // 知值查排名
				node *u = root;
				int ans = 0;
				while (u != nullptr)
					if (cmp_value(_v, u -> value)){
						u = u -> left;
					}
					else if (cmp_value(u -> value, _v)){
						ans += u -> cnt;
						if (u -> left != nullptr) ans += u -> left -> size;
						u = u -> right;
					}
					else{
						if (u -> left != nullptr) ans += u -> left -> size;
						return std::make_pair(true, ans + 1);
					}
				return std::make_pair(false, ans + 1);
			}
			inline std::pair<bool, _Tp> RankToValue(int _r) const{ // 知排名查值
				node *u = root;
				int sz = 0;
				--_r;
				while (u != nullptr){
					if (u -> left == nullptr) sz = 0;
					  else sz = u -> left -> size;
					if (_r < sz) u = u -> left;
					else if (_r < sz + u -> cnt) return std::make_pair(true, u -> value);
					else{
						_r -= sz + u -> cnt;
						u = u -> right;
					}
				}
				return std::make_pair(false, _Tp());
			}
		};
}
using namespace Tree;
原文