Skip to content

Commit

Permalink
bplus tree is working better
Browse files Browse the repository at this point in the history
  • Loading branch information
travierm committed Aug 17, 2024
1 parent d484eca commit fa1c7db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
10 changes: 5 additions & 5 deletions .archive/bplustree.go → bplustree.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ func (tree *BPlusTree) Insert(key int, value interface{}) {
index := sort.SearchInts(leaf.keys, key)
leaf.keys = append(leaf.keys[:index], append([]int{key}, leaf.keys[index:]...)...)

if len(leaf.keys) > int(tree.maxKeys) {
tree.splitNode(leaf)
}

if len(tree.root.keys) > int(tree.maxKeys) {
tree.SplitRoot()
return
}

if len(leaf.keys) > int(tree.maxKeys) {
tree.splitNode(leaf)
}
}

func (tree *BPlusTree) findLeaf(key int) *BPlusTreeNode {
Expand Down Expand Up @@ -149,7 +149,7 @@ func (tree *BPlusTree) SplitRoot() {
// split the node in two
mid := len(root.keys) / 2
leftKeys := root.keys[:mid]
rightKeys := root.keys[mid:]
rightKeys := root.keys[mid+1:]

leftChildren := []*BPlusTreeNode{}
rightChildren := []*BPlusTreeNode{}
Expand Down
39 changes: 32 additions & 7 deletions .archive/bplustree_test.go → bplustree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ func TestIsShapedCorrectlyAfter5(t *testing.T) {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}

tree.Print()
return

// root
assert.Equal(t, []int{3}, tree.root.keys)

Expand All @@ -91,12 +88,40 @@ func TestIsShapedCorrectlyAfter5(t *testing.T) {

// 3rd layer
// left
assert.Equal(t, []int{1}, tree.root.children[0].children[0])
assert.Equal(t, []int{2}, tree.root.children[0].children[1])
assert.Equal(t, []int{1}, tree.root.children[0].children[0].keys)
assert.Equal(t, []int{2}, tree.root.children[0].children[1].keys)

// right
assert.Equal(t, []int{3}, tree.root.children[1].children[0].keys)
assert.Equal(t, []int{4, 5}, tree.root.children[1].children[1].keys)

tree.Print()
}

func TestIsShapedCorrectlyAfter6(t *testing.T) {
tree := NewBPlusTree(3)
keys := []int{1, 2, 3, 4, 5, 6}

for _, key := range keys {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}

// root
assert.Equal(t, []int{3}, tree.root.keys)

// 2nd layer
assert.Equal(t, []int{2}, tree.root.children[0].keys) // right
assert.Equal(t, []int{4, 5}, tree.root.children[1].keys) // left

// 3rd layer
// left
assert.Equal(t, []int{1}, tree.root.children[0].children[0].keys)
assert.Equal(t, []int{2}, tree.root.children[0].children[1].keys)

// right
assert.Equal(t, []int{3}, tree.root.children[1].children[0])
assert.Equal(t, []int{4, 5}, tree.root.children[1].children[1])
assert.Equal(t, []int{3}, tree.root.children[1].children[0].keys)
assert.Equal(t, []int{4}, tree.root.children[1].children[1].keys)
assert.Equal(t, []int{5, 6}, tree.root.children[1].children[2].keys)

tree.Print()
}
Expand Down

0 comments on commit fa1c7db

Please sign in to comment.