48
2-3-4 Tree 2 3 4 Tree B- Tree

Lecture16

Embed Size (px)

Citation preview

Page 1: Lecture16

2-3-4 Tree2 3 4 Tree

B- Tree

Page 2: Lecture16

Outline

l d S hBalanced Search Trees

• 2-3 Trees

• 2-3-4 Trees2 3 4 Trees

• B-Tree

CSCI 2720Slide 2

Page 3: Lecture16

2-3-4 Trees

• similar to 2-3 trees• 4-nodes can have 3 items and 4 children

4-node4-node

CSCI 2720Slide 3

Page 4: Lecture16

2-3-4 Tree Example

CSCI 2720Slide 4

Page 5: Lecture16

2-3-4 Tree: Insertion

Insertion procedure:

i il i i i 2 3 • similar to insertion in 2-3 trees

• items are inserted at the leafs

• since a 4-node cannot take another item,s ce a ode ca o a e a o e e ,4-nodes are split up during insertion process

Strategy

• on the way from the root down to the leaf:split up all 4-nodes "on the way"

insertion can be done in one passinsertion can be done in one pass(remember: in 2-3 trees, a reverse pass might be necessary)

CSCI 2720Slide 5

Page 6: Lecture16

2-3-4 Tree: Insertion

Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100

CSCI 2720Slide 6

Page 7: Lecture16

2-3-4 Tree: Insertion

Inserting 60, 30, 10, 20 ...

... 50, 40 ...

CSCI 2720Slide 7

Page 8: Lecture16

2-3-4 Tree: Insertion

Inserting 50, 40 ...

... 70, ...

CSCI 2720Slide 8

Page 9: Lecture16

2-3-4 Tree: Insertion

Inserting 70 ...

... 80, 15 ...

CSCI 2720Slide 9

Page 10: Lecture16

2-3-4 Tree: Insertion

Inserting 80, 15 ...

... 90 ...

CSCI 2720Slide 10

Page 11: Lecture16

2-3-4 Tree: Insertion

Inserting 90 ...

... 100 ...

CSCI 2720Slide 11

Page 12: Lecture16

2-3-4 Tree: Insertion

Inserting 100 ...

CSCI 2720Slide 12

Page 13: Lecture16

2-3-4 Tree: Insertion Procedure

Splitting 4-nodes during Insertion

CSCI 2720Slide 13

Page 14: Lecture16

2-3-4 Tree: Insertion Procedure

Splitting a 4-node whose parent is a 2-node during insertion

CSCI 2720Slide 14

Page 15: Lecture16

2-3-4 Tree: Insertion Procedure

Splitting a 4-node whose parent is a 3-node during insertion

CSCI 2720Slide 15

Page 16: Lecture16

2-3-4 Tree: Deletion

Deletion procedure:

• similar to deletion in 2-3 trees• similar to deletion in 2-3 trees

• items are deleted at the leafsswap item of internal node with inorder successor

• note: a 2-node leaf creates a problem

Strategy (different strategies possible)

• on the way from the root down to the leaf:turn 2-nodes (except root) into 3-nodes

deletion can be done in one pass(remember: in 2-3 trees, a reverse pass might be necessary)

CSCI 2720Slide 16

Page 17: Lecture16

2-3-4 Tree: Deletion

Turning a 2-node into a 3-node ...

Case 1: an adjacent sibling has 2 or 3 itemsCase 1: an adjacent sibling has 2 or 3 items"steal" item from sibling by rotating items and moving subtree

30 50 20 50

10 20 40 10 30 40

"rotation"

25 25

CSCI 2720Slide 17

Page 18: Lecture16

2-3-4 Tree: Deletion

Turning a 2-node into a 3-node ...

Case 2: each adjacent sibling has only one itemCase 2: each adjacent sibling has only one item"steal" item from parent and merge node with sibling(note: parent has at least two items, unless it is the root)

30 50 50

10 40merging

10 30 40

25 2535 35

CSCI 2720Slide 18

Page 19: Lecture16

2-3-4 Tree: Deletion Practice

Delete 32, 35, 40, 38, 39, 37, 60

CSCI 2720Slide 19

Page 20: Lecture16

B-Trees

• Large degree B-trees used to represent very large dictionaries that reside on disk.

• Smaller degree B-trees used for internal-memory dictionaries to overcome cache-miss penalties.

CSCI 2720Slide 20

Page 21: Lecture16

AVL Trees

• n = 230 = 109 (approx)n 2 10 (approx).• 30 <= height <= 43.

h h A id di k 43• When the AVL tree resides on a disk, up to 43disk access are made for a search.

• This takes up to (approx) 4 seconds.• Not acceptable.p

CSCI 2720Slide 21

Page 22: Lecture16

Red-Black Trees

• n = 230 = 109 (approx)n 2 10 (approx).• 30 <= height <= 60.

h h d bl k id di k• When the red-black tree resides on a disk, up to 60 disk access are made for a search.

• This takes up to (approx) 6 seconds.• Not acceptable.p

CSCI 2720Slide 22

Page 23: Lecture16

m-way Search Trees

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

CSCI 2720Slide 23

Page 24: Lecture16

4-Way Search Tree

10 30 10 30 35

k > k < 10

10 < k < 30

30 < k < 35

k > 35

CSCI 2720Slide 24

Page 25: Lecture16

Maximum # Of Pairs

• Happens when all internal nodes are m-nodes.Happens when all internal nodes are m nodes.• Full degree m tree.

# f d 1 + + 2 + 3 + + h 1• # of nodes = 1 + m + m2 + m3 + … + mh-1

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

CSCI 2720Slide 25

Page 26: Lecture16

Capacity Of m-Way Search TreeTree

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

h = 5 31 3.2 * 1011 - 1 16h = 7 127 1.28 * 1016 - 1

CSCI 2720Slide 26

Page 27: Lecture16

Definition Of B-Tree

• Definition assumes external nodes ( d d h )(extended m-way search tree).

• B-tree of order m.– m-way search tree.– Not empty => root has at least 2 children.p y– Remaining internal nodes (if any) have at least

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

CSCI 2720Slide 27

Page 28: Lecture16

2-3 And 2-3-4 Trees

• 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.

• 2-3 tree is B-tree of order 3. CSCI 2720

Slide 28

• 2-3-4 tree is B-tree of order 4.

Page 29: Lecture16

B-Trees Of Order 5 And 2

• 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.

• B-tree of order 5 is 3-4-5 tree (root b 2 d th h)may be 2-node though).

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

CSCI 2720Slide 29

Page 30: Lecture16

Minimum # Of Pairs

• n = # of pairs.• # of external nodes = n + 1.• Height = h => external nodes on level h + 1.

level # of

1 12 >= 2

nodes

23 >=

2*ceil(m/2)h + >= CSCI 2720

Slide 30

( / )h + 1 2*ceil(m/2)h-

1n + 1 >=

2*ceil(m/2)h-1 h >= 1

Page 31: Lecture16

Minimum # Of Pairs

n + 1 >= 2* il( /2)h 1 h 1

• m = 200.2*ceil(m/2)h-1, h >= 1

height

# of pairs2 >= p2 >= 1993 >= 19 9994 >= 2 * 106 – 119,9994 > 2 10 1

5 >= 2 * 108 – 1

CSCI 2720Slide 31

h <= log ceil(m/2)[(n+1)/2] + 1

Page 32: Lecture16

Choice Of m

• Worst-case search time.– (time to fetch a node + time to search node) * height– (a + b*m + c * log2m) * h( g2 )

where a, b and c are constants.

search sea ctime

CSCI 2720Slide 32m

50 400

Page 33: Lecture16

Insert

8

15 204

1 3

5 6

30 409 16 173 6

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

CSCI 2720Slide 33

bottom up node splitting pass.

Page 34: Lecture16

Split An Overfull Node

m a0 p1 a1 p2 a2 …p a• ai is a pointer to a subtree.• pi is a dictionary pair

pm am

pi is a dictionary pair.

ceil(m/2) 1 a p a p aceil(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• p plus pointer to new node

CSCI 2720Slide 34

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

Page 35: Lecture16

Insert88

4 15 204

1 3

5 6

30 409 16 17

• Insert a pair with key = 2.

• New pair goes into a 3-node.

CSCI 2720Slide 35

Page 36: Lecture16

Insert Into A Leaf 3-node

• Insert new pair so that the 3 keys are in ascending order.

• Split overflowed node around

1 2 3

Split overflowed node around middle key. 2

1 3

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

CSCI 2720Slide 36

p

Page 37: Lecture16

Insert

8

15 204

1 3

5 6

30 409 16 173 6

• Insert a pair with key = 2.

CSCI 2720Slide 37

Page 38: Lecture16

Insert

8

15 204 2

5 6

30 409 16 17

31

6

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

CSCI 2720Slide 38

Page 39: Lecture16

Insert

8

15 202 4

1

4

5 6

30 409 16 1736

• Now, insert a pair with key = 18.

CSCI 2720Slide 39

Page 40: Lecture16

Insert Into A Leaf 3-nodei h h k i• Insert new pair so that the 3 keys are in

ascending order.

• Split the overflowed node

16 17 18

• Split the overflowed node.

17

1816

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

CSCI 2720Slide 40

p

Page 41: Lecture16

Insert

8

15 202 4

1

4

5 6

30 409 16 1736

• Insert a pair with key = 18.

CSCI 2720Slide 41

Page 42: Lecture16

Insert

8

17

15 202 4

18

17

1

4

5 6

30 4093 166

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

CSCI 2720Slide 42

p p

Page 43: Lecture16

Insert

8 17

2 4

15 20

1

4

5 6

30 4093 16 186

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

CSCI 2720Slide 43

p p

Page 44: Lecture16

Insert8 17

2 4

15 20

1

4

5 6

30 4093 16 186

• Now, insert a pair with key = 7.

CSCI 2720Slide 44

Page 45: Lecture16

Insert8 17

2 4

15 207

6

30 401

4

93 16 185

7

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

CSCI 2720Slide 45

p p

Page 46: Lecture16

Insert

8 17 4

15 20

6

2

30 1 93 16 185 7

40

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

CSCI 2720Slide 46

p p

Page 47: Lecture16

Insert8

417

62

17

1 3

15 20

5 730 40

19

316 18

5 7

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

CSCI 2720Slide 47

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

Page 48: Lecture16

Insert8

4 17

15 2062

30 1 93 16 185 7

• Height increases by 1

401 3 5 7

CSCI 2720Slide 48

• Height increases by 1.