Information about BTrees - Great alternative to Red Black, AVL and other BSTs

BTrees - designed by Rudolf Bayer and Ed McCreight - fundamental data structure in computer science. Great alternative to BSTs. Very appropriate for disk based access.

Instructor Prof. Amrinder Arora amrinder@gwu.edu Please copy TA on emails Please feel free to call as well TA Iswarya Parupudi iswarya2291@gwmail.gwu.edu CS 6213 - ADS - Arora L4 - BTrees 2

Record / Struct Basics Arrays / Linked Lists / Stacks / Queues Graphs / Trees / BSTs Trie, B-Tree CS 6213 Advanced Splay Trees Union Find Databases Applications Generics Multi Threading CS 6213 - ADS - Arora L4 - BTrees 3

T.K.Prasad @ Purdue University Prof. Sin-Min Lee @ San Jose State University Rada Mihalcea @ University of North Texas CS 6213 - ADS - Arora L4 - BTrees 4

Eventually you run out of RAM Plus, you need persistent storage Storing information on disk requires different approach to efficiency Access time includes seek time and rotational delay Assuming that a disk spins at 3600 RPM, one revolution occurs in 1/60 of a second, or 16.7ms. In other words, one disk access takes about the same time as 200,000 instructions CS 6213 - ADS - Arora L4 - BTrees 5

Assume that we use an AVL tree to store about 20 million records log2 20,000,000 is about 24 24 operations in terms of time is very small (4 GHz CPU, etc). Normal data operation should take a few nanoseconds. However, a large binary tree in a file will cause lots of different disk accesses 24 * 16.7ms = 0.4 seconds Suddenly query response time in seconds starts making sense. CS 6213 - ADS - Arora L4 - BTrees 6

We know we can’t improve on the log n lower bound on search for a binary tree But, the solution is to use more branches and thus reduce the height of the tree! As branching increases, depth decreases CS 6213 - ADS - Arora L4 - BTrees 7

Invented by Bayer and McCreight in 1972 (Bayer also invented Red Black Trees) Definition is in terms of “order”, which is not always clear, and different researchers mean different things, but concepts remain the same. We will use Knuth’s terminology, where order represents the maximum number of children. CS 6213 - ADS - Arora L4 - BTrees 8

B-tree of order m (where m is an odd number) is an m-way search tree, where keys partition the keys in the children in the fashion of a search tree, with following additional constraints: 1. [max] a node contains up to m – 1 keys and up to m children (Actual number of keys is one less than the number of children) 2. [min] all non-root nodes contain at least (m-1)/2 keys 3. [leaf level] all leaves are on the same level 4. [root] the root is either a leaf node, or it has at least two children CS 6213 - ADS - Arora L4 - BTrees 9

While as per Knuth’s definition B-Tree of order 5 is a tree where a node has a maximum of 5 children nodes, the same tree may be defined as a [2,4] tree in the sense that for any node, the number of keys is between 2 and 4, both inclusive. CS 6213 - ADS - Arora L4 - BTrees 10

A B-tree of order 5: • Root has at least 2 children • Every other non-leaf node has at least 2 keys and 3 children • Each leaf has at least two keys 35 6 12 42 1 2 4 38 7 40 8 13 45 46 48 15 18 55 62 32 53 51 60 61 64 75 92 [All the leaves are at the same level] CS 6213 - ADS - Arora L4 - BTrees 11

Different approach compared AVL Trees Don’t insert a new leaf, rather split the root and add a new level above the root. This automatically increases the height of ALL the leaves by one. CS 6213 - ADS - Arora L4 - BTrees 12

We want to construct a B-tree of order 5 Suppose we start with an empty B-tree and keys arrive in the following order: 1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45 The first four items go into the root: 1 CS 6213 - ADS - Arora 2 8 12 L4 - BTrees 13

To put the fifth item in the root would violate constraint 1 (max) Therefore, when 25 arrives, we pick the middle key to make a new root 8 1 CS 6213 - ADS - Arora 2 12 25 L4 - BTrees 14

6, 14, 28 get added to the leaf nodes 8 1 CS 6213 - ADS - Arora 2 6 12 14 25 28 L4 - BTrees 15

Adding 17 to the right leaf node would violate constraint 1 (max), so we promote the middle key (17) to the root and split the leaf 8 1 CS 6213 - ADS - Arora 2 6 17 12 14 25 28 L4 - BTrees 16

7, 52, 16, 48 get added to the leaf nodes 8 1 2 CS 6213 - ADS - Arora 6 7 12 17 14 16 25 28 48 52 L4 - BTrees 17

Adding 68 causes us to split the right most leaf, promoting 48 to the root, and adding 3 causes us to split the left most leaf, promoting 3 to the root; 26, 29, 53, 55 then go into the leaves 3 1 2 6 CS 6213 - ADS - Arora 7 8 12 14 17 16 48 25 26 28 29 52 53 55 68 L4 - BTrees 18

Adding 45 causes a split of 25 26 28 29 But we observe that this does not cause the problem of leaves at different heights. Rather, we promote 28 to go to the root. However, root is already full: 3 8 17 48 So, this causes the root to split: 17 then becomes the new root. CS 6213 - ADS - Arora L4 - BTrees 19

17 3 1 2 6 CS 6213 - ADS - Arora 7 8 28 12 14 16 25 26 29 48 45 52 53 55 68 L4 - BTrees 20

Attempt to insert the new key into a leaf If this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parent If this would result in the parent becoming too big, split the parent into two, promoting the middle key This strategy might have to be repeated all the way to the top If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher CS 6213 - ADS - Arora L4 - BTrees 21

Insert the following keys to a 5-way B-tree: 13, 27, 51, 3, 2, 14, 28, 1, 7, 71, 89, 37, 41, 44 CS 6213 - ADS - Arora L4 - BTrees 22

Scenario 1: • Key to delete is a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted. Scenario 2: • Key to delete is not in a leaf and moving its successor or predecessor does not cause the leaf node to have too few keys. (We are guaranteed by the nature of a B-tree that its predecessor or successor will be in a leaf.) Scenario 3: • Key to delete is a leaf node, but deleting it will have the leaf to have too few keys, and we can borrow from an adjacent leaf node. Scenario 4: • Key to delete is a leaf node, but deleting it will have the leaf to have too few keys, and we cannot borrow from an adjacent leaf node. Then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required CS 6213 - ADS - Arora L4 - BTrees 23

Scenario 1 12 29 52 2 7 9 15 22 31 43 56 69 72 We want to delete 2: Since there are enough keys in the node, we can just delete it CS 6213 - ADS - Arora L4 - BTrees 24

Scenario 1 12 29 52 7 9 15 22 31 43 56 69 72 That’s it, we deleted 2 and we are done. CS 6213 - ADS - Arora L4 - BTrees 25

Scenario 2 We want to delete 52. So, we delete it, and see that the successor can be moved up. 12 29 52 7 9 15 22 31 43 56 69 72 Borrow the predecessor or (in this case) successor CS 6213 - ADS - Arora L4 - BTrees 26

Scenario 2 Done. 52 is gone. 56 promoted to the non-leaf node. Leaf nodes are still meeting the min constraint. 12 29 56 7 9 15 22 31 43 69 72 Borrow the predecessor or (in this case) successor CS 6213 - ADS - Arora L4 - BTrees 27

Scenario 3 12 29 Demote root key and promote leaf key 7 9 15 22 31 43 56 69 We want to delete 22 – that will lead to too few keys in the node (constraint 2). But we can borrow from the adjacent node (via the root). CS 6213 - ADS - Arora L4 - BTrees 28

Scenario 3 12 31 7 9 15 29 43 56 69 Done – 22 is gone. 29 came down from the parent node, and 31 has gone up from the right adjacent node. CS 6213 - ADS - Arora L4 - BTrees 29

Scenario 4 12 29 56 7 9 15 22 31 43 69 72 We want to delete 72. This will lead to too few keys in this node (constraint 2). We cannot borrow from the adjacent sibling as it only has two. So, we need to combine 31, 43, 56 and 69 into one node. CS 6213 - ADS - Arora L4 - BTrees 30

Scenario 4 12 29 7 9 15 22 31 43 56 69 Done. 72 is gone. 31, 43, 56 and 69 combined into one node. CS 6213 - ADS - Arora L4 - BTrees 31

The maximum number of items in a B-tree of order m and height h: root level 1 level 2 . . . level h m–1 m(m – 1) m2(m – 1) mh(m – 1) So, the total number of items is (1 + m + m2 + m3 + … + mh)(m – 1) = [(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1 When m = 5 and h = 2 this gives 53 – 1 = 124 CS 6213 - ADS - Arora L4 - BTrees 32

Since there is a lower bound on the number of child nodes of non- root nodes, a B-Tree is at least 50% “full”. On average it is 75% full. The advantage of not being 100% full is that there are empty spaces for insertions to happen without going all the way to the root. CS 6213 - ADS - Arora L4 - BTrees 33

If m = 3, the specific case of B-Tree is called a 2-3 tree. For in memory access, 2-3 Tree may be a good alternative to Red Black or AVL Tree. CS 6213 - ADS - Arora L4 - BTrees 34

For small in-memory data structures, BSTs, Arrays, Hashmaps, etc. work well. When we exceed the size of the RAM (or for persistence reasons), the problem becomes quite different. The cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferred B-Trees are a great alternative (and very highly used) data structure for disk accesses A B-Tree of order m allows each node to have from m/2 up to m children. There is flexibility that allows for gaps. This flexibility allows: (i) some new elements to be stored in leaves with no other changes, and (ii) some elements to be deleted easily without changes propagating to root If we use a B-tree of order 101, a B-tree of order 101 and height 3 can hold 101 4 – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory) CS 6213 - ADS - Arora L4 - BTrees 35

If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys) B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree CS 6213 - ADS - Arora L4 - BTrees 36

BTrees – Great alternative to Red Black, AVL and other BSTs from Amrinder Arora. Related Posts. Self Organizing Data Structures - Splay Trees; Tries ...

Read more

BTrees – Great alternative to Red Black, AVL and other BSTs from Amrinder Arora. BTrees

Read more

BTrees - Great alternative to Red Black, AVL and other BSTs. 1,246 Views. altairhtcus. Integrated AVL EXCITE – Altair Engineering Software Solution for...

Read more

B-tree faster than AVL or RedBlack-Tree? ... An alternative to all these trees are AA-Trees. ... (AVL, Red/Black, B-Tree, an others) ...

Read more

Comparison to other structures. Both AVL trees and red-black trees are self-balancing binary search trees ... AVL tree; Red–black tree; Self-balancing ...

Read more

B-trees have substantial advantages over alternative ... an important consideration for databases and/or other B-tree ... AVL tree; Red–black tree;

Read more

We refer to 2-3-4 trees represented in this way as red–black BSTs. ... or AVL, tree, discovered in ... the same number of black nodes. Now, an ...

Read more

Classic Red-Black Trees [Guibas and Sedgewick, 1978] ... Put, and Get. Remove will be addressed for all BSTs at the end of this chapter. AVL Trees.

Read more

## Add a comment