8#ifndef INCLUDED_BSLALG_RBTREEUTIL
9#define INCLUDED_BSLALG_RBTREEUTIL
728#include <bslscm_version.h>
815 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
817 NODE_VALUE_COMPARATOR& comparator,
819 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
821 NODE_VALUE_COMPARATOR& comparator,
839 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
841 NODE_VALUE_COMPARATOR& comparator,
843 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
845 NODE_VALUE_COMPARATOR& comparator,
864 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
866 NODE_VALUE_COMPARATOR& comparator,
868 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
870 NODE_VALUE_COMPARATOR& comparator,
892 template <
class FACTORY>
895 FACTORY *nodeFactory);
909 template <
class FACTORY>
912 FACTORY *nodeFactory,
913 FACTORY *originalNodeFactory);
923 template <
class FACTORY>
953 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
955 bool *insertAsLeftChildFlag,
957 NODE_VALUE_COMPARATOR& comparator,
959 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
961 bool *insertAsLeftChildFlag,
963 NODE_VALUE_COMPARATOR& comparator,
994 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
996 int *comparisonResult,
998 NODE_VALUE_COMPARATOR& comparator,
1000 template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1002 int *comparisonResult,
1004 NODE_VALUE_COMPARATOR& comparator,
1019 template <
class NODE_COMPARATOR>
1021 const NODE_COMPARATOR& comparator,
1132 void (*printNodeValueCallback)(FILE *,
const RbTreeNode *),
1134 int spacesPerLevel = 4);
1166 template <
class NODE_COMPARATOR>
1168 const NODE_COMPARATOR& comparator);
1169 template <
class NODE_COMPARATOR>
1171 const char **errorDescription,
1173 const NODE_COMPARATOR& comparator);
1200 template <
class NODE_COMPARATOR>
1202 const NODE_COMPARATOR& comparator);
1228 template <
class NODE_COMPARATOR>
1230 const char **errorDescription,
1234 const NODE_COMPARATOR& comparator);
1267template <
class DELETER>
1273 DELETER *d_deleter_p;
1331template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1334 NODE_VALUE_COMPARATOR& comparator,
1341template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1344 NODE_VALUE_COMPARATOR& comparator,
1352 if (lowBound != tree.
sentinel() && !comparator(value, *lowBound)) {
1358template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1360 NODE_VALUE_COMPARATOR& comparator,
1367template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1370 NODE_VALUE_COMPARATOR& comparator,
1376 if (comparator(*node, value)) {
1380 nextLargestNode = node;
1384 return nextLargestNode;
1387template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1390 NODE_VALUE_COMPARATOR& comparator,
1397template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1400 NODE_VALUE_COMPARATOR& comparator,
1406 if (comparator(value, *node)) {
1407 nextLargestNode = node;
1414 return nextLargestNode;
1417template <
class FACTORY>
1420 FACTORY *nodeFactory)
1436 RbTreeNode *copiedRoot = nodeFactory->cloneNode(*originalNode);
1449 originalNode = originalNode->
leftChild();
1450 RbTreeNode *newNode = nodeFactory->cloneNode(*originalNode);
1457 copiedNode = newNode;
1462 RbTreeNode *newNode = nodeFactory->cloneNode(*originalNode);
1469 copiedNode = newNode;
1472 originalNode = originalNode->
parent();
1473 copiedNode = copiedNode->
parent();
1475 }
while (original.
sentinel() != originalNode);
1479 result->
reset(copiedRoot,
1484template <
class FACTORY>
1487 FACTORY *nodeFactory,
1488 FACTORY *originalNodeFactory)
1505 RbTreeNode *copiedRoot = nodeFactory->moveIntoNewNode(originalNode);
1520 originalNode = originalNode->
leftChild();
1521 RbTreeNode *newNode = nodeFactory->moveIntoNewNode(originalNode);
1528 copiedNode = newNode;
1533 RbTreeNode *newNode = nodeFactory->moveIntoNewNode(originalNode);
1540 copiedNode = newNode;
1543 originalNode = originalNode->
parent();
1544 copiedNode = copiedNode->
parent();
1546 }
while (original->
sentinel() != originalNode);
1550 result->
reset(copiedRoot,
1559template <
class FACTORY>
1591 nodeFactory->deleteNode(node);
1594 }
while (tree->
sentinel() != node);
1598template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1600 bool *insertAsLeftChildFlag,
1602 NODE_VALUE_COMPARATOR& comparator,
1610 *insertAsLeftChildFlag =
true;
1615 *insertAsLeftChildFlag = comparator(value, *node);
1616 if (*insertAsLeftChildFlag) {
1626template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1628 bool *insertAsLeftChildFlag,
1630 NODE_VALUE_COMPARATOR& comparator,
1641 if (tree->
sentinel() == hint || !comparator(*hint, value)) {
1646 if (tree->
firstNode() == hint || !comparator(value, *prev)) {
1653 *insertAsLeftChildFlag =
true;
1657 *insertAsLeftChildFlag =
false;
1670template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1672 int *comparisonResult,
1674 NODE_VALUE_COMPARATOR& comparator,
1688 bool leftChild =
true;
1693 leftChild = comparator(value, *node);
1698 nextSmallestNode = node;
1703 if (nextSmallestNode && !comparator(*nextSmallestNode, value)) {
1704 *comparisonResult = 0;
1705 return nextSmallestNode;
1707 *comparisonResult = leftChild ? -1 : 1;
1711template <
class NODE_VALUE_COMPARATOR,
class VALUE>
1713 int *comparisonResult,
1715 NODE_VALUE_COMPARATOR& comparator,
1723 enum { LEFT_CHILD = -1, NODE_FOUND = 0, RIGHT_CHILD = 1 };
1727 if (tree->
sentinel() == hint || comparator(value, *hint)) {
1732 if (tree->
firstNode() == hint || comparator(*prev, value)) {
1739 *comparisonResult = LEFT_CHILD;
1744 *comparisonResult = RIGHT_CHILD;
1753 if (!comparator(value, *prev)) {
1754 *comparisonResult = NODE_FOUND;
1763 else if (tree->
sentinel() != hint && !comparator(*hint, value)) {
1764 *comparisonResult = NODE_FOUND;
1775template <
class NODE_COMPARATOR>
1777 const NODE_COMPARATOR& comparator,
1789 bool leftChildFlag =
true;
1794 leftChildFlag = comparator(*newNode, *node);
1795 if (leftChildFlag) {
1802 return insertAt(tree, parent, leftChildFlag, newNode);
1823template <
class NODE_COMPARATOR>
1826 const NODE_COMPARATOR& comparator)
1829 const char *errorDescription;
1830 return validateRbTree(&errorNode, &errorDescription, rootNode, comparator);
1833template <
class NODE_COMPARATOR>
1835 const char **errorDescription,
1837 const NODE_COMPARATOR& comparator)
1850template <
class NODE_COMPARATOR>
1853 const NODE_COMPARATOR& comparator)
1866template <
class NODE_COMPARATOR>
1869 const char **errorDescription,
1873 const NODE_COMPARATOR& comparator)
1891 enum { INVALID_RBTREE = -1};
1901 if ((minNodeValue && comparator(*rootNode, *minNodeValue)) ||
1902 (maxNodeValue && comparator(*maxNodeValue, *rootNode))) {
1903 *errorNode = rootNode;
1904 *errorDescription =
"Invalid binary search tree.";
1905 return INVALID_RBTREE;
1910 if ((left != 0 || right != 0) && left == right) {
1911 *errorNode = rootNode;
1912 *errorDescription =
"Invalid children";
1913 return INVALID_RBTREE;
1918 if ((left && left->
parent() != rootNode) ||
1919 (right && right->
parent() != rootNode)) {
1920 *errorNode = rootNode;
1921 *errorDescription =
"Invalid parent pointers for children";
1922 return INVALID_RBTREE;
1930 *errorNode = rootNode;
1931 *errorDescription =
"Red node with a red child.";
1932 return INVALID_RBTREE;
1949 if (leftDepth < 0 || rightDepth < 0) {
1950 return INVALID_RBTREE;
1955 if (leftDepth != rightDepth) {
1956 *errorNode = rootNode;
1958 "Black violation (unequal black depth from node to leaves).";
1959 return INVALID_RBTREE;
1971template <
class DELETER>
1976, d_deleter_p(deleter)
1981template <
class DELETER>
1985 if (d_tree_p && d_tree_p->rootNode()) {
1986 d_tree_p->rootNode()->setParent(d_tree_p->sentinel());
1993template <
class DELETER>
Definition bslalg_rbtreeanchor.h:352
RbTreeNode * sentinel()
Definition bslalg_rbtreeanchor.h:533
RbTreeNode * firstNode()
Definition bslalg_rbtreeanchor.h:521
int numNodes() const
Return the numNodes attribute of this object.
Definition bslalg_rbtreeanchor.h:552
RbTreeNode * rootNode()
Definition bslalg_rbtreeanchor.h:527
void reset(RbTreeNode *rootNode, RbTreeNode *firstNode, int numNodes)
Definition bslalg_rbtreeanchor.h:479
Definition bslalg_rbtreenode.h:376
RbTreeNode * rightChild()
Definition bslalg_rbtreenode.h:598
RbTreeNode * leftChild()
Definition bslalg_rbtreenode.h:592
void setRightChild(RbTreeNode *address)
Definition bslalg_rbtreenode.h:552
void setParent(RbTreeNode *address)
Definition bslalg_rbtreenode.h:537
Color color() const
Return the color of this node.
Definition bslalg_rbtreenode.h:635
void setLeftChild(RbTreeNode *address)
Definition bslalg_rbtreenode.h:546
void setColor(Color value)
Set the color of this node to the specified value.
Definition bslalg_rbtreenode.h:558
RbTreeNode * parent()
Definition bslalg_rbtreenode.h:586
@ BSLALG_BLACK
Definition bslalg_rbtreenode.h:382
@ BSLALG_RED
Definition bslalg_rbtreenode.h:381
Definition bslalg_rbtreeutil.h:1268
void release()
Release from management the tree supplied at construction.
Definition bslalg_rbtreeutil.h:1995
~RbTreeUtilTreeProctor()
Definition bslalg_rbtreeutil.h:1983
RbTreeUtilTreeProctor(RbTreeAnchor *tree, DELETER *deleter)
Definition bslalg_rbtreeutil.h:1973
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlc_flathashmap.h:1805
Definition bslalg_rbtreeutil.h:1211
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:1867
static bool isWellFormedAnchor(const RbTreeAnchor &tree)
Definition bslalg_rbtreeutil.h:756
static const RbTreeNode * rightmost(const RbTreeNode *subtree)
static bool isLeftChild(const RbTreeNode *node)
Definition bslalg_rbtreeutil.h:1806
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:1399
static void swap(RbTreeAnchor *a, RbTreeAnchor *b)
static void deleteTree(RbTreeAnchor *tree, FACTORY *nodeFactory)
Definition bslalg_rbtreeutil.h:1560
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:1599
static RbTreeNode * findUniqueInsertLocation(int *comparisonResult, RbTreeAnchor *tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value)
Definition bslalg_rbtreeutil.h:1671
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:1825
static void copyTree(RbTreeAnchor *result, const RbTreeAnchor &original, FACTORY *nodeFactory)
Definition bslalg_rbtreeutil.h:1418
static void moveTree(RbTreeAnchor *result, RbTreeAnchor *original, FACTORY *nodeFactory, FACTORY *originalNodeFactory)
Definition bslalg_rbtreeutil.h:1485
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:1343
static bool isWellFormed(const RbTreeAnchor &tree, const NODE_COMPARATOR &comparator)
Definition bslalg_rbtreeutil.h:1852
static bool isRightChild(const RbTreeNode *node)
Definition bslalg_rbtreeutil.h:1815
static const RbTreeNode * lowerBound(const RbTreeAnchor &tree, NODE_VALUE_COMPARATOR &comparator, const VALUE &value)
Definition bslalg_rbtreeutil.h:1369
static void insert(RbTreeAnchor *tree, const NODE_COMPARATOR &comparator, RbTreeNode *newNode)
Definition bslalg_rbtreeutil.h:1776
static void rotateRight(RbTreeNode *node)