版本比较
各种模版
标题
增加 1 行删除 1 行
11BST模版各种模版正文
增加 331 行删除 60 行
121234564567891011789101112131415161718192018212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768246970712772737475767778798081378283394084854386878889902 行未改动93949596549798565799100601011026263103104666768697071721051061071087475767778109110111808182838411211311486878889901151161179293949596971181191201211229910010110212312412512612712812913013110413213313413513613713813914010714114214314414528 行未改动1741751761771441781791801811824 行未改动187188189190157158191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430```namespace Binary_Search_Tree{### 二叉搜索树 BST(Binary Search Tree)---
```cppnamespace Tree{ template<typename _Tp> struct node{ node *left, *right; _Tp value; int cnt; node() : left(nullptr), right(nullptr), value(), cnt(0) {} }; template<typename _Tp> struct BST{ 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<_Tp> *root; 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<_Tp>*> q; queue<node*> q; q.push(root); while (!q.empty()){ node<_Tp> *u = q.front(); 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 (nothrow) node<_Tp>(); root = new (std::nothrow) node(_v); if (root == nullptr) throw std::bad_alloc(); root -> value = _v; root -> cnt = 1; return; } node<_Tp> *u = root, *fa = nullptr; 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;2 行未改动 return; } } if (_v < fa -> value){ fa -> left = new (nothrow) node<_Tp>(); fa -> left = new (std::nothrow) node(_v); if (fa -> left == nullptr) throw std::bad_alloc(); fa -> left -> value = _v; fa -> left -> cnt = 1; } else{ fa -> right = new (nothrow) node<_Tp>(); fa -> right = new (std::nothrow) node(_v); if (fa -> right == nullptr) throw std::bad_alloc(); fa -> right -> value = _v; fa -> right -> cnt = 1; } } inline node<_Tp>* find(const _Tp &_v){ // 查找 node<_Tp> *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 bool find(const _Tp &_v) const{ // 公开查找 node *u = InlineFind(_v); return u != nullptr; } inline void preorder(node<_Tp> *rt, const char* pattern){ // 先序遍历 if (rt == nullptr) return; printf(pattern, rt -> value); preorder(rt -> left, pattern); preorder(rt -> right, pattern); inline void preorder(const char* pattern) const{ // 公开先序遍历 InlinePreorder(root, pattern); } inline void inorder(node<_Tp> *rt, const char* pattern){ // 中序遍历 if (rt == nullptr) return; inorder(rt -> left, pattern); printf(pattern, rt -> value); inorder(rt -> right, pattern); inline void inorder(const char* pattern) const{ // 公开中序遍历 InlineInorder(root, pattern); } inline void postorder(node<_Tp> *rt, const char* pattern){ // 后序遍历 if (rt == nullptr) return; postorder(rt -> left, pattern); postorder(rt -> right, pattern); printf(pattern, rt -> value); inline void postorder(const char* pattern) const{ // 公开后序遍历 InlinePostorder(root, pattern); } inline node<_Tp>* precursor(const _Tp &_v){ // 前驱 node<_Tp> *u = find(_v); if (u == nullptr || u -> left == nullptr) return nullptr; u = u -> left; while (u -> right != nullptr) u = u -> right; return u; 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 node<_Tp>* successor(const _Tp &_v){ // 后继 node<_Tp> *u = find(_v); if (u == nullptr || u -> right == nullptr) return nullptr; u = u -> right; 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 u; 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<_Tp> *u = root, *fa = nullptr; 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;28 行未改动 else fa -> right = u -> left; delete u; return; } node<_Tp> *v = u -> left, *f = u; node *v = u -> left, *f = u; while (v -> right != nullptr){ f = v; v = v -> right; }4 行未改动 delete v; } };}using namespace Binary_Search_Tree;BST<int> tr;using namespace Tree;```
### 树堆 Treap---
```cppnamespace 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;```