版本比较
各种模版
标题
增加 1 行删除 1 行
1BST模版1各种模版
正文
增加 331 行删除 60 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546472 行未改动505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011128 行未改动1401411421431441451461471484 行未改动153154155156157158159```namespace Binary_Search_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{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;BST() : root(nullptr) {}BST(const BST&) = delete;BST& operator=(const BST&) = delete;~BST(){if (root == nullptr) return;queue<node<_Tp>*> q;q.push(root);while (!q.empty()){node<_Tp> *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>();if (root == nullptr) throw std::bad_alloc();root -> value = _v;root -> cnt = 1;return;}node<_Tp> *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>();if (fa -> left == nullptr) throw std::bad_alloc();fa -> left -> value = _v;fa -> left -> cnt = 1;}else{fa -> right = new (nothrow) node<_Tp>();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 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 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 postorder(node<_Tp> *rt, const char* pattern){ // 后序遍历if (rt == nullptr) return;postorder(rt -> left, pattern);postorder(rt -> right, pattern);printf(pattern, rt -> value);}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 node<_Tp>* successor(const _Tp &_v){ // 后继node<_Tp> *u = find(_v);if (u == nullptr || u -> right == nullptr) return nullptr;u = u -> right;while (u -> left != nullptr) u = u -> left;return u;}inline void DeleteNode(const _Tp &_v){ // 删除node<_Tp> *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;while (v -> right != nullptr){f = v;v = v -> right;}4 行未改动delete v;}};}using namespace Binary_Search_Tree;BST<int> tr;```1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889902 行未改动9394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414528 行未改动1741751761771781791801811824 行未改动187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430### 二叉搜索树 BST(Binary Search Tree)---```cppnamespace 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;2 行未改动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;28 行未改动else fa -> right = u -> left;delete u;return;}node *v = u -> left, *f = u;while (v -> right != nullptr){f = v;v = v -> right;}4 行未改动delete v;}};}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{ // 树堆 Treapprivate: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;```