lecture16
TRANSCRIPT
2-3-4 Tree2 3 4 Tree
B- Tree
Outline
l d S hBalanced Search Trees
• 2-3 Trees
• 2-3-4 Trees2 3 4 Trees
• B-Tree
CSCI 2720Slide 2
2-3-4 Trees
• similar to 2-3 trees• 4-nodes can have 3 items and 4 children
4-node4-node
CSCI 2720Slide 3
2-3-4 Tree Example
CSCI 2720Slide 4
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
2-3-4 Tree: Insertion
Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100
CSCI 2720Slide 6
2-3-4 Tree: Insertion
Inserting 60, 30, 10, 20 ...
... 50, 40 ...
CSCI 2720Slide 7
2-3-4 Tree: Insertion
Inserting 50, 40 ...
... 70, ...
CSCI 2720Slide 8
2-3-4 Tree: Insertion
Inserting 70 ...
... 80, 15 ...
CSCI 2720Slide 9
2-3-4 Tree: Insertion
Inserting 80, 15 ...
... 90 ...
CSCI 2720Slide 10
2-3-4 Tree: Insertion
Inserting 90 ...
... 100 ...
CSCI 2720Slide 11
2-3-4 Tree: Insertion
Inserting 100 ...
CSCI 2720Slide 12
2-3-4 Tree: Insertion Procedure
Splitting 4-nodes during Insertion
CSCI 2720Slide 13
2-3-4 Tree: Insertion Procedure
Splitting a 4-node whose parent is a 2-node during insertion
CSCI 2720Slide 14
2-3-4 Tree: Insertion Procedure
Splitting a 4-node whose parent is a 3-node during insertion
CSCI 2720Slide 15
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
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
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
2-3-4 Tree: Deletion Practice
Delete 32, 35, 40, 38, 39, 37, 60
CSCI 2720Slide 19
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
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
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
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
4-Way Search Tree
10 30 10 30 35
k > k < 10
10 < k < 30
30 < k < 35
k > 35
CSCI 2720Slide 24
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
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
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
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.
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
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
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
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
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.
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.
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
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
Insert
8
15 204
1 3
5 6
30 409 16 173 6
• Insert a pair with key = 2.
CSCI 2720Slide 37
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
Insert
8
15 202 4
1
4
5 6
30 409 16 1736
• Now, insert a pair with key = 18.
CSCI 2720Slide 39
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
Insert
8
15 202 4
1
4
5 6
30 409 16 1736
• Insert a pair with key = 18.
CSCI 2720Slide 41
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
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
Insert8 17
2 4
15 20
1
4
5 6
30 4093 16 186
• Now, insert a pair with key = 7.
CSCI 2720Slide 44
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
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
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.
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.