Implement an iterator for the RedBlackTree class in Worked Example 17.2 that visits the nodes in sorted

Question:

Implement an iterator for the RedBlackTree class in Worked Example 17.2 that visits the nodes in sorted order. Take advantage of the parent links.

Data from worked example 17.2.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

The code for fixing up a double-red violation is quite long. Recall that there are four possible arrangements of the double red nodes:

image text in transcribed

In each case, we must sort the nodes and their children. Once we have the seven references n1, n2, n3, t1, t2, t3, and t4, the remainder of the procedure is straightforward. We build the replacement tree, change the reds to black, and subtract one from the color of the grandparent (which might be a double-black node when this method is called during node removal).
If we find that we introduced another double-red violation, we continue fixing it. Eventually, the violation is removed, or we reach the root, in which case the root is simply colored black:

image text in transcribed

image text in transcribed

image text in transcribed

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: