|
BDE 4.39.x Production Release
|
#include <bslalg_rbtreeutil.h>
This struct provides a namespace for a suite of utility functions that operate on elements of type RbTreeNode.
Each method of this class, other than copyTree, provides the no-throw exception guarantee if the client-supplied comparator provides the no-throw guarantee, and provides the strong guarantee otherwise (see bsldoc_glossary ). copyTree provides the strong guarantee.
Static Public Member Functions | |
| static const RbTreeNode * | leftmost (const RbTreeNode *subtree) |
| static RbTreeNode * | leftmost (RbTreeNode *subtree) |
| static const RbTreeNode * | rightmost (const RbTreeNode *subtree) |
| static RbTreeNode * | rightmost (RbTreeNode *subtree) |
| static const RbTreeNode * | next (const RbTreeNode *node) |
| static RbTreeNode * | next (RbTreeNode *node) |
| static const RbTreeNode * | previous (const RbTreeNode *node) |
| static RbTreeNode * | previous (RbTreeNode *node) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static const RbTreeNode * | find (const RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static RbTreeNode * | find (RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static const RbTreeNode * | lowerBound (const RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static RbTreeNode * | lowerBound (RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static const RbTreeNode * | upperBound (const RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static RbTreeNode * | upperBound (RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class FACTORY > | |
| static void | copyTree (RbTreeAnchor *result, const RbTreeAnchor &original, FACTORY *nodeFactory) |
| template<class FACTORY > | |
| static void | moveTree (RbTreeAnchor *result, RbTreeAnchor *original, FACTORY *nodeFactory, FACTORY *originalNodeFactory) |
| template<class FACTORY > | |
| static void | deleteTree (RbTreeAnchor *tree, FACTORY *nodeFactory) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static RbTreeNode * | findInsertLocation (bool *insertAsLeftChildFlag, RbTreeAnchor *tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static RbTreeNode * | findInsertLocation (bool *insertAsLeftChildFlag, RbTreeAnchor *tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value, RbTreeNode *hint) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static RbTreeNode * | findUniqueInsertLocation (int *comparisonResult, RbTreeAnchor *tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value) |
| template<class NODE_VALUE_COMPARATOR , class VALUE > | |
| static RbTreeNode * | findUniqueInsertLocation (int *comparisonResult, RbTreeAnchor *tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value, RbTreeNode *hint) |
| template<class NODE_COMPARATOR > | |
| static void | insert (RbTreeAnchor *tree, const NODE_COMPARATOR &comparator, RbTreeNode *newNode) |
| static void | insertAt (RbTreeAnchor *tree, RbTreeNode *parentNode, bool leftChildFlag, RbTreeNode *newNode) |
| static void | remove (RbTreeAnchor *tree, RbTreeNode *node) |
| static void | swap (RbTreeAnchor *a, RbTreeAnchor *b) |
| static bool | isLeftChild (const RbTreeNode *node) |
| static bool | isRightChild (const RbTreeNode *node) |
| static void | rotateLeft (RbTreeNode *node) |
| static void | rotateRight (RbTreeNode *node) |
| static void | printTreeStructure (FILE *file, const RbTreeNode *subtree, void(*printNodeValueCallback)(FILE *, const RbTreeNode *), int level=0, int spacesPerLevel=4) |
| template<class NODE_COMPARATOR > | |
| static int | validateRbTree (const RbTreeNode *rootNode, const NODE_COMPARATOR &comparator) |
| template<class NODE_COMPARATOR > | |
| static int | validateRbTree (const RbTreeNode **errorNode, const char **errorDescription, const RbTreeNode *rootNode, const NODE_COMPARATOR &comparator) |
| template<class NODE_COMPARATOR > | |
| static bool | isWellFormed (const RbTreeAnchor &tree, const NODE_COMPARATOR &comparator) |
|
static |
Load, into the specified result, a collection of newly created nodes having the same red-black tree structure as that of the specified original tree, where each node in the returned tree is created by invoking nodeFactory->createNode on the corresponding original node; if an exception occurs, use nodeFactory->deleteNode to destroy any newly created nodes, and propagate the exception to the caller (i.e., this operation provides the strong exception guarantee). FACTORY shall be a class providing two methods that can be called as if they had the following signatures:
result is an empty tree, original is a well-formed (see isWellFormed), and nodeFactory->deleteNode does not throw.
|
static |
Call nodeFactory->deleteNode on each node in tree and reset tree to an empty state. FACTORY shall be a class providing a method that can be called as if it has the following signature:
tree is a valid binary tree, and nodeFactory->deleteNode does not throw.
|
inlinestatic |
Return the address of the leftmost node holding the specified value in the specified tree (organized according to the specified comparator) if found, and returntree.sentinel()' otherwise. COMPARATOR shall be a functor providing two methods that can be called as if they had the following signatures:
comparator provides a strict weak ordering on objects of type VALUE, and tree is well-formed (see isWellFormed).
|
inlinestatic |
|
static |
Return the address of the node that would be the parent a node holding the specified value, if it were to be inserted into the specified tree (organized according to the specified comparator), and load, into the specified insertAsLeftChildFlag, true if value would be held as the returned node's left child, and false if value would be held in its right child, unless tree is empty, in which case return tree->sentinel() and load true into insertAsLeftChildFlag. Optionally specify a hint, suggesting a node in tree that might be the immediate successor of a node holding value if it were to be inserted into tree. If the supplied hint is the successor, this operation will take amortized constant time; otherwise, it will take O(log(N)) operations, where N is the number of nodes in the tree. If a node holding value is inserted as suggested by this method, the resulting tree will be an ordered binary tree, but may require rebalancing (and re-coloring) to again be a valid red-black tree. COMPARATOR shall be a functor providing two methods that can be called as if they have the following signatures:
comparator provides a strict weak ordering on objects of type VALUE, tree is well-formed (see isWellFormed), and hint, if supplied, is a node in tree.insertAt method.
|
static |
|
static |
Return the address of the node holding the specified value in the specified tree (organized according to the specified comparator) if found, and the address of the node that would be the parent for value otherwise; load, into the specified comparisonResult, 0 if value is found, a negative number if value would be held in its left child, and a positive number if value would be held in its right child, unless tree is empty, in which case load a negative number into comparisonResult and return tree->sentinel(). Optionally specify a hint, suggesting a node in tree that might be the immediate successor of a node holding value if it were to be inserted into tree. If the supplied hint is the successor, this operation will take amortized constant time; otherwise, it will take O(log(N)) operations, where N is the number of nodes in the tree. If a node holding value is inserted as suggested by this method, the resulting tree will be an ordered binary tree, but may require rebalancing (and re-coloring) to again be a valid red-black tree. COMPARATOR shall be a functor providing two methods that can be called as if they have the following signatures:
comparator provides a strict weak ordering on objects of type VALUE, tree is well-formed (see isWellFormed), and hint, if supplied, is a node in tree.insertAt method.
|
static |
|
static |
Insert the specified newNode into the specified tree, organized according to the specified comparator. The resulting tree will be well-formed (see isWellFormed). NODE_COMPARATOR shall be a functor providing a method that can be called as if it had the following signatures:
comparator provides a strict weak ordering on objects of type VALUE, and tree is well-formed (see isWellFormed).
|
static |
Insert the specified newNode into the specified tree as either the left or right child of the specified parentNode, as indicated by the specified leftChildFlag, and then rebalance the tree so that it is a valid red-black tree (see validateRbTree).
tree is well-formed (see isWellFormed), and, if tree is empty, parentNode is tree->sentinel() and leftChildFlag is true, or, if tree is not empty, parentNode is a node in tree whose left or right child (as indicated by leftChildFlag) is 0 where if newNode were attached as that child (without rebalancing) tree would still form an ordered binary tree (though not necessarily a valid red-black tree). findInsertLocation or findUniqueInsertLocation methods.
|
inlinestatic |
Return true if the specified node is the left child of its parent, and false otherwise.
0 != node->parent().
|
inlinestatic |
Return true if the specified node is the left child of its parent, and false otherwise.
0 != node->parent().
|
inlinestatic |
Return true if the specified tree is well-formed and refers to a valid red-black tree, and false otherwise. For a RbTreeAnchor to be considered well-formed all of the following must be true:
tree.rootNode() must refer to a valid red-black tree, whose nodes are organized according to comparator (see validateRbTree).tree.firstNode() must refer to tree.sentinel() if tree.rootNode() is 0, and leftmost(tree.rootNode())' otherwise.tree.nodeCount() must be the count of nodes in tree (not including the sentinel node).tree.sentinel()->leftchild() is tree.rootNode(), and (if tree.rootNode() is not 0) tree.rootNode()->parent() is tree.sentinel().tree.rootNode() is 0 or tree.rootNode().isBlack() is truetree.rootNode() is 0 or refers to a valid binary tree. tree, it is intended for debugging purposes only. Note also that the final condition, that the root node be either 0 or colored black, is not a canonical requirement of a red-black tree but an additional invariant enforced by the methods of RbTreeUtil to simplify the implementations.
|
static |
Return the address of the leftmost node in the specified subtree, and subtree if subtree has no left child.
0 != subtree, and subtree refers to a valid binary tree. subtree (as determined by the comparator used to organize the red-black subtree data).
|
inlinestatic |
|
inlinestatic |
Return the address of the leftmost node holding the smallest value greater-than or equal-to value in the specified tree (organized according to the specified 'comparator) if found, and return tree.sentinel() if value is greater-than the rightmost node in tree. COMPARATOR shall be a functor providing two methods that can be called as if they had the following signatures:
comparator provides a strict weak ordering on objects of type VALUE, and tree is well-formed (isWellFormed). value could be inserted into tree while preserving its ordering.
|
static |
|
static |
Load into the specified result, using the specified nodeFactory to create and delete nodes, a collection of newly created nodes with the same (red-black) tree structure as that of the specified original tree, which uses the specified originalNodeFactory to create and delete nodes, where the value attribute of each node in result is constructed from explicitly moving the value attribute of the corresponding node in original. original is left in a valid but unspecified state. If an exception occurs, both result and original are left in a valid but unspecified state.
result is an empty tree, original is well-formed (see isWellFormed), and nodeFactory->deleteNode and originalNodeFactory->deleteNode do not throw.
|
static |
Return the address of the node that follows the specified node in an in-order traversal of the binary tree to which node belongs, or the tree's sentinel node if node is the rightmost node in the tree.
node is a member of a valid binary tree, and is not a sentinel node. node.
|
inlinestatic |
|
static |
Return the address of the node that precedes the specified node in an in-order traversal of the binary tree to which node belongs, or the tree's rightmost node if node is the sentinel node of the tree.
node is a non-leftmost member of a valid binary tree or is a sentinel node. node.
|
inlinestatic |
|
static |
Write a description of the structure of the specified subtree to the specified output file in a human-readable format, using the specified printValueCallback to render the value of each node. Optionally specify an initial indentation level, whose absolute value is incremented recursively for nested objects. If level is specified, optionally specify spacesPerLevel, whose absolute value indicates the number of spaces per indentation level for this and all of its nested objects. If level is negative, suppress indentation of the first line. If spacesPerLevel is negative, format the entire output on one line, suppressing all but the initial indentation (as governed by level).
node is 0, or the root of a valid binary tree.
|
static |
Remove the specified node from the specified tree, and then rebalance tree so that it again forms a valid red-black tree (see validateRbTree).
tree is well-formed (see isWellFormed).
|
static |
Return the address of the rightmost node in the specified subtree, and subtree if subtree has no right child.
0 != subtree and subtree refers to a valid binary tree. subtree (as determined by the comparator used to organize the red-black subtree data).
|
inlinestatic |
|
static |
Perform counter-clockwise rotation on the specified node: Rotate the node's right child (the pivot) to be the node's parent, and attach the pivot's left child as the node's right child.
node->rightChild() is not 0, node->parent() is not 0, and node's parent refers to node as one of its children. node. Also note this operation will successfully rotate the root node of an unbalanced, but otherwise well-formed, tree referred to by a RbTreeAnchor object (see isWellFormed) because the parent of the root node is the tree's sentinel node (i.e., not 0), which refers to the root node as its left child, and an RbTreeAnchor object returns the left child of the sentinel node as the root of the tree.
|
static |
Perform clockwise rotation on the specified node: Rotate the node's left child (the pivot) to be the node's parent, and attach the pivot's right child as the node's left child.
node->leftChild() is not 0, node->parent() is not 0, and node's parent refers to node as one of its children. node. Also note this operation will successfully rotate the root node of an unbalanced, but otherwise well-formed, tree referred to by a RbTreeAnchor object (see isWellFormed) because the parent of the root node is the tree's sentinel node (i.e., not 0), which refers to the root node as its left child, and an RbTreeAnchor object returns the left child of the sentinel node as the root of the tree.
|
static |
Efficiently exchange the nodes in the specified a tree with the nodes in the specified b tree. This method provides the no-throw exception-safety guarantee.
a and b are well-formed (see isWellFormed).
|
inlinestatic |
Return the address of the leftmost node holding the smallest value greater-than value in the specified tree (organized according to the specified 'comparator) if found, and return tree.sentinel() if value is greater-than or equal-to the rightmost node in tree. COMPARATOR shall be a functor providing two methods that can be called as if they had the following signatures:
comparator provides a strict weak ordering on objects of type VALUE, and tree is well-formed (isWellFormed). value could be inserted into tree while preserving its ordering.
|
inlinestatic |
|
static |
|
inlinestatic |
Return the (common) number of black nodes on each path from the specified rootNode to a leaf in the tree, 0 if rootNode is 0, and a negative number if rootNode does not refer to a valid red-black binary search tree, ordered according to the specified comparator. Optionally specify errorNode and errorDescription in which to load the address of a node violating a red-black tree constraint and a description of that violation, respectively.
rootNode is 0, or refers to a valid binary tree.Each node of a red-black tree is colored either red or black; null nodes are considered black. Four requirements must be satisfied for rootNode to refer to a valid red-black binary search tree:
comparator), and no descendents to the right of that node would order before it.rootNode is 0, or refers to a valid binary tree. tree, it is intended for debugging purposes only.