Presentation is loading. Please wait.

Presentation is loading. Please wait.

B-Trees Text B-Tree Objects Building a B-Tree Read Weiss, §19.8

Similar presentations


Presentation on theme: "B-Trees Text B-Tree Objects Building a B-Tree Read Weiss, §19.8"— Presentation transcript:

1 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

2 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

3 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)

4 Motivation Simple overview of memory and disk storage
click on “Storage” Short paper comparing 512-byte vs 4K-byte sectors

5 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

6 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

7 Motivation Ideal Access Time Candidate Data Structure: B-Trees
24–33 accesses (3-6 seconds) still too long ideally 4-5 accesses ( seconds) Candidate Data Structure: B-Trees

8 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

9 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

10 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

11 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

12 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

13 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

14 Start with an empty B-Tree
root = null Start with an empty B-Tree

15 10 root = null Calling method passes to B-Tree insert() method a value to be inserted.

16 root 10 B-Tree object receives value, realizes tree is empty, and creates new empty Leaf.

17 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

18 Insert: 10 root 10 Leaf stores value.

19 Leaf maintains records in sorted order
Insert: 5 root 5 5 10 Leaf maintains records in sorted order

20 Leaf maintains records in sorted order
Insert: 7 root 7 5 7 10 Leaf maintains records in sorted order

21 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

22 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

23 Insert: 14 root 10 5 7 10 14 Old Leaf creates an Information Packet to return to parent. What must Packet contain?

24 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

25 Insert is complete and Packet is trashed.
root ● 10 ● _ ● 5 7 10 14 Insert is complete and Packet is trashed.

26 Root sends value to interior node.
Insert: 24 root 24 ● 10 ● _ ● 5 7 10 14 Root sends value to interior node.

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

28 Leaf receives and stores value.
Insert: 24 root ● 10 ● _ ● 5 7 10 14 24 Leaf receives and stores value.

29 Root sends value to interior node.
Insert: 17 root 17 ● 10 ● _ ● 5 7 10 14 24 Root sends value to interior node.

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

31 Leaf receives and stores value
Insert: 17 root ● 10 ● _ ● 5 7 10 14 17 24 Leaf receives and stores value

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

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

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

35 Insert is complete and packet is trashed.
root ● 10 ● 17 ● 5 7 10 14 17 24 Insert is complete and packet is trashed.

36 Insert: 6 root 6 ● 10 ● 17 ● 5 7 10 14 17 24

37 Insert: 6 root ● 10 ● 17 ● 6 5 7 10 14 17 24

38 Insert: 6 root ● 10 ● 17 ● 5 6 7 10 14 17 24

39 Insert: 4 root 4 ● 10 ● 17 ● 5 6 7 10 14 17 24

40 Insert: 4 root ● 10 ● 17 ● 4 5 6 7 10 14 17 24

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

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

43 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

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

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

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

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

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

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

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

51 Assignment #2 B-Trees

52

53


Download ppt "B-Trees Text B-Tree Objects Building a B-Tree Read Weiss, §19.8"

Similar presentations


Ads by Google