Trees
-
Upload
susant-sahani -
Category
Technology
-
view
74 -
download
1
Transcript of Trees
![Page 1: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/1.jpg)
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.
![Page 2: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/2.jpg)
Example
• A 2-3 tree storing 18 items.
20 80
5 30 70 90 100
2 4 10 25 40 50 75 85 95 110 120
![Page 3: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/3.jpg)
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);
![Page 4: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/4.jpg)
Insertion
• Insert(3);
20 80
5 30 70 90 100
2 3 4 10 15 23 25 40 50 75 85 95 110 120
![Page 5: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/5.jpg)
Insert(3);
• In mid air…20 80
5 30 70 90 100
2 10 15 23 25 40 50 75 85 95 110 120
3
4
![Page 6: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/6.jpg)
Done….
20 80
3 5 30 70 90 100
2 10 15 23 25 40 50 75 85 95 110 1204
![Page 7: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/7.jpg)
Tree grows at the root…
• Insert(45);
20 80
3 5 30 70 90 100
2 10 25 40 45 50 75 85 95 110 1204
![Page 8: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/8.jpg)
• New root:
80
3 5 30 90 100
2 10 25 40 75 85 95 110 1204
45
20
50
70
![Page 9: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/9.jpg)
Delete
• If item is not in a leaf exchange with in-order 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);
![Page 10: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/10.jpg)
Remove(80);• Step 1: Exchange 80 with in-order
successor.
85
3 5 30 90 100
2 10 25 40 75 80 95 110 1204
45
20
50
70
![Page 11: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/11.jpg)
Remove(80);• Redistribute
85
3 5 30 95 110
2 10 25 40 75 90 100 1204
45
20
50
70
![Page 12: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/12.jpg)
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 can spare an item: redistribute.95 110
![Page 13: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/13.jpg)
Delete(70)
85
3 5 30 95 110
2 10 25 40 90 100 1204
45
20
50
75
![Page 14: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/14.jpg)
New tree:• Delete(85) will “shrink” the tree.
95
3 5 30 110
2 10 25 40 90 100 1204
45
20
50
85
![Page 15: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/15.jpg)
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.
![Page 16: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/16.jpg)
“Shorter” 2-3 Tree
3 5 30 95 110
2 10 25 40 100 1204
20 45
50 90
![Page 17: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/17.jpg)
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.
![Page 18: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/18.jpg)
Deletion (continued)
• If N is the root, delete it and return its child as the new root.
• Example: Delete(8);
5
3 8
5
3
1 2
3 5
3
Return 3 5
![Page 19: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/19.jpg)
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)
![Page 20: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/20.jpg)
(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.
![Page 21: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/21.jpg)
(2,4) Tree
10 45 60
3 8 25 50 55 70 90 100
![Page 22: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/22.jpg)
Insert(38);
Insert(38);
3 8 25 38 50 55 70 90 100
45
10
60
![Page 23: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/23.jpg)
Insert(105)
• Insert(105);
3 8 25 38 50 55 100 105
45
10 60 90
70
![Page 24: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/24.jpg)
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
![Page 25: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/25.jpg)
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”.
![Page 26: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/26.jpg)
Delete(15)
35
20 60
6 15 40 50 70 80 90
![Page 27: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/27.jpg)
Delete(15)
35
20 60
6 40 50 70 80 90
![Page 28: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/28.jpg)
Continued
• Drop item from parent
35
60
6 20 40 50 70 80 90
![Page 29: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/29.jpg)
Fuse
35
60
6 20 40 50 70 80 90
![Page 30: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/30.jpg)
Drop item from root
• Remove root, return the child.
35 60
6 20 40 50 70 80 90
![Page 31: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/31.jpg)
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.
![Page 32: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/32.jpg)
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.
![Page 33: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/33.jpg)
A RedBlack tree.Black depth 3.
30
15 70
20 8510
5
60
6550
40
9080
55
![Page 34: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/34.jpg)
RedBlack
Insertion
![Page 35: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/35.jpg)
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.
![Page 36: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/36.jpg)
Color adjustments.• Red child, red parent. Parent has a black
sibling (Zig-Zag).
a
b u
v w
zVl
ZlZr
![Page 37: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/37.jpg)
Rotation• Z-middle key. Black height does not
change! No more red-red.
a
b z
v
w
u
Vl Zl Zr
![Page 38: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/38.jpg)
Color adjustment II
a
b u
v w
z
Vr
ZlZr
![Page 39: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/39.jpg)
Rotation II• v-middle key a
b v
z u
ZrZlw
Vr
![Page 40: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/40.jpg)
Recoloring
• Red child, red parent. Parent has a red sibling.
a
b u
v w
zVl
Zr
![Page 41: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/41.jpg)
Color adjustment
• Red-red may move up…
a
b u
v w
zVl
ZrZl
![Page 42: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/42.jpg)
Red Black Tree
• Insert 10 – root
10
![Page 43: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/43.jpg)
Red Black Tree
• Insert 10 – root (external nodes not shown)
10
![Page 44: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/44.jpg)
Red Black Tree
• Insert 85
10
85
![Page 45: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/45.jpg)
Red Black Tree
• Insert 15
10
85
15
![Page 46: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/46.jpg)
Red Black Tree
• Rotate – Change colors
15
10 85
![Page 47: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/47.jpg)
Red Black Tree
• Insert 70
15
10 85
70
![Page 48: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/48.jpg)
Red Black Tree
• Change Color
15
10 85
70
![Page 49: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/49.jpg)
Red Black Tree
• Insert 20 (sibling of parent is black)
20
15
10 85
70
![Page 50: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/50.jpg)
Red Black Tree
• Rotate
15
10 70
20 85
![Page 51: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/51.jpg)
Red Black Tree
• Insert 60 (sibling of parent is red)
15
10 70
20 85
60
![Page 52: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/52.jpg)
Red Black Tree
• Change Color
15
10 70
20 85
60
![Page 53: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/53.jpg)
Red Black Tree
• Insert 30 (sibling of parent is black)
15
10 70
20 85
60
30
![Page 54: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/54.jpg)
Red Black Tree
• Rotate
15
10 70
30 85
6020
![Page 55: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/55.jpg)
Red Black Tree
• Insert 50 (sibling ?)
15
10 70
30 85
6020
50
![Page 56: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/56.jpg)
Red Black Tree
• Insert 50 (sibling of 70 is black!)
15
10 70
30 85
6020
50Oops, red-red. ROTATE!
Child 30
Parent 70
gramps 15
![Page 57: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/57.jpg)
Red Black Tree
• Double Rotate – Adjust colors
30
15 70
20 8510 60
50Child-Parent-Gramps
Middle goes to “top”
Previous top becomes child. Its right child is middles left child.
![Page 58: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/58.jpg)
Red Black Tree
• Insert 65
30
15 70
20 8510 60
6550
![Page 59: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/59.jpg)
Red Black Tree
• Insert 80
30
15 70
20 8510 60
6550 80
![Page 60: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/60.jpg)
Red Black Tree
• Insert 90
30
15 70
20 8510 60
6550 9080
![Page 61: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/61.jpg)
Red Black Tree
• Insert 40
30
15 70
20 8510 60
6550
40
9080
![Page 62: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/62.jpg)
Red Black Tree
• Adjust color
30
15 70
20 8510 60
6550
40
9080
![Page 63: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/63.jpg)
Red Black Tree
• Insert 5
30
15 70
20 8510
5
60
6550
40
9080
![Page 64: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/64.jpg)
Red Black Tree
• Insert 55
30
15 70
20 8510
5
60
6550
40
9080
55
![Page 65: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/65.jpg)
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.
![Page 66: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/66.jpg)
Delete demo
• Delete 30: Swap with 40 and delete red leaf.
30
15 70
20 8510
5
60
6550
40
9080
55
![Page 67: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/67.jpg)
40
15 70
20 8510
5
60
6550 9080
55
![Page 68: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/68.jpg)
Inorder successor is BlackChange colors along the traverse path so that the leaf to be deleted is RED.
Delete 15.30
15 70
20 8510
5
60
6550
40
9080
55
![Page 69: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/69.jpg)
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
![Page 70: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/70.jpg)
X T
P
Both children of X and T are black:
P Black X Red, T Red
A B
![Page 71: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/71.jpg)
X T
P
If X is a leaf we are done. Recall: x is the inorder successor!
A B
![Page 72: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/72.jpg)
X T
P
C1
B C
D
Zig-Zag, C1 Middle key.
A
Even though we want to proceed with X we have a red-red violation that needs to be fixed.
T has a red child.
Even though we want to proceed with X we have a red-red violation that needs to be fixed.
T has a red child.
![Page 73: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/73.jpg)
P T
C1
B C D
X
A
Note: black depth remains unchanged!
Note: black depth remains unchanged!
![Page 74: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/74.jpg)
Third case
X T
P
C1
B
C D
A
T middle key.
B will become P’s right child. No change in depth.
B will become P’s right child. No change in depth.
![Page 75: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/75.jpg)
P C1
T
B C DA
X
![Page 76: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/76.jpg)
• 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:
![Page 77: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/77.jpg)
T
P
X
EC1
B
Y
C
AB
Left as a drill.
Root of C is black
Otherwise, continue
X has a red child
Root of C is black
Otherwise, continue
X has a red child
![Page 78: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/78.jpg)
T
P
XE
C1
Y
CA
B
![Page 79: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/79.jpg)
30
15 70
20 8510
5
60
6550
40
9080
55
Delete 15
![Page 80: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/80.jpg)
30
20
70
10
8515
5
60
6550
40 908055
Delete 15
30
15
70
10
8520
5
60
6550
40 908055
Swap (15, 20)
![Page 81: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/81.jpg)
30
15
70
10
8520
5
60
6550
40 908055
Delete 15
Third case: (mirror image) X (15) has two black children (Nulls)
Sibling has one red and one black child.
![Page 82: Trees](https://reader036.fdocuments.in/reader036/viewer/2022062513/5577b592d8b42a1c068b46bf/html5/thumbnails/82.jpg)
30 70
10 85
205
60
6550
40 908055
Delete 15