Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
Example
A 2-3 tree storing 18 items.
20 80 5 2 4 10 25 30 70 40 50 75 90 100 85 95 110 120
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);
Insertion
Insert(3);
20 80 5 2 3 4 10 15 30 70 23 25 40 50 75 90 100 85 95 110 120
Insert(3);
In mid air
20 80 5 3 2 4 10 15 23 25 40 50 75 85 95 110 120 30 70 90 100
Done.
20 80 3 5 2 30 70 40 50 75 90 100 85 95 110 120
4 10 15 23 25
New root:
20
3 5 2 4
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);
Remove(80);
3 5 2 4
RedistributeRemove(80);
45 20 85 30 10 25 40 70 50 75 95 110 90 100 120
3 5 2 4
Delete(70)
45 20 85 30 10 25 40 75 50 95 110 90 100 120
3 5 2 4
New tree:
Delete(85) will shrink the tree.
45 20 95 30 10 85 90 110 100 120
3 5 2 4
25 40 50
Details
1. Swap(85, 90) //inorder successor 2. Remove(85) //empty node created 3. Merge with sibling 4. Drop item from parent// (50,90) empty Parent 5. Merge empty node with sibling, drop item from parent (95) 6. Parent empty, merge with sibling drop item. Parent (root) empty, remove root.
3 5 2 4 10
30 25 40 50 90
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.
Deletion (continued)
If N is the root, delete it and return its child as the new root. Example: Delete(8);
1
5 3 8
5
2 3
35
Return
35
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)
(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.
(2,4) Tree
10 45 60 70 90 100
3 8
25
50 55
Insert(38);
Insert(38); 45 10 60
3 8
25 38
50 55
70 90 100
Insert(105)
Insert(105);
45 10 60 90
3 8
25 38
50 55
70
100 105
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
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.
Delete(15)
35 20 6 60
15
40 50
70 80 90
Delete(15)
35 20 6 60
40 50
70 80 90
Continued
Drop item from parent
35 60 6 20
40 50
70 80 90
Fuse
35 60 6 20
40 50
70 80 90
35 60 6 20
40 50
70 80 90
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.
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.
RedBlack
Insertion
Color adjustments.
Rotation
Z-middle key. Black height does not change! No more red-red.
a b v Zr z u w
Vl
Zl
Color adjustment II
a b v Vr z Zr u w
Zl
Rotation II
v-middle key
b z a v u w
Zl
Zr
Vr
Recoloring
Red child, red parent. Parent has a red sibling.
a b v z Vl Zr u w
Color adjustment
Red-red may move up
a b v z Vl Zl Zr u w
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.
Delete demo
Delete 30: Swap with 40 and delete red leaf.
30 15 10 5 40 20 50 55 60 65 80 70 85 90
40 15 10 5 20 50 55 60 65 80 70 85 90
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
P X A T
P X T
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. T has aared child. T has red child.
P X T
C1 D B C
Note: black Note: black depth remains depth remains unchanged! unchanged!
C1 P X B C D T
B will become Ps B will become Ps right child. No right child. No change in depth. change in depth.
Third case
P X T
A B C T middle key.
C1
T P X B C1
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:
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 Y A B Left as a drill. T
C B
P C1 X Y A B C E T
Delete 15 15 10 5 20
30 70 60 50 40 55 65 80 85 90
Delete 15 30 15 10 5 20 40 50 55
60 70 65 80 60 30 20 10 15 40 50 55 65 80 70 85 90 85 90
Delete 15 30 20 10 5 15 40 50 55
60 70 65 80 85 90
Third case: (mirror image) X (15) has two black children (Nulls) Sibling has one red and one black child.
Delete 15 30 10 5 20 40 50 55
60 70 65 80 85 90