Balanced Trees
__gnu_pbds::tree
Attached: Official Documentation Link
1 2 3 4 5 6 | |
Template Parameters
Key: The type of elements stored. To store multiple identicalKeyelements, you need to use methods similar tostd::pairandstruct, and use thelower_boundandupper_boundmember functions for searching.Mapped: The mapping rule (Mapped-Policy) type. To indicate that the associative container is a set, similar to storing elements instd::set, fill innull_typehere (for olderg++versions, usenull_mapped_type). To indicate that the associative container is a set with values, similar to storing elements instd::map, fill in aValuetype likestd::map<Key, Value>.Cmp_Fn: The key comparison functor, e.g.,std::less<Key>.Tag: Selects the underlying data structure type. The default isrb_tree_tag.__gnu_pbdsprovides three different balanced trees:rb_tree_tag: Red-black tree. Generally use this one; the performance of the other two is usually not as good as the red-black tree.splay_tree_tag: Splay tree.ov_tree_tag: Ordered vector tree. It's just an ordered structure implemented byvector, similar to using a sortedvectorto implement a balanced tree. Performance depends on whether the data tries to exploit its weaknesses.
Node_Update: The policy for updating nodes. The default isnull_node_update. To use theorder_of_keyandfind_by_ordermethods, you need to usetree_order_statistics_node_update.Allocator: The memory allocator type.
Construction
1 2 3 4 | |
Member Functions
insert(x): Inserts an elementxinto the tree. Returnsstd::pair<point_iterator, bool>, where the first element is an iterator pointing to the insertion position, and the second element indicates whether the insertion was successful.erase(x): Deletes an element/iteratorxfrom the tree. Ifxis an iterator, it returns an iterator pointing to the element afterx(returnsend()ifxisend()). Ifxis aKey, it returns whether the deletion was successful (fails if the element does not exist).order_of_key(x): Returns the number of elements strictly less thanx(usingCmp_Fnas the comparison logic), i.e., the rank starting from \(0\).find_by_order(x): Returns the iterator corresponding to the element with the rankxaccording to theCmp_Fncomparison.lower_bound(x): Returns an iterator pointing to the first element not less thanx(usingCmp_Fnas the comparison logic).upper_bound(x): Returns an iterator pointing to the first element strictly greater thanx(usingCmp_Fnas the comparison logic).join(x): Merges treexinto the current tree. Treexis cleared (must ensure the comparison functions and element types of both trees are identical).split(x,b): UsingCmp_Fnfor comparison, elements less than or equal toxbelong to the current tree, and the rest belong to treeb.empty(): Returns whether the tree is empty.size(): Returns the size.
Note
The join(x) function requires that the key value range of the tree being merged and the key value range of the tree being merged into are disjoint (i.e., all values in the merging tree must be either all greater than or all less than all values in the current tree). Otherwise, a join_error exception will be thrown.
To merge two trees with overlapping value ranges, you need to insert the elements of one tree one by one into the other tree.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
References
- Tree-Based Containers
- Implementation of
joinfunction in GCC 14.1.0 - Implementation of
erasefunction in GCC 14.1.0
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用