简体   繁体   中英

B+ tree where keys are the sum of its children

I've been attempting to make a B+ Tree where each key is the sum of the keys of the corresponding child. The leaves would then contain a string, and its key would be the strings length. I'm basically trying to make a B+ Tree Rope.

Here is what I mean (This tree has an order of 4): 在此处输入图像描述

1 immediate problem I can see is that the keys are not sorted, which I believe is a requirement on B+ trees . (5, 7, 10 are, but that's just a coincidence). However, I'm not sure on how to make it so that the keys are sorted, while keeping the Rope like nature of this (which isn't sorted).

Or do I even need to keep each nodes keys sorted? Is the structure of the tree valid like this, and will the worst case time complexity of insertion and searching still be that of a regular B+ Tree ?

Another question I have is regarding insertion . If I insert a new chunk of text "abc", at index 11, which is right after the "n" (assuming I don't realloc the "n" buffer), then is what I would do is just:

  1. Detect overflow
  2. Take 3 and 2 into another node. (m / 2 nodes in first node)
  3. Move the 1 (which is "n") up into the root.
  4. The second node would simply contain 3 (abc), and 1 (g) (m / 2 nodes in second node)

The part that I'm confused about is #2. Since I can't move actual data into non-leaf nodes, what would I do?

Also, I noticed that the tree I'm using has a maximum of 4 keys, with a maximum of 4 children as well. However, In a regular B+ Tree , you can only have a maximum of 3 keys. Will this affect anything?

Any help would be greatly appreciated.

The tree does not need to be sorted so long as you can apply an order to the nodes (in this case the order is simply the text position within the container).

What you move is the node length, the only data you are using as your key. Say you have the following (one level tree (values representing node lengths):

4, 9, 2, 7

and you want to insert a node of length 8 before 2, you could end up with a tree:

  21, 9
4, 9, 8 2, 7

or

  13, 17
4, 9  8, 2, 7

I will note that I used a red-black tree instead of B+ tree when implementing rope simply because it is so much easier to implement while providing good enough balance.

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