30
PART 11 Multiway Search Trees B-Trees Search Insertion Deletion

PART 11 Multiway Search Trees

  • Upload
    holleb

  • View
    52

  • Download
    6

Embed Size (px)

DESCRIPTION

PART 11 Multiway Search Trees. B-Trees Search Insertion Deletion. AVL Trees. n = 2 30 = 10 9 (approx). 30

Citation preview

Page 1: PART 11 Multiway Search Trees

PART 11Multiway Search Trees

• B-Trees• Search• Insertion• Deletion

Page 2: PART 11 Multiway Search Trees

AVL Trees

• n = 230 = 109 (approx).• 30 <= height <= 43.• When the AVL tree resides on a disk, up to

43 disk access are made for a search.• This takes up to (approx) 4 seconds.• Not acceptable.

Page 3: PART 11 Multiway Search Trees

m-way Search Trees

• Each node has up to m – 1 pairs and m children. • m = 2 => binary search tree.

Page 4: PART 11 Multiway Search Trees

4-Way Search Tree

10 30 35

k < 10 10 < k < 30 30 < k < 35 k > 35

Page 5: PART 11 Multiway Search Trees

Maximum # Of Pairs

• Happens when all internal nodes are m-nodes.• Full degree m tree.• # of nodes = 1 + m + m2 + m3 + … + mh-1

= (mh – 1)/(m – 1). • Each node has m – 1 pairs.• So, # of pairs = mh – 1.

Page 6: PART 11 Multiway Search Trees

Capacity Of m-Way Search Tree

m = 2 m = 200 h = 3 7 8 * 106 - 1

h = 5 31 3.2 * 1011 - 1

h = 7 127 1.28 * 1016 - 1

Page 7: PART 11 Multiway Search Trees

Definition Of B-Tree• Definition assumes external nodes

(extended m-way search tree).• B-tree of order m.

m-way search tree. Not empty => root has at least 2 children. Remaining internal nodes (if any) have at least

ceil(m/2) children. External (or failure) nodes on same level.

Page 8: PART 11 Multiway Search Trees

B Trees

• 2-3 tree is B-tree of order 3. • 2-3-4 tree is B-tree of order 4.• B-tree of order 5 is 3-4-5 tree (root may be 2-node

though).• B-tree of order 2 is full binary tree.

Page 9: PART 11 Multiway Search Trees

Insert

15 20

8

4

1 3 5 6 30 409

Insertion into a full leaf triggers bottom-up node splitting pass.

16 17

Page 10: PART 11 Multiway Search Trees

Split An Overfull Node

• ai is a pointer to a subtree.

• pi is a dictionary pair.

m a0 p1 a1 p2 a2 … pm am

ceil(m/2)-1 a0 p1 a1 p2 a2 … pceil(m/2)-1 aceil(m/2)-1

m-ceil(m/2) aceil(m/2) pceil(m/2)+1 aceil(m/2)+1 … pm am

• pceil(m/2) plus pointer to new node is inserted in parent.

Page 11: PART 11 Multiway Search Trees

Insert

15 20

8

4

1 3 5 6 30 409

• Insert a pair with key = 2.

• New pair goes into a 3-node.

16 17

Page 12: PART 11 Multiway Search Trees

Insert Into A Leaf 3-node• Insert new pair so that the 3 keys are in

ascending order.

• Split overflowed node around middle key.

1 2 3

• Insert middle key and pointer to new node into parent.

1 3

2

Page 13: PART 11 Multiway Search Trees

Insert

15 20

8

4

1 3 5 6 30 409

• Insert a pair with key = 2.

16 17

Page 14: PART 11 Multiway Search Trees

Insert

15 20

8

4

5 6 30 409 16 173

1

2

• Insert a pair with key = 2 plus a pointer into parent.

Page 15: PART 11 Multiway Search Trees

Insert

• Now, insert a pair with key = 18.

15 20

8

1

2 4

5 6 30 409 16 173

Page 16: PART 11 Multiway Search Trees

Insert Into A Leaf 3-node• Insert new pair so that the 3 keys are in

ascending order.

• Split the overflowed node.

16 17 18

• Insert middle key and pointer to new node into parent.

1816

17

Page 17: PART 11 Multiway Search Trees

Insert

• Insert a pair with key = 18.

15 20

8

1

2 4

5 6 30 409 16 173

Page 18: PART 11 Multiway Search Trees

Insert

• Insert a pair with key = 17 plus a pointer into parent.

15 20

8

1

2 4

5 6 30 4093

18

16

17

Page 19: PART 11 Multiway Search Trees

Insert

• Insert a pair with key = 17 plus a pointer into parent.

8

1

2 4

5 6 30 4093 16

17

15

18

20

Page 20: PART 11 Multiway Search Trees

Insert

• Now, insert a pair with key = 7.

1

2 4

5 6 30 4093 16

15

18

20

8 17

Page 21: PART 11 Multiway Search Trees

Insert

• Insert a pair with key = 6 plus a pointer into parent.

30 401

2 4

93 16

15

18

20

8 17

5

7

6

Page 22: PART 11 Multiway Search Trees

Insert

• Insert a pair with key = 4 plus a pointer into parent.

30 401 93 16

15

18

20

8 17

6

4

2

5 7

Page 23: PART 11 Multiway Search Trees

Insert

• Insert a pair with key = 8 plus a pointer into parent.

• There is no parent. So, create a new root.

30 401

93

16

15

18

206

8

2

5 7

417

Page 24: PART 11 Multiway Search Trees

Insert

• Height increases by 1.

30 401 93 16

15

18

2062

5 7

4 17

8

Page 25: PART 11 Multiway Search Trees

Delete A Pair• Deletion from interior node is transformed into

a deletion from a leaf node.• Deficient leaf triggers bottom-up borrowing

and node combining pass.• Deficient node is combined with an adjacent

sibling who has exactly ceil(m/2) – 1 pairs.• After combining, the node has [ceil(m/2) – 2]

(original pairs) + [ceil(m/2) – 1] (sibling pairs) + 1 (from parent) <= m –1 pairs.

Page 26: PART 11 Multiway Search Trees
Page 27: PART 11 Multiway Search Trees
Page 28: PART 11 Multiway Search Trees
Page 29: PART 11 Multiway Search Trees
Page 30: PART 11 Multiway Search Trees

Exercises

• Page 623, Problem 2• Page 623, Problem 3• Page 623, Problem 4• Page 623, Problem 5