B-Trees Text B-Tree Objects Building a B-Tree Read Weiss, §19.8 Leaf, Interior Node, Information Packet, B-Tree Building a B-Tree Sequence of inserts Leaf splitting Node splitting Creation of root nodes Role of packet
Motivation Discussion and analysis of BST, AVL Trees, and Splay Trees assumed that trees were all contained in memory Unrealistic – databases typically larger than available memory Example: 10 million records ≈ 10M records = 10* 220 records each record has a total of 512 = 29 bytes/record total bytes required 10* 220 * 29 = 10 * 229 = 5Gbytes user is one of 20 on system application has 1/20 of resources 1/20 of a 32-Gbyte memory ≈ 1.6 Gbytes < 5Gbytes
Motivation Differences between memory and disk speeds Example: Memory 25 MIPS machine = 25 million instructions/sec Disk concentric tracks 3600 rev/sec one revolution in 1/60 sec=16.7 ms ignoring read/write head movement, ave access time = 8.3 ms more realistically, 9-11 ms access time 120 disk accesses/sec (typically gathers several clusters of data, where 1 cluster = multiple sectors of data)
Motivation Simple overview of memory and disk storage http://www.jegsworks.com/Lessons/lessonintro.htm click on “Storage” Short paper comparing 512-byte vs 4K-byte sectors http://www.usenix.org/publications/library/proceedings/fast02/wips/mccarthy.pdf
Motivation Number of accesses Assume N=10M log2 N = log2 10M ≈ 24 BST (worst case) = O(N) access 10M records BST (average case) = 1.38 log N accesses 1.38 * 24 ≈ 33 accesses AVL (worst case) = 1.44 log N accesses 1.44 * 24 ≈ 34 accesses AVL (average case) ≈ log N accesses ≈ 24 accesses
Motivation Access Time Assume each access takes total of 160 ms seek time + latency + data transmission time BST (worst case) 10M accesses = 10 * 220 * 160 ms = 19.4 days BST (average case) 33 accesses = 5.28 seconds AVL (worst case) 34 accesses = 5.44 seconds AVL (average case) 24 accesses = 3.84 seconds
Motivation Ideal Access Time Candidate Data Structure: B-Trees 24–33 accesses (3-6 seconds) still too long ideally 4-5 accesses (0.64 - 0.8 seconds) Candidate Data Structure: B-Trees
Definition A B-tree of order M is an M-ary tree such that: The data items are stored at the leaves The interior nodes store up to M−1 keys to guide the searching; key i represents the smallest key in subtree (i−1) The root is either a leaf or has between two and M children All interior nodes (except possibly the root) has between ┌ M/2┐ and M children All leaves are at the same depth and have between ┌ L/2┐ and L children
Contains records in sorted order Interior Node, M=3 Contains references to all children and smallest keys of all but one child subtree root B-Tree object ● 10 ● _ ● 17 Information Packet, this one contains a reference to a newly created Leaf and the smallest key in the leaf 5 7 __ 10 14 __ 17 24 __ Leaf, L=3 Contains records in sorted order
Choosing M Each node represents a disk block For example, let a block be 8K = 213 bytes An interior node can hold M−1 keys and M disk references Assume a key requires 32 bytes Assume a disk reference requires 4 bytes 32*(M−1) + 4*M ≤ 213 36*M − 32 ≤ 8192 M ≤ 228
Choosing L Each node represents a disk block Let a block be 8K = 213 bytes A leaf can hold L records Assume a record requires 256 bytes 256*L ≤ 213 28 * L ≤ 213 L ≤ 32
Example Step-Through Assume Order 3 B-Tree, i.e., L = M = 3 Each leaf can hold at most 3 records Each interior node can hold at most 2 keys and 3 references to children
Contains records in sorted order Cast of Objects Interior Node, M=3 Contains references to all children and smallest keys of all but one child subtree root B-Tree object ● 10 ● _ ● 17 Information Packet, this one contains a reference to a newly created Leaf and the smallest key in the leaf 5 7 __ 10 14 __ 17 24 __ Leaf, L=3 Contains records in sorted order
Start with an empty B-Tree root = null Start with an empty B-Tree
10 root = null Calling method passes to B-Tree insert() method a value to be inserted.
root 10 B-Tree object receives value, realizes tree is empty, and creates new empty Leaf.
Root sends value to Leaf, i.e., passes value to Leaf.insert() method 10 Root sends value to Leaf, i.e., passes value to Leaf.insert() method
Insert: 10 root 10 Leaf stores value.
Leaf maintains records in sorted order Insert: 5 root 5 5 10 Leaf maintains records in sorted order
Leaf maintains records in sorted order Insert: 7 root 7 5 7 10 Leaf maintains records in sorted order
Number of records in leaf greater than order (3) of B-Tree Insert: 14 root 14 5 7 10 14 Number of records in leaf greater than order (3) of B-Tree Leaf must split
Old Leaf creates new Leaf; moves greater values into new Leaf Insert: 14 root 5 7 10 14 Old Leaf creates new Leaf; moves greater values into new Leaf
Insert: 14 root 10 5 7 10 14 Old Leaf creates an Information Packet to return to parent. What must Packet contain?
Insert: 14 10 root ● 10 ● _ ● 5 7 10 14 Root receives Packet and uses it to create a new Interior Node which becomes new root
Insert is complete and Packet is trashed. root ● 10 ● _ ● 5 7 10 14 Insert is complete and Packet is trashed.
Root sends value to interior node. Insert: 24 root 24 ● 10 ● _ ● 5 7 10 14 Root sends value to interior node.
Interior Node determines to which of its children to send value. Insert: 24 root ● 10 ● _ ● 24 5 7 10 14 Interior Node determines to which of its children to send value.
Leaf receives and stores value. Insert: 24 root ● 10 ● _ ● 5 7 10 14 24 Leaf receives and stores value.
Root sends value to interior node. Insert: 17 root 17 ● 10 ● _ ● 5 7 10 14 24 Root sends value to interior node.
Interior node determines to which of its children to send value. Insert: 17 root ● 10 ● _ ● 17 5 7 10 14 24 Interior node determines to which of its children to send value.
Leaf receives and stores value Insert: 17 root ● 10 ● _ ● 5 7 10 14 17 24 Leaf receives and stores value
Insert: 17 root ● 10 ● _ ● 5 7 10 14 17 24 Leaf recognizes that it is overfull. It creates a new Leaf and moves the larger values into the new Leaf.
Leaf also creates Information Packet to return to parent. Insert: 17 root ● 10 ● _ ● 17 5 7 10 14 17 24 Leaf also creates Information Packet to return to parent.
Insert: 17 root ● 10 ● 17 ● 17 5 7 10 14 17 24 Interior Node receives Packet and uses it to set link to newly created Leaf.
Insert is complete and packet is trashed. root ● 10 ● 17 ● 5 7 10 14 17 24 Insert is complete and packet is trashed.
Insert: 6 root 6 ● 10 ● 17 ● 5 7 10 14 17 24
Insert: 6 root ● 10 ● 17 ● 6 5 7 10 14 17 24
Insert: 6 root ● 10 ● 17 ● 5 6 7 10 14 17 24
Insert: 4 root 4 ● 10 ● 17 ● 5 6 7 10 14 17 24
Insert: 4 root ● 10 ● 17 ● 4 5 6 7 10 14 17 24
Leaf recognizes that it is overfull. Insert: 4 root ● 10 ● 17 ● 4 5 6 7 10 14 17 24 Leaf recognizes that it is overfull.
Leaf creates new Leaf and moves larger values to new Leaf. Insert: 4 root ● 10 ● 17 ● 4 5 6 7 10 14 17 24 Leaf creates new Leaf and moves larger values to new Leaf.
Insert: 4 root ● 10 ● 17 ● 4 5 6 7 10 14 17 24 Leaf also creates an Information Packet that contains information about the new Leaf. Packet will be sent to old Leaf’s parent. 6
Insert: 4 root ● 6 ● 10 ● 17 ● 6 4 5 6 7 10 14 17 24 Interior Node uses information in the Packet it receives to update its key information.
Interior Node realizes that it is overfull. Insert: 4 root ● 6 ● 10 ● 17 ● 4 5 6 7 10 14 17 24 Interior Node realizes that it is overfull.
Insert: 4 root ● 6 ● 10 ● ● 17 ● _● 4 5 6 7 10 14 17 24 Interior Node creates new Node and stores larger value and links into new Node.
Insert: 4 root 10 ● 6 ● _ ● ● 17 ● _● 4 5 6 7 10 14 17 24 Interior Node also creates an Information Packet containing information about the new Node.
Insert: 4 10 root ● 6 ● _ ● ● 17 ● _● 4 5 6 7 10 14 17 24 B-Tree object receives Packet which tells it that it should create a new root Node.
B-Tree creates new root Node and initializes its links. 10 Insert: 4 ● 10 ● _● root ● 6 ● _ ● ● 17 ● _● 4 5 6 7 10 14 17 24 B-Tree creates new root Node and initializes its links.
Insert is complete and Packet is trashed. ● 10 ● _● root ● 6 ● _ ● ● 17 ● _● 4 5 6 7 10 14 17 24 Insert is complete and Packet is trashed.
Assignment #2 B-Trees