Professional Documents
Culture Documents
By Mahmoud Ismail
Agenda
! Review of 2-4 Tree ! B Tree ! Dictionary and Map
22
27
(2,4) Trees
! A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-way
search with the following properties
! !
Node-Size Property: every internal node has at most four children Depth Property: all the external nodes have the same depth
10 15 24 2 8 12 18 27 32
12 14
12 14 15
Insert 15
let v1 v5 be the children of v and k1 k4 be the keys of v node v is replaced nodes v' and v"
" v' is a 3-node with keys k1 k2 and children v1 v2 v3 " v" is a 2-node with key k4 and children v4 v5
u
12 18
15 24 27 30 32 35
u v
12 18
15 24 32 27 30
v'
35
v" v5
v1 v2 v3 v4 v5
2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor: Greg Hager
v1 v2 v3 v4
11 12 14 15 Split 10 14 11 12
5
2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor: Greg Hager
15
Analysis of Insertion
! Algorithm insert(k, o) ! 1. ! 2.
We search for key k to locate the insertion node v We add the new entry (k, o) at node v
! 3. while overflow(v)
if isRoot(v) create a new empty root above v v ! split(v)
! !
Tree T has O(log n) height Step 1 takes O(log n) time because we visit O (log n) nodes Step 2 takes O(1) time Step 3 takes O(log n) time because each split takes O(1) time and we perform O(log n) splits
! Thus, an insertion in a
Deletion
Simple Case: Delete item from a leaf node
6 8 10 5 7 9 12 14 15
Remove 14 5
6 8 10 7 9 12 15
6 8 10 5 7 9 12 14 15
Remove 10 5 7
6 8 9 12 14 15
Swap 6 8 12 5 7 9 14 15
Transfer operation: 1. we move a child of w to v 2. we move an item from u to v 3. we move an item from w to u After a transfer, no underflow occurs
u 4 9
w 6 8
u 4 8 w 6
10
14 15
Fusion operation: we merge v with an adjacent sibling w and move an entry from u to the merged node v' After a fusion, the underflow may propagate to the parent u
u 2 5 7
9 14 10 w v
u 2 5 7
9 10 14 v'
Fusion 6 10 5
2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor: Greg Hager
8 9
12 14 15
Swap next larger element into hole (so weve removed element above an external) ! 4. If node has no elements If an adjacent sibling has > 1 element Perform transfer (kind of rotation) ! Else Perform fusion (can cascade upward)
CS 600.226: Data Structures, Professor: Greg Hager
Analysis of Deletion
! Let T be a (2,4) tree with n items
!
Tree T has O(log n) height We visit O(log n) nodes to locate the node from which to delete the entry We handle an underflow with a series of O(log n) fusions, followed by at most one transfer Each fusion and transfer takes O(1) time
! In a deletion operation
! !
(a,b) Trees
! Generalization of (2,4) trees ! Size property: internal node has at least a
children and at most b children
!
same depth ! Height of (a,b) tree is " (logn/logb) and O(logn/loga) CS 600.226: Data Structures,
2004 Goodrich, Tamassia Professor: Greg Hager
External Memory
2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor: Greg Hager
Primary Motivation
! External memory access much slower
than internal memory access
!
orders of magnitude slower ! need to minimize I/O Complexity ! can afford slightly more work on data in memory in exchange for lower I/O complexity
2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor: Greg Hager
B+ Trees
! Choose a and b to be #(B)
! Data are stored at leaves. ! All leaves are at the same depth
B+Trees
! Non Leaf nodes (except root) have between B/2 and B
children ! Root has between 2 and B children
! All leaves are at the same depth and have between L/2
B+ Tree
Example
B+ Tree
! Best case h is O(logBn) ! Worst Case h is O(logB/2n) ! I/O complexity for search is O(logBn)$