Brendan Ang

Search

Search IconIcon to open search

B+ Tree Index

Last updated Nov 8, 2022 Edit Source

# B+ Tree Index

Idea: build a multi-layer index in the structure of a B-tree

  1. Each tree node is stored within a block
  2. Each node stores at most n+1 pointers and n keys
  3. Each level is an index - sorted within each node - sorted across nodes at the same level

[!Leaf Node Properties] Consider a leaf node storing k+1 pointers (k <= n) and k keys

  1. First k pointers are to records and last one is to the next leaf node
  2. Each key is equal to the key that its corresponding pointer is pointing to in the record

[!Internal Node Properties] Consider an internal node storing k+1 pointers (k <= n) and k keys

  1. The ith key is the lower bound of the range of the i+1 pointer. The 2nd pointer points to a subtree that has the first key as the first element:

# Validity

# Searching

  1. If search key is greater than ith key, follow i+1 pointer, else follow ith pointer

# Insertion

  1. Find which node to insert record
  2. If node is not full, insert the record (maintain sorted order)
  3. Else
    1. Split the node and distribute the keys
    2. If no parent, create root node
    3. Else, insert into parent recursively until no splits needed

# Deletion

Case 1: key can be deleted while maintaining constraints Case 2: key can be borrowed from sibling nodes, e.g. take 16 from left: Case 3: cannot borrow from siblings

  1. Merge 2 nodes and delete 1 of them
  2. Delete key from parent (one child is removed, may need to remove key from parent)
  3. Recursively apply delete on this parent if it is not full enough

# Construction

One way to do so is through a series of insert operations No sequential storage of leaf nodes but is more suitable for dynamic data.

# Bulk Loading

Leaves will be stored sequentially, will work for static data (all data is known before hand)

  1. Sort all data entries based on search key
  2. Start by creating all leaf nodes by packing the keys
  3. Insert internal nodes bottom up
## Practice Problems ![](https://i.imgur.com/WLEaTv1.png) a. 1. 5 2. 5 3. 4 4. 4 b. 1. Min key + 1 = 3 2. Min key + 1 = 3 3. Original keys: n. After split at least $\lfloor(n/2)\rfloor=2$ 4. $\lfloor(n+1)/2\rfloor=2$ ![](https://i.imgur.com/6QXYSgP.png)

Drawing 2022-09-05 10.35.50.excalidraw Height of the B+tree will be $log_{150}1000000=2.75$ Need at least 3 levels in the btree At most will take 3 I/O + 1 I/O to access data block