+ All Categories
Home > Documents > Multi-Way search Trees

Multi-Way search Trees

Date post: 03-Jan-2016
Category:
Upload: felicia-pittman
View: 19 times
Download: 0 times
Share this document with a friend
Description:
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. 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. Updating. - PowerPoint PPT Presentation
63
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.
Transcript
Page 1: Multi-Way search Trees

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: Multi-Way search Trees

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: Multi-Way search Trees

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);

Page 4: Multi-Way search Trees

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: Multi-Way search Trees

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: Multi-Way search Trees

Done….

20 80

3 5 30 70 90 100

2 10 15 23 25 40 50 75 85 95 110 1204

Page 7: Multi-Way search Trees

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: Multi-Way search Trees

• New root:

80

3 5 30 90 100

2 10 25 40 75 85 95 110 1204

45

20

50

70

Page 9: Multi-Way search Trees

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: Multi-Way search Trees

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: Multi-Way search Trees

Remove(80);• Redistribute

85

3 5 30 95 110

2 10 25 40 75 90 100 1204

45

20

50

70

Page 12: Multi-Way search Trees

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.1 110

Page 13: Multi-Way search Trees

Delete(70)

85

3 5 30 95 110

2 10 25 40 90 100 1204

45

20

50

75

Page 14: Multi-Way search Trees

New tree:• Delete(75) will “shrink” the tree.

95

3 5 30 110

2 10 25 40 90 100 1204

45

20

50

75

Page 15: Multi-Way search Trees

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.

Page 16: Multi-Way search Trees

“Shorter” 2-3 Tree

3 5 30 95 110

2 10 25 40 100 1204

20 45

50 90

Page 17: Multi-Way search Trees

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: Multi-Way search Trees

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: Multi-Way search Trees

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: Multi-Way search Trees

(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: Multi-Way search Trees

(2,4) Tree

10 45 60

3 8 25 50 55 70 90 100

Page 22: Multi-Way search Trees

Insert(38);

Insert(38);

3 8 25 38 50 55 70 90 100

45

10 60

Page 23: Multi-Way search Trees

Insert(105)

• Insert(105);

3 8 25 38 50 55 100 105

45

10 60 90

70

Page 24: Multi-Way search Trees

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: Multi-Way search Trees

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: Multi-Way search Trees

Delete(15)

35

20 60

6 15 40 50 70 80 90

Page 27: Multi-Way search Trees

Delete(15)

35

20 60

6 40 50 70 80 90

Page 28: Multi-Way search Trees

Continued

• Drop item from parent

35

60

6 20 40 50 70 80 90

Page 29: Multi-Way search Trees

Fuse

35

60

6 20 40 50 70 80 90

Page 30: Multi-Way search Trees

Drop item from root

• Remove root, return the child.

35 60

6 20 40 50 70 80 90

Page 31: Multi-Way search Trees

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.

Page 32: Multi-Way search Trees

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.

Page 33: Multi-Way search Trees

RedBlack

Insertion

Page 34: Multi-Way search Trees

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 35: Multi-Way search Trees

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

sibling.

a

b u

v w

zVl

ZlZr

Page 36: Multi-Way search Trees

Rotation• Z-middle key. Black height does not

change! No more red-red.

a

b z

v

w

u

Vl Zl Zr

Page 37: Multi-Way search Trees

Color adjustment II

a

b u

v w

z

Vr

ZlZr

Page 38: Multi-Way search Trees

Rotation II

a

b v

z u

ZrZlw

Vr

Page 39: Multi-Way search Trees

Recoloring

• Red child, red parent. Parent has a red sibling.

a

b u

v w

zVl

Zr

Page 40: Multi-Way search Trees

Recoloring

• Red-red may move up…

a

b u

v w

zVl

ZrZl

Page 41: Multi-Way search Trees

Red Black Tree

• Insert 10 – root

10

Page 42: Multi-Way search Trees

Red Black Tree

• Insert 10 – root

10

Page 43: Multi-Way search Trees

Red Black Tree

• Insert 85

10

85

Page 44: Multi-Way search Trees

Red Black Tree

• Insert 15

10

85

15

Page 45: Multi-Way search Trees

Red Black Tree

• Rotate – Change colors

15

10 85

Page 46: Multi-Way search Trees

Red Black Tree

• Insert 70

15

10 85

70

Page 47: Multi-Way search Trees

Red Black Tree

• Change Color

15

10 85

70

Page 48: Multi-Way search Trees

Red Black Tree

• Insert 20

20

15

10 85

70

Page 49: Multi-Way search Trees

Red Black Tree

• Rotate – Change Color

15

10 70

20 85

Page 50: Multi-Way search Trees

Red Black Tree

• Insert 60

15

10 70

20 85

60

Page 51: Multi-Way search Trees

Red Black Tree

• Change Color

15

10 70

20 85

60

Page 52: Multi-Way search Trees

Red Black Tree

• Insert 30

15

10 70

20 85

60

30

Page 53: Multi-Way search Trees

Red Black Tree

• Rotate

15

10 70

30 85

6020

Page 54: Multi-Way search Trees

Red Black Tree

• Insert 50

15

10 70

30 85

6020

50

Page 55: Multi-Way search Trees

Red Black Tree

• Insert 50

15

10 70

30 85

6020

50Oops, red-red. ROTATE!

Page 56: Multi-Way search Trees

Red Black Tree

• Double Rotate – Adjust colors

30

15 70

20 8510 60

50Child-Parent-Gramps

Middle goes to “top

Previous top becomes child.

Page 57: Multi-Way search Trees

Red Black Tree

• Insert 65

30

15 70

20 8510 60

6550

Page 58: Multi-Way search Trees

Red Black Tree

• Insert 80

30

15 70

20 8510 60

6550 80

Page 59: Multi-Way search Trees

Red Black Tree

• Insert 90

30

15 70

20 8510 60

6550 9080

Page 60: Multi-Way search Trees

Red Black Tree

• Insert 40

30

15 70

20 8510 60

6550

40

9080

Page 61: Multi-Way search Trees

Red Black Tree

• Adjust color

30

15 70

20 8510 60

6550

40

9080

Page 62: Multi-Way search Trees

Red Black Tree

• Insert 5

30

15 70

20 8510

5

60

6550

40

9080

Page 63: Multi-Way search Trees

Red Black Tree

• Insert 55

30

15 70

20 8510

5

60

6550

40

9080

55


Recommended