CSE 326 Lecture 9: Splay Trees and B-Trees What's On Our Plate Today?
CSE 326 Lecture 9: Splay Trees and B-Trees What's On Our Plate Today?
element left
right
parent
2. When X is accessed, apply one of six rotation operations: Single Rotations (X has a Parent but no Grandparent) zig-left, zig-right Double Rotations (X has both a Parent and a Grandparent) zig-zig-left, zig-zig-right zig-zag-left, zig-zag-right
R. Rao, CSE 326 3
Zig-Right
! Zig-Right is just a single right rotation, as in an AVL tree ! Suppose R was the node that was accessed (e.g. using Find)
Zig-right
Zig-Left
! Suppose Q is accessed (e.g. using Find)
Zig-left
Zig-Zig
! Zig-Zig consists of two single rotations of the same type
Zig (Zig-right)
Zig (Zig-right)
! Note: Parent-Grandparent rotated first ! Zig-Zig Left is just Zig-Left followed by Zig-Left
R. Rao, CSE 326 6
Zig-Zag
! Zig-Zag consists of two rotations of the opposite type
Zig (Zig-left)
Zag (Zig-right)
! Note: R and Parent rotated first, R and Grandparent next ! The other Zig-Zag is just Zig-Right followed by Zig-Left
R. Rao, CSE 326 7
at the root and move the largest item in its left subtree to the root using splaying.
! Note on Find X: If X was not found, splay the leaf node that
13:-
Keys
6:11 17:-
Data
3 4
6 7 8
11 12
13 14
17 18
B-Trees
B-Trees are multi-way search trees commonly used in database systems or other applications where data is stored externally on disks and keeping the tree shallow is important. A B-Tree of order M has the following properties: 1. The root is either a leaf or has between 2 and M children. 2. All nonleaf nodes (except the root) have between !M/2" and M children. 3. All leaves are at the same depth. All data records are stored at the leaves. Leaves store between !L/2" and L data records. L depends on disk block size and data record size (e.g. L = M).
R. Rao, CSE 326 12
B-Tree Details
Each internal node of a B-tree has: " Between !M/2" and M children. " up to M-1 keys k1 # k2 #$%%%$# kM-1
k1
...
ki-1
ki . . .
kM-1
Properties of B-Trees
k1. . . ki-1 k i . . .k M-1 T 1 ... T i ... T M
Children of each internal node are "between" the items in that node. Suppose subtree Ti is the ith child of the node: All keys in Ti must be between ki-1 and ki
i.e. ki-1
&$Ti #$ki
ki-1 is the smallest key in Ti All keys in first subtree T1 #$k1 All keys in last subtree TM '$kM-1
R. Rao, CSE 326 14
B-trees Example
! B-tree of order 3: also known as 2-3 tree (2 to 3 children)
13:6:11 17:-
3 4
6 7 8
11 12
13 14
17 18
" Each node must have at least !M/2" = 2 and at most M = 3 children " Leaves store between !M/2" = 2 and M = 3 data records
15
" If leaf node is not full, fill in empty slot with X. E.g. Insert 5 in the tree below " If leaf node is full, split leaf node and adjust parents up to root node. E.g. Insert 9 in the tree below
13:6:11 17:-
6 7 8
11 12 -
13 14 -
17 18 16
" May have to combine leaf nodes and adjust parents up to root node if number of data items falls below !M/2" = 2 E.g. Delete 17 in the tree below
13:6:11 17:-
3 4 -
6 7 8
11 12 -
13 14 -
17 18 -
17
1. Each internal node has up to M-1 keys to search 2. Each internal node has between !M/2" and M children i.e. Depth of B-Tree storing N data items is O(log !M/2" N) (Why? Hint: Draw a B-tree with minimum children at each node. Count its leaves as a function of depth)
! Find: Run time is:
O(log M) to binary search which branch to take at each node Total time to find an item is O(depth*log M) = O(log N)
18
" O(M) to handle splitting or combining keys in nodes " Total time is O(depth*M) = O((log N/log !M/2" )*M) = O((M/log M)*log N)
! Tree in internal memory ! Tree on Disk
M = 3 or 4
M = 32 to 256. Interior and leaf nodes fit on allows very fast access to data in large
19
per node allows shallow trees; all leaves are at the same depth keeping tree balanced at all times
20
Next Class: Heaps on Heaps To Do: Read Chapter 6 Homework # 2 (due this Friday)
21