<5>
<5 40>
<10>
/ \
/ \
<5> <40>
<10>
/ \
/ \
<5> <20 40>
<10 20>
/ | \
/ | \
<5> <15> <40>
<10 20>
/ | \
/ | \
<5> <15> <30 40>
<15 20>
/ | \
/ | \
<5> <10> <30 40>
Then 10's node has no other items, so deleting 10 would leave an empty node
behind. The rightmost sibling has 2 data items, though, so we
redistribute:
<15 30>
/ | \
/ | \
<5> <20> <40>
<10>
/ \
/ \
<4> <20>
/ | | \
/ | | \
<3> <5> <15> <30 40>
<5>
<5 40>
<5 10 40>
<10>
/ \
/ \
<5> <20 40>
<10>
/ \
/ \
<5> <15 20 40>
<10 20>
/ | \
/ | \
<5> <15> <30 40>
<10 20>
/ | \
/ | \
<3 4 5> <15> <30 40>
<50>
/ \
/ \
<30> <70>
/ \ | \
| \ | \
<10 15 20> <40> <60> <80 90>
The red-black tree for it is:
<50>
/ \
/ \
<30> <70>
/ \ | \
| \ | \
<15> <40> <60> <80>
) ( (
) ( (
<10> <20> <90>
(where the ('s and )'s are used to denote dashed, or "red" lines, and the
slashes and pipes denote solid, or "black" lines).
Exercises:
<100>
/
/
<40>
/ \
/ \
<20> <50>
\
\
<60>
\
\
<90>
<50 90>
/ | \
/ | \
<20 40> <60> <100>
<50 90>
/ | \
/ | \
<20 40> <60> <100>
<50>
/ (
/ (
<20> <90>
( / \
( / \
<40> <60> <100>
<50>
/ \
/ \
<40> <90>
/ / \
/ / \
<20> <60> <100>
VisitRange(T, fromKey, toKey)
// Visits each item in T with key between fromKey and toKey
if (T's root is a leaf)
if (fromKey<=T.ldata <=toKey) // visit T.ldata
if (fromKey<=T.rdata <=toKey) // visit T.rdata
else
if (T has 2 items)
if (fromKeyT.ldata))
VisitRange(T.mChild, fromKey, toKey)
if (fromKey<=T.rdata <=toKey) // visit T.rdata
if (toKey>T.rdata)
VisitRange(T.rChild, fromKey, toKey)
else // T has 1 data item
if (fromKeyT.ldata)
VisitRange(T.mChild, fromKey, toKey)
<50>
/ \
/ \
<30> <70 90>
/ \ / | \
/ \ / | \
<10 20> <40><60><80> <100>
The result is:
<37 50>
____________/ | \___________
/ | \
<30 35> <39> <70 90>
____/ | \_____ / \ / | \
/ | \ / \ / | \
<10 20> <32 33 34> <36> <38> <40> <60> <80> <100>
<50>
) \________________
) \
<37> <70>
/ \______ / (
/ \ / (
<35> <39> <60> <90>
) \ / \ / \
) \ / \ / \
<30> <36> <38> <40> <80> <100>
/ \
/ \
<20> <33>
) ) (
) ) (
<10> <32> <34>
<37 50>
____________/ | \______________
/ | \
<30 35> <39> <70 85 90>
/ | \______ / \ / | |__ \____
/ | \ / \ / | | \
<10 20> <32 33 34> <36> <38> <40 45> <60 65><80> <87 89> <100>