b trees algo notes
Yuxuan Wu Lv13

2,3 Trees

  1. All levels are at the same level
  2. Each internal node has either 2 or 3 children
  3. If it has:
    1. 2 children => it has 1 key
    2. 3 children => it has 2 keys

image-20211030153100739

Find operation (multiway searching)

image-20211030160307028

Insert operation

Condition 1

image-20211030160410010

Key rotation to address the overflow problems

Condition 2

2,3 Tree insertion - When key rotation fails

image-20211030160624658

Solutions: may have to recursively split nodes, working back to the root

image-20211030160731597

Condition 3

image-20211031160539669

image-20211030161742928

They would finally increase the depth by 1

a,b-trees

image-20211030161849940

Insertion and deletion

image-20211030161919775

B trees

  • A B-tree of order b is an a,b-tree with b =2a-1.
    • In other words, we choose the largest allowed a
    • Each node (page) is at least 50% full
  • Want to have large b if bringing a node into memory is slow (reading a disc block), but scanning the node once in memory is fast
  • B is usually chosen to match characteristics of the device

e.g. B-tree of order 1023 has a = 512

  • If this B-tree stores n = 10 million records, its height no more than O(log a n) = 2.58. So only around 3 blocks need to be read from disk

Definition

B-tree: Generalize 2-3 trees by allowing up to M-1 key-link pairs per node (M=2a)

  • At least 2 key-link pairs at root
  • At least M/2 key-link pairs in other nodes
  • External nodes contain client keys
  • Internal nodes contain copies of keys to guide search

image-20211031085749384

Searching in a B-tree

  1. Start at root
  2. Find interval for search key and take corresponding link
  3. Serach terminates in external node.

image-20211031085929844

Insertion in a B-tree

  1. Serach for new key
  2. Insert at boots
  3. Split nodes with M key-link pairs on the way up the tree

image-20211031091133699

Balance in B-tree

  • Proposition: A search or an insertion in a B-tree of order M with N keys requires between and probes
  • All internal nodes (besides root) have between M/2 and M-1 links
  • In practice. Number of probes is small, e.g. 4. (M = 1024; N = 62 billion, )

B is very large

  1. Need to be able to find which subtree to traverse
  2. Could linearly search through keys - technically constant time if b is a constant, but may be time consuming
  3. Possible solution: Store a balanced tree (say a splay tree) at each node so that you can search for keys efficiently
  • Post title:b trees algo notes
  • Post author:Yuxuan Wu
  • Create time:2021-10-30 15:27:23
  • Post link:yuxuanwu17.github.io2021/10/30/2021-10-30-b-trees-algo-notes/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.