Multi-Way search Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Multi-Way search Trees

Description:

Red Black Trees, Insertion Find proper external ... Red Black Tree Insert 65 30 15 70 20 85 10 60 65 50 Red Black Tree Insert 80 30 15 70 20 85 10 60 65 50 80 Red ... – PowerPoint PPT presentation

Number of Views:205
Avg rating:3.0/5.0
Slides: 64
Provided by: mrosenfeld
Category:
Tags: black | insertion | multi | search | tree | trees | way

less

Transcript and Presenter's Notes

Title: Multi-Way search Trees


1
Multi-Way search Trees
  • 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
5
30 70
90 100
25
40 50
75
85
110 120
2 4
10
95
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 item among two children.
  • Insert(3)

4
Insertion
  • Insert(3)

20 80
5
30 70
90 100
40 50
75
85
110 120
2 3 4
10 15
23 25
95
5
Insert(3)
  • In mid air

20 80
5
30 70
90 100
3
23 25
40 50
75
85
110 120
2
10 15
95
4
6
Done.
20 80
3 5
30 70
90 100
23 25
40 50
75
85
110 120
4
2
10 15
95
7
Tree grows at the root
  • Insert(45)

20 80
3 5
30 70
90 100
25
40 45 50
75
85
110 120
4
2
10
95
8
  • New root

45
20
80
3 5
30
90 100
70
25
40
75
85
110 120
4
50
2
10
95
9
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)

10
Remove(80)
  • Step 1 Exchange 80 with in-order successor.

45
20
85
3 5
30
90 100
70
110 120
25
40
75
80
4
50
2
10
95
11
Remove(80)
  • Redistribute

45
20
85
3 5
30
95 110
70
120
25
40
75
90
4
50
2
10
100
12
Some more removals
  • Remove(70)
  • Swap(70, 75)
  • Remove(70)
  • Merge Empty node with sibling
  • Join parent with node
  • Now every node has k1 children except that one
    node has 0 items and one child.
  • Sibling can spare an item
    redistribute.
  1. 110

13
Delete(70)
45
20
85
3 5
30
95 110
75
120
25
40

90
4
50
2
10
100
14
New tree
  • Delete(75) will shrink the tree.

45
20
95
3 5
30
110
75
120
25
40
90
100
4
50
2
10
15
Details
  • 1. Swap(75, 90) //inorder successor
  • 2. Remove(75) //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.

16
Shorter 2-3 Tree
20 45
3 5
30
95 110
120
50 90
25
40
100
4
2
10
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 k1 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
2
3
3
3 5
3
8
Return
3 5
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 size1 children.
  • Depth property all leaves have the same depth.
  • Insertion If during the search for the leaf you
    encounter a full node, split it.

21
(2,4) Tree
10 45 60
70 90 100
  • 3 8

50 55
25
22
Insert(38)
  • Insert(38)

45
10
60
70 90 100
3 8
50 55
25 38
23
Insert(105)
  • Insert(105)

45
10
60 90
3 8
50 55
100 105
70
25 38
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.

26
Delete(15)
35
20
60
6
15
40 50
70 80 90
27
Delete(15)
35
20
60
6
40 50
70 80 90
28
Continued
  • Drop item from parent

35
60
6 20
40 50
70 80 90
29
Fuse
35
60
6 20
40 50
70 80 90
30
Drop item from root
  • Remove root, return the child.

35 60
6 20
40 50
70 80 90
31
Summary
  • Both 2-3 tress 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
  • Internal property Children of a RED node are
    BLACK.
  • Depth property All external nodes have the same
    BLACK depth.

33
RedBlack
  • Insertion

34
Red Black Trees, Insertion
  • Find proper external node.
  • Insert and color node red.
  • No black depth violation but may violate the
    red-black parent-child relationship.
  • Let z be the inserted node, v its parent and u
    its grandparent. If v is red then u must be
    black.

35
Color adjustments.
  • Red child, red parent. Parent has a black
    sibling.

a
b
u
w
v
z
Vl
Zr
Zl
36
Rotation
  • Z-middle key. Black height does not change! No
    more red-red.

a
b
z
u
v
w
Zr
Zl
Vl
37
Color adjustment II
a
b
u
w
v
Vr
z
Zr
Zl
38
Rotation II
a
b
v
u
z
w
Zr
Zl
Vr
39
Recoloring
  • Red child, red parent. Parent has a red sibling.

a
b
u
w
v
z
Vl
Zr
40
Recoloring
  • Red-red may move up

a
b
u
w
v
z
Vl
Zr
Zl
41
Red Black Tree
  • Insert 10 root

10
42
Red Black Tree
  • Insert 10 root

10
43
Red Black Tree
  • Insert 85

10
85
44
Red Black Tree
  • Insert 15

10
85
15
45
Red Black Tree
  • Rotate Change colors

15
10
85
46
Red Black Tree
  • Insert 70

15
10
85
70
47
Red Black Tree
  • Change Color

15
10
85
70
48
Red Black Tree
  • Insert 20

15
10
85
70
20
49
Red Black Tree
  • Rotate Change Color

15
10
70
85
20
50
Red Black Tree
  • Insert 60

15
10
70
85
20
60
51
Red Black Tree
  • Change Color

15
10
70
85
20
60
52
Red Black Tree
  • Insert 30

15
10
70
85
20
60
30
53
Red Black Tree
  • Rotate

15
10
70
85
30
60
20
54
Red Black Tree
  • Insert 50

15
10
70
85
30
60
20
50
55
Red Black Tree
  • Insert 50

15
10
70
85
30
60
20
Oops, red-red. ROTATE!
50
56
Red Black Tree
  • Double Rotate Adjust colors

30
15
70
20
10
85
60
Child-Parent-Gramps Middle goes to top Previous
top becomes child.
50
57
Red Black Tree
  • Insert 65

30
15
70
20
10
85
60
65
50
58
Red Black Tree
  • Insert 80

30
15
70
20
10
85
60
80
65
50
59
Red Black Tree
  • Insert 90

30
15
70
20
10
85
60
90
80
65
50
60
Red Black Tree
  • Insert 40

30
15
70
20
10
85
60
90
80
65
50
40
61
Red Black Tree
  • Adjust color

30
15
70
20
10
85
60
90
80
65
50
40
62
Red Black Tree
  • Insert 5

30
15
70
20
10
85
60
5
90
80
65
50
40
63
Red Black Tree
  • Insert 55

30
15
70
20
10
85
60
5
90
80
65
50
40
55
Write a Comment
User Comments (0)
About PowerShow.com