AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
Trees
1. Multi-Way search Trees
1. 2-3 Trees:
a. Nodes may contain 1 or 2 items.
b. A node with k items has k + 1 children
c. All leaves are on same level.
2. Example
• A 2-3 tree storing 18 items.
20 80
30 70
5
2 4
10
25
40 50 75
90 100
85
95 110 120
3. Updating
• Insertion:
• Find the appropriate leaf. If there is only
one item, just add to leaf.
• Insert(23); Insert(15)
• If no room, move middle item to parent and
split remaining two items among two
children.
• Insert(3);
9. Delete
• If item is not in a leaf exchange with inorder successor.
• If leaf has another item, remove item.
• Examples: Remove(110);
• (Insert(110); Remove(100); )
• If leaf has only one item but sibling has two
items: redistribute items. Remove(80);
12. Some more removals…
• Remove(70);
Swap(70, 75);
Remove(70);
“Merge” Empty node with sibling;
Join parent with node;
Now every node has k+1 children except that one
node has 0 items and one child.
Sibling 95 110 can spare an item: redistribute.
17. Deletion Summary
• If item k is present but not in a leaf, swap
with inorder successor;
• Delete item k from leaf L.
• If L has no items: Fix(L);
• Fix(Node N);
• //All nodes have k items and k+1 children
• // A node with 0 items and 1 child is
possible, it will have to be fixed.
18. Deletion (continued)
• If N is the root, delete it and return its child
as the new root.
• Example: Delete(8);
5
5
1
3
2
8
3
3
Return
35
35
19. Deletion (Continued)
• If a sibling S of N has 2 items distribute
items among N, S and the parent P; if N is
internal, move the appropriate child from S
to N.
• Else bring an item from P into S;
• If N is internal, make its (single) child the
child of S; remove N.
• If P has no items Fix(P) (recursive call)
20. (2,4) Trees
• Size Property: nodes may have 1,2,3 items.
• Every node, except leaves has size+1
children.
• Depth property: all leaves have the same
depth.
• Insertion: If during the search for the leaf
you encounter a “full” node, split it.
24. Removal
• As with BS trees, we may place the node to
be removed in a leaf.
• If the leaf v has another item, done.
• If not, we have an UNDERFLOW.
• If a sibling of v has 2 or 3 items, transfer an
item.
• If v has 2 or 3 siblings we perform a
transfer
25. Removal
• If v has only one sibling with a single item
we drop an item from the parent to the
sibling, remove v. This may create an
underflow at the parent. We “percolate” up
the underflow. It may reach the root in
which case the root will be discarded and
the tree will “shrink”.
30. Drop item from root
• Remove root, return the child.
35 60
6 20
40 50
70 80 90
31. Summary
• Both 2-3 trees and 2-4 trees make it very
easy to maintain balance.
• Insertion and deletion easier for 2-4 tree.
• Cost is waste of space in each node. Also
extra comparison inside each node.
• Does not “extend” binary trees.
32. Red-Black Trees
• Root property: Root is BLACK.
• External Property: Every external node is
BLACK (external nodes: null nodes)
• Internal property: Children of a RED node
are BLACK.
• Depth property: All external nodes have the
same BLACK depth.
35. Red Black Trees, Insertion
1. Find proper external node.
2. Insert and color node red.
3. No black depth violation but may violate
the red-black parent-child relationship.
4. Let: z be the inserted node, v its parent
and u its grandparent. If v is red then u
must be black.
36. Color adjustments.
• Red child, red parent. Parent has a black
sibling (Zig-Zag).
a
b
u
w
v
z
Vl
Zl
Zr
56. Red Black Tree
• Insert 50 (sibling of 70 is black!)
15
gramps 15
10
70
Child 30
Oops, red-red.
ROTATE!
85
30
20
60
50
Parent 70
57. Red Black Tree
• Double Rotate – Adjust colors
30
15
10
Child-Parent-Gramps
70
20
60
50
Middle goes to “top”
Previous top becomes child. Its right
85
64. Red Black Tree
• Insert 55
30
15
10
70
20
85
60
5
50
40
65
55
80
90
65. Delete
• We first note that a red node is either a leaf or
must have two children.
• Also, if a black node has a single child it must be a
red leaf.
• Swap X with inorder successor.
• If inorder successor is red, (must be a leaf) delete.
If it is a single child parent, delete and change its
child color to black. In both cases the resulting
tree is a legit red-black tree.
66. Delete demo
• Delete 30: Swap with 40 and delete red leaf.
30
15
10
70
20
85
60
5
50
40
65
55
80
90
68. Inorder successor is Black
Change colors along the traverse path so that the leaf to
be deleted is RED.
Delete 15.
30
15
10
70
20
85
60
5
50
40
65
55
80
90
69. General strategy
• As you traverse the tree to locate the
inorder successor let X be the current node,
T its sibling and P the parent.
• Color the root red.
• Retain: “the color of P is red.”
• If all children of X and T are black:
• P Black, X Red, T Red
71. P
X
A
T
B
If X is a leaf we are done. Recall: x is the inorder
successor!
72. Even though we
Even though we
want to proceed
want to proceed
with X we have aa
with X we have
red-red violation
red-red violation
that needs to be
that needs to be
fixed.
fixed.
P
T has aared child.
T has red child.
X
T
C1
A
D
B
Zig-Zag, C1 Middle key.
C
76. • If both children of T are red select one of
the two rotations.
• If the right child of X is red make it the new
parent (it is on the inorder-successor path).
• If the left child of X is red:
77. Root of C is black
Root of C is black
Otherwise, continue
Otherwise, continue
X has aared child
X has red child
P
X
C1
C
B
Y
A
B
Left as a drill.
T
E