8#ifndef INCLUDED_BSLALG_RBTREEUTIL
9#define INCLUDED_BSLALG_RBTREEUTIL
728#include <bslscm_version.h>
826 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
828 NODE_VALUE_COMPARATOR& comparator,
830 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
832 NODE_VALUE_COMPARATOR& comparator,
852 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
854 NODE_VALUE_COMPARATOR& comparator,
856 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
858 NODE_VALUE_COMPARATOR& comparator,
879 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
881 NODE_VALUE_COMPARATOR& comparator,
883 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
885 NODE_VALUE_COMPARATOR& comparator,
908 template <
class FACTORY>
911 FACTORY *nodeFactory);
926 template <
class FACTORY>
929 FACTORY *nodeFactory,
930 FACTORY *originalNodeFactory);
941 template <
class FACTORY>
973 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
975 bool *insertAsLeftChildFlag,
977 NODE_VALUE_COMPARATOR& comparator,
979 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
981 bool *insertAsLeftChildFlag,
983 NODE_VALUE_COMPARATOR& comparator,
1016 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1018 int *comparisonResult,
1020 NODE_VALUE_COMPARATOR& comparator,
1021 const VALUE& value);
1022 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1024 int *comparisonResult,
1026 NODE_VALUE_COMPARATOR& comparator,
1042 template <
class NODE_COMPARATOR>
1044 const NODE_COMPARATOR& comparator,
1167 void (*printNodeValueCallback)(FILE *,
const RbTreeNode *),
1169 int spacesPerLevel = 4);
1204 template <
class NODE_COMPARATOR>
1206 const NODE_COMPARATOR& comparator);
1207 template <
class NODE_COMPARATOR>
1209 const char **errorDescription,
1211 const NODE_COMPARATOR& comparator);
1240 template <
class NODE_COMPARATOR>
1242 const NODE_COMPARATOR& comparator);
1271 template <
class NODE_COMPARATOR>
1273 const char **errorDescription,
1277 const NODE_COMPARATOR& comparator);
1312template <
class DELETER>
1318 DELETER *d_deleter_p;
1376template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1379 NODE_VALUE_COMPARATOR& comparator,
1386template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1389 NODE_VALUE_COMPARATOR& comparator,
1397 if (lowBound != tree.
sentinel() && !comparator(value, *lowBound)) {
1403template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1405 NODE_VALUE_COMPARATOR& comparator,
1412template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1415 NODE_VALUE_COMPARATOR& comparator,
1421 if (comparator(*node, value)) {
1425 nextLargestNode = node;
1429 return nextLargestNode;
1432template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1435 NODE_VALUE_COMPARATOR& comparator,
1442template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1445 NODE_VALUE_COMPARATOR& comparator,
1451 if (comparator(value, *node)) {
1452 nextLargestNode = node;
1459 return nextLargestNode;
1462template <
class FACTORY>
1465 FACTORY *nodeFactory)
1481 RbTreeNode *copiedRoot = nodeFactory->cloneNode(*originalNode);
1494 originalNode = originalNode->
leftChild();
1495 RbTreeNode *newNode = nodeFactory->cloneNode(*originalNode);
1502 copiedNode = newNode;
1507 RbTreeNode *newNode = nodeFactory->cloneNode(*originalNode);
1514 copiedNode = newNode;
1517 originalNode = originalNode->
parent();
1518 copiedNode = copiedNode->
parent();
1520 }
while (original.
sentinel() != originalNode);
1524 result->
reset(copiedRoot,
1529template <
class FACTORY>
1532 FACTORY *nodeFactory,
1533 FACTORY *originalNodeFactory)
1550 RbTreeNode *copiedRoot = nodeFactory->moveIntoNewNode(originalNode);
1565 originalNode = originalNode->
leftChild();
1566 RbTreeNode *newNode = nodeFactory->moveIntoNewNode(originalNode);
1573 copiedNode = newNode;
1578 RbTreeNode *newNode = nodeFactory->moveIntoNewNode(originalNode);
1585 copiedNode = newNode;
1588 originalNode = originalNode->
parent();
1589 copiedNode = copiedNode->
parent();
1591 }
while (original->
sentinel() != originalNode);
1595 result->
reset(copiedRoot,
1604template <
class FACTORY>
1636 nodeFactory->deleteNode(node);
1639 }
while (tree->
sentinel() != node);
1643template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1645 bool *insertAsLeftChildFlag,
1647 NODE_VALUE_COMPARATOR& comparator,
1655 *insertAsLeftChildFlag =
true;
1660 *insertAsLeftChildFlag = comparator(value, *node);
1661 if (*insertAsLeftChildFlag) {
1671template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1673 bool *insertAsLeftChildFlag,
1675 NODE_VALUE_COMPARATOR& comparator,
1686 if (tree->
sentinel() == hint || !comparator(*hint, value)) {
1691 if (tree->
firstNode() == hint || !comparator(value, *prev)) {
1698 *insertAsLeftChildFlag =
true;
1702 *insertAsLeftChildFlag =
false;
1715template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1717 int *comparisonResult,
1719 NODE_VALUE_COMPARATOR& comparator,
1733 bool leftChild =
true;
1738 leftChild = comparator(value, *node);
1743 nextSmallestNode = node;
1748 if (nextSmallestNode && !comparator(*nextSmallestNode, value)) {
1749 *comparisonResult = 0;
1750 return nextSmallestNode;
1752 *comparisonResult = leftChild ? -1 : 1;
1756template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1758 int *comparisonResult,
1760 NODE_VALUE_COMPARATOR& comparator,
1768 enum { LEFT_CHILD = -1, NODE_FOUND = 0, RIGHT_CHILD = 1 };
1772 if (tree->
sentinel() == hint || comparator(value, *hint)) {
1777 if (tree->
firstNode() == hint || comparator(*prev, value)) {
1784 *comparisonResult = LEFT_CHILD;
1789 *comparisonResult = RIGHT_CHILD;
1798 if (!comparator(value, *prev)) {
1799 *comparisonResult = NODE_FOUND;
1808 else if (tree->
sentinel() != hint && !comparator(*hint, value)) {
1809 *comparisonResult = NODE_FOUND;
1820template <
class NODE_COMPARATOR>
1822 const NODE_COMPARATOR& comparator,
1834 bool leftChildFlag =
true;
1839 leftChildFlag = comparator(*newNode, *node);
1840 if (leftChildFlag) {
1847 return insertAt(tree, parent, leftChildFlag, newNode);
1868template <
class NODE_COMPARATOR>
1871 const NODE_COMPARATOR& comparator)
1874 const char *errorDescription;
1875 return validateRbTree(&errorNode, &errorDescription, rootNode, comparator);
1878template <
class NODE_COMPARATOR>
1880 const char **errorDescription,
1882 const NODE_COMPARATOR& comparator)
1895template <
class NODE_COMPARATOR>
1898 const NODE_COMPARATOR& comparator)
1911template <
class NODE_COMPARATOR>
1914 const char **errorDescription,
1918 const NODE_COMPARATOR& comparator)
1936 enum { INVALID_RBTREE = -1};
1946 if ((minNodeValue && comparator(*rootNode, *minNodeValue)) ||
1947 (maxNodeValue && comparator(*maxNodeValue, *rootNode))) {
1948 *errorNode = rootNode;
1949 *errorDescription =
"Invalid binary search tree.";
1950 return INVALID_RBTREE;
1955 if ((left != 0 || right != 0) && left == right) {
1956 *errorNode = rootNode;
1957 *errorDescription =
"Invalid children";
1958 return INVALID_RBTREE;
1963 if ((left && left->
parent() != rootNode) ||
1964 (right && right->
parent() != rootNode)) {
1965 *errorNode = rootNode;
1966 *errorDescription =
"Invalid parent pointers for children";
1967 return INVALID_RBTREE;
1975 *errorNode = rootNode;
1976 *errorDescription =
"Red node with a red child.";
1977 return INVALID_RBTREE;
1994 if (leftDepth < 0 || rightDepth < 0) {
1995 return INVALID_RBTREE;
2000 if (leftDepth != rightDepth) {
2001 *errorNode = rootNode;
2003 "Black violation (unequal black depth from node to leaves).";
2004 return INVALID_RBTREE;
2016template <
class DELETER>
2021, d_deleter_p(deleter)
2026template <
class DELETER>
2030 if (d_tree_p && d_tree_p->rootNode()) {
2031 d_tree_p->rootNode()->setParent(d_tree_p->sentinel());
2038template <
class DELETER>
Definition bslalg_rbtreeanchor.h:353
RbTreeNode * sentinel()
Definition bslalg_rbtreeanchor.h:537
RbTreeNode * firstNode()
Definition bslalg_rbtreeanchor.h:525
int numNodes() const
Return the numNodes attribute of this object.
Definition bslalg_rbtreeanchor.h:556
RbTreeNode * rootNode()
Definition bslalg_rbtreeanchor.h:531
void reset(RbTreeNode *rootNode, RbTreeNode *firstNode, int numNodes)
Definition bslalg_rbtreeanchor.h:483
Definition bslalg_rbtreenode.h:377
RbTreeNode * rightChild()
Definition bslalg_rbtreenode.h:603
RbTreeNode * leftChild()
Definition bslalg_rbtreenode.h:597
void setRightChild(RbTreeNode *address)
Definition bslalg_rbtreenode.h:557
void setParent(RbTreeNode *address)
Definition bslalg_rbtreenode.h:542
Color color() const
Return the color of this node.
Definition bslalg_rbtreenode.h:640
void setLeftChild(RbTreeNode *address)
Definition bslalg_rbtreenode.h:551
void setColor(Color value)
Set the color of this node to the specified value.
Definition bslalg_rbtreenode.h:563
RbTreeNode * parent()
Definition bslalg_rbtreenode.h:591
@ BSLALG_BLACK
Definition bslalg_rbtreenode.h:383
@ BSLALG_RED
Definition bslalg_rbtreenode.h:382
Definition bslalg_rbtreeutil.h:1313
void release()
Release from management the tree supplied at construction.
Definition bslalg_rbtreeutil.h:2040
~RbTreeUtilTreeProctor()
Definition bslalg_rbtreeutil.h:2028
RbTreeUtilTreeProctor(RbTreeAnchor *tree, DELETER *deleter)
Definition bslalg_rbtreeutil.h:2018
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlc_flathashmap.h:2218
Definition bslalg_rbtreeutil.h:1253
static int validateRbTree(const RbTreeNode **errorNode, const char **errorDescription, const RbTreeNode *rootNode, const RbTreeNode *minNodeValue, const RbTreeNode *maxNodeValue, const NODE_COMPARATOR &comparator)
Definition bslalg_rbtreeutil.h:1912
static bool isWellFormedAnchor(const RbTreeAnchor &tree)
Definition bslalg_rbtreeutil.h:758
static const RbTreeNode * rightmost(const RbTreeNode *subtree)
static bool isLeftChild(const RbTreeNode *node)
Definition bslalg_rbtreeutil.h:1851
static const RbTreeNode * previous(const RbTreeNode *node)
static void remove(RbTreeAnchor *tree, RbTreeNode *node)
static void rotateLeft(RbTreeNode *node)
static const RbTreeNode * upperBound(const RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value)
Definition bslalg_rbtreeutil.h:1444
static void swap(RbTreeAnchor *a, RbTreeAnchor *b)
static void deleteTree(RbTreeAnchor *tree, FACTORY *nodeFactory)
Definition bslalg_rbtreeutil.h:1605
static void insertAt(RbTreeAnchor *tree, RbTreeNode *parentNode, bool leftChildFlag, RbTreeNode *newNode)
static const RbTreeNode * leftmost(const RbTreeNode *subtree)
static RbTreeNode * findInsertLocation(bool *insertAsLeftChildFlag, RbTreeAnchor *tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value)
Definition bslalg_rbtreeutil.h:1644
static RbTreeNode * findUniqueInsertLocation(int *comparisonResult, RbTreeAnchor *tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value)
Definition bslalg_rbtreeutil.h:1716
static void printTreeStructure(FILE *file, const RbTreeNode *subtree, void(*printNodeValueCallback)(FILE *, const RbTreeNode *), int level=0, int spacesPerLevel=4)
static int validateRbTree(const RbTreeNode *rootNode, const NODE_COMPARATOR &comparator)
Definition bslalg_rbtreeutil.h:1870
static void copyTree(RbTreeAnchor *result, const RbTreeAnchor &original, FACTORY *nodeFactory)
Definition bslalg_rbtreeutil.h:1463
static void moveTree(RbTreeAnchor *result, RbTreeAnchor *original, FACTORY *nodeFactory, FACTORY *originalNodeFactory)
Definition bslalg_rbtreeutil.h:1530
static const RbTreeNode * next(const RbTreeNode *node)
static const RbTreeNode * find(const RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value)
Definition bslalg_rbtreeutil.h:1388
static bool isWellFormed(const RbTreeAnchor &tree, const NODE_COMPARATOR &comparator)
Definition bslalg_rbtreeutil.h:1897
static bool isRightChild(const RbTreeNode *node)
Definition bslalg_rbtreeutil.h:1860
static const RbTreeNode * lowerBound(const RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value)
Definition bslalg_rbtreeutil.h:1414
static void insert(RbTreeAnchor *tree, const NODE_COMPARATOR &comparator, RbTreeNode *newNode)
Definition bslalg_rbtreeutil.h:1821
static void rotateRight(RbTreeNode *node)