简体   繁体   中英

splitting a B+ tree root

When splitting the root node of a b+ tree I know you take n/2 +1 and make that the new root and split everything accordingly.

My question lies when n is equal to an odd number. Like in this case, n = 5 .

So lets use a simple example:

   10  20  30  40 
 /   |   |   |   \

where all of the children are null. Lets say I want to add 50 to this.

would it look like

       30
      /  \
(10,20)   (40,50) 

or

           40
          /  \
 (10,20,30)  (50)

or something different?

If you split a node that contains n keys - including the incoming key that caused the split - then (n - 1) / 2 keys go to one new child, n - 1 - (n - 1) / 2 go to the other, and one key goes up to the parent level (as separator key). The key that goes up is not necessarily the same as the one that caused the split. If there is nowhere for the separator to go up to then you have a new root and the separator key will be its single lone occupant (the minimum occupancy requirement does not apply to root nodes).

Of course, the formulae look friendlier if you look at the rest that remains after taking out the new separator: r = n - 1 gives r / 2 for one side and r - r / 2 for the other.

In other words, under normal circumstances both 'halves' differ at most by one, which will occur if the total key count is even and hence leaves an odd rest when the separator key is taken out. It does not matter whether the extra key goes left or right.

However, this is not hammered in stone. There are other valid redistribution strategies, most notably Knuth's B* where two full nodes make three nodes that are already 2/3rds full, and so on. This also shows that the split/merge logic is strongly tied to the redistribution measures that do not change the structure, ie donation and borrowing of keys between siblings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM