BDE 4.14.0 Production release
|
Provide a base class for a red-black binary tree node.
This component provides a single POD-like class, RbTreeNode
, used to represent a node in a red-black binary search tree. An RbTreeNode
provides the address to its parent, left-child, and right-child nodes, as well as providing a "color" (red or black). RbTreeNode
does not, however, contain "payload" data (e.g., a value), as it is intended to work with generalized tree operations (see bslalg_rbtreenodeutil ). Clients creating a red-black binary search tree must define their own node type that incorporates RbTreeNode
(generally via inheritance), and that maintains the "key" value and any associated data.
To reduce the memory footprint of the RbTreeNode
, the color information is stored at the least-significant-bit (LSB) of the parent node. The address of the parent node and the color can be accessed through bit-wise operations. This is possible because all memory addresses are at least 4-bytes aligned, therefore, the 2 LSB of any pointer are always 0.
This section illustrates intended usage of this component.
This example demonstrates creating a function that prints, to a FILE
, a tree of RbTreeNode
objects.
First, we define the signature of a function, printTree
, that accepts, in addition to an output file and root node, a function pointer argument (supplied by clients) used to print each node's value, note that a node's value is not accessible through RbTreeNode
:
Now, we define the body of printTree
, which is a recursive function that performs a prefix traversal of the supplied binary tree, printing the value and color of rootNode
before recursively printing its left and then right sub-trees.
Notice that we use FILE
in the context of this usage example to avoid a dependency of standard library streams. Finally, we will use printTree
to print a description of a tree in the next example.
This example demonstrates creating a simple tree of integer values using RbTreeNode
. Note that, in practice, clients should use associated utilities to manage such a tree (see bslalg_rbtreenodeutil ).
First, we define a node-type, IntTreeNode
, that inherits from RbTreeNode
:
Then, we define a function printIntNodeValue
to print the value of an integer node. Note that this function's signature matches that required by printTree
(defined in the preceding example):
Next, we define main
for our test, and create three nodes that we'll use to construct a tree:
Then, we describe the structure of the tree we wish to construct.
Now, we set the properties for the nodes A
, B
, and C
to form a valid tree whose structure matches that description:
Finally, we use the printTree
function with the printIntTreeNodeValue
function to print the structure of our tree to stdout
:
Resulting in a single line of console output:
This example demonstrates creating a function to validate the properties of a red-black tree.
First, we declare the signature of a function validateRbTree
, which takes two arguments: (1) the address to the root node of a tree, and (2) a comparator function, which is used to compare the payload values of the tree nodes. Note that the parameterized comparator is needed because a node's value is not accessible through the supplied RbTreeNode
.
Then, we declare the signature for an auxiliary function, validateRbTreeRaw
, that accepts, additionally, the address of minimum and maximum value nodes, and is needed to recursively apply rule 1:
Now, we define the implementation of validateRbTree
, which simply delegates to validateRbTreeRaw
.
Finally, we define the implementation of validateRbTreeRaw
, which tests if rootNode
violates any of the rules defined in the validateRbTree
method documentation, and then recursively calls validateRbTreeRaw
on the left and right sub-trees or rootNode
: