For the complete documentation index, see llms.txt. This page is also available as Markdown.

13. Red-Black Trees

Exercises

13.1-1

Red-black tree of black-height 2. The image was produced with the data structure visualizations tool.

If you color the nodes 4 and 12 into black (children of the root node 8), then the resulting tree will have black-height of 3. If you color all nodes into black, then you will get a tree of black-height 4.

13.1-2

The node 36 would be placed as the right child of red node 35 in Figure 13.1 (see the book). Sure, this will not be a legal red-black tree. It cannot be colored red, as property 4 would be violated. It cannot be colored black neither, since property 5 would be violated; from node 38 there would be two simple paths toward descendant leaves with different numbers of black nodes.

Correct red-back tree after inserting node 36 into the tree from Figure 13.1. The image was produced with the data structure visualizations tool.

13.1-3

Available in the latest revision of the IM.

13.1-4

Available in the latest revision of the IM.

13.1-5 🌟

Available in the latest revision of the IM.

13.1-6

Available in the latest revision of the IM.

13.1-7

Available in the latest revision of the IM.

13.1-8 🌟

Available in the latest revision of the IM.

13.2-1

Available in the latest revision of the IM.

13.2-2

Available in the latest revision of the IM.

13.2-3

Available in the latest revision of the IM.

13.2-4 🌟

Available in the latest revision of the IM.

β˜… 13.2-5

Available in the latest revision of the IM.

13.3-1

Available in the latest revision of the IM.

13.3-2

Use the data structure visualizations tool and watch the resulting tree as you insert the nodes. You can adjust the animation speed to clearly see the re-colorings and rotations.

13.3-3

Available in the latest revision of the IM.

13.3-4

Available in the latest revision of the IM.

13.3-5

Available in the latest revision of the IM.

13.3-6

Observe that all nodes accessed via parent pointers in RB-Insert-Fixup are exactly those that are encountered by RB-Insert while finding the location for the new node. Therefore, RB-Insert should store them in a stack, initialized with the sentinel T.nil. RB-Insert-Fixup would pop and push nodes from this stack, as needed. The procedures for rotations would need to receive an additional parameter, the rotated node's parent.

The size of this stack will be commensurate with the height of a RB tree, which is O(lg⁑n)O(\lg n). Pushing and popping nodes execute in O(1)O(1) time, so the asymptotic runtime of RB-Insert would remain the same.

Last updated