1// Copyright (c) 2015, Emir Pasic. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// Package btree implements a B tree. 6// 7// According to Knuth's definition, a B-tree of order m is a tree which satisfies the following properties: 8// - Every node has at most m children. 9// - Every non-leaf node (except root) has at least ⌈m/2⌉ children. 10// - The root has at least two children if it is not a leaf node. 11// - A non-leaf node with k children contains k−1 keys. 12// - All leaves appear in the same level 13// 14// Structure is not thread safe. 15// 16// References: https://en.wikipedia.org/wiki/B-tree 17package btree 18 19import ( 20 "bytes" 21 "fmt" 22 "github.com/emirpasic/gods/trees" 23 "github.com/emirpasic/gods/utils" 24 "strings" 25) 26 27func assertTreeImplementation() { 28 var _ trees.Tree = (*Tree)(nil) 29} 30 31// Tree holds elements of the B-tree 32type Tree struct { 33 Root *Node // Root node 34 Comparator utils.Comparator // Key comparator 35 size int // Total number of keys in the tree 36 m int // order (maximum number of children) 37} 38 39// Node is a single element within the tree 40type Node struct { 41 Parent *Node 42 Entries []*Entry // Contained keys in node 43 Children []*Node // Children nodes 44} 45 46// Entry represents the key-value pair contained within nodes 47type Entry struct { 48 Key interface{} 49 Value interface{} 50} 51 52// NewWith instantiates a B-tree with the order (maximum number of children) and a custom key comparator. 53func NewWith(order int, comparator utils.Comparator) *Tree { 54 if order < 3 { 55 panic("Invalid order, should be at least 3") 56 } 57 return &Tree{m: order, Comparator: comparator} 58} 59 60// NewWithIntComparator instantiates a B-tree with the order (maximum number of children) and the IntComparator, i.e. keys are of type int. 61func NewWithIntComparator(order int) *Tree { 62 return NewWith(order, utils.IntComparator) 63} 64 65// NewWithStringComparator instantiates a B-tree with the order (maximum number of children) and the StringComparator, i.e. keys are of type string. 66func NewWithStringComparator(order int) *Tree { 67 return NewWith(order, utils.StringComparator) 68} 69 70// Put inserts key-value pair node into the tree. 71// If key already exists, then its value is updated with the new value. 72// Key should adhere to the comparator's type assertion, otherwise method panics. 73func (tree *Tree) Put(key interface{}, value interface{}) { 74 entry := &Entry{Key: key, Value: value} 75 76 if tree.Root == nil { 77 tree.Root = &Node{Entries: []*Entry{entry}, Children: []*Node{}} 78 tree.size++ 79 return 80 } 81 82 if tree.insert(tree.Root, entry) { 83 tree.size++ 84 } 85} 86 87// Get searches the node in the tree by key and returns its value or nil if key is not found in tree. 88// Second return parameter is true if key was found, otherwise false. 89// Key should adhere to the comparator's type assertion, otherwise method panics. 90func (tree *Tree) Get(key interface{}) (value interface{}, found bool) { 91 node, index, found := tree.searchRecursively(tree.Root, key) 92 if found { 93 return node.Entries[index].Value, true 94 } 95 return nil, false 96} 97 98// Remove remove the node from the tree by key. 99// Key should adhere to the comparator's type assertion, otherwise method panics. 100func (tree *Tree) Remove(key interface{}) { 101 node, index, found := tree.searchRecursively(tree.Root, key) 102 if found { 103 tree.delete(node, index) 104 tree.size-- 105 } 106} 107 108// Empty returns true if tree does not contain any nodes 109func (tree *Tree) Empty() bool { 110 return tree.size == 0 111} 112 113// Size returns number of nodes in the tree. 114func (tree *Tree) Size() int { 115 return tree.size 116} 117 118// Keys returns all keys in-order 119func (tree *Tree) Keys() []interface{} { 120 keys := make([]interface{}, tree.size) 121 it := tree.Iterator() 122 for i := 0; it.Next(); i++ { 123 keys[i] = it.Key() 124 } 125 return keys 126} 127 128// Values returns all values in-order based on the key. 129func (tree *Tree) Values() []interface{} { 130 values := make([]interface{}, tree.size) 131 it := tree.Iterator() 132 for i := 0; it.Next(); i++ { 133 values[i] = it.Value() 134 } 135 return values 136} 137 138// Clear removes all nodes from the tree. 139func (tree *Tree) Clear() { 140 tree.Root = nil 141 tree.size = 0 142} 143 144// Height returns the height of the tree. 145func (tree *Tree) Height() int { 146 return tree.Root.height() 147} 148 149// Left returns the left-most (min) node or nil if tree is empty. 150func (tree *Tree) Left() *Node { 151 return tree.left(tree.Root) 152} 153 154// LeftKey returns the left-most (min) key or nil if tree is empty. 155func (tree *Tree) LeftKey() interface{} { 156 if left := tree.Left(); left != nil { 157 return left.Entries[0].Key 158 } 159 return nil 160} 161 162// LeftValue returns the left-most value or nil if tree is empty. 163func (tree *Tree) LeftValue() interface{} { 164 if left := tree.Left(); left != nil { 165 return left.Entries[0].Value 166 } 167 return nil 168} 169 170// Right returns the right-most (max) node or nil if tree is empty. 171func (tree *Tree) Right() *Node { 172 return tree.right(tree.Root) 173} 174 175// RightKey returns the right-most (max) key or nil if tree is empty. 176func (tree *Tree) RightKey() interface{} { 177 if right := tree.Right(); right != nil { 178 return right.Entries[len(right.Entries)-1].Key 179 } 180 return nil 181} 182 183// RightValue returns the right-most value or nil if tree is empty. 184func (tree *Tree) RightValue() interface{} { 185 if right := tree.Right(); right != nil { 186 return right.Entries[len(right.Entries)-1].Value 187 } 188 return nil 189} 190 191// String returns a string representation of container (for debugging purposes) 192func (tree *Tree) String() string { 193 var buffer bytes.Buffer 194 if _, err := buffer.WriteString("BTree\n"); err != nil { 195 } 196 if !tree.Empty() { 197 tree.output(&buffer, tree.Root, 0, true) 198 } 199 return buffer.String() 200} 201 202func (entry *Entry) String() string { 203 return fmt.Sprintf("%v", entry.Key) 204} 205 206func (tree *Tree) output(buffer *bytes.Buffer, node *Node, level int, isTail bool) { 207 for e := 0; e < len(node.Entries)+1; e++ { 208 if e < len(node.Children) { 209 tree.output(buffer, node.Children[e], level+1, true) 210 } 211 if e < len(node.Entries) { 212 if _, err := buffer.WriteString(strings.Repeat(" ", level)); err != nil { 213 } 214 if _, err := buffer.WriteString(fmt.Sprintf("%v", node.Entries[e].Key) + "\n"); err != nil { 215 } 216 } 217 } 218} 219 220func (node *Node) height() int { 221 height := 0 222 for ; node != nil; node = node.Children[0] { 223 height++ 224 if len(node.Children) == 0 { 225 break 226 } 227 } 228 return height 229} 230 231func (tree *Tree) isLeaf(node *Node) bool { 232 return len(node.Children) == 0 233} 234 235func (tree *Tree) isFull(node *Node) bool { 236 return len(node.Entries) == tree.maxEntries() 237} 238 239func (tree *Tree) shouldSplit(node *Node) bool { 240 return len(node.Entries) > tree.maxEntries() 241} 242 243func (tree *Tree) maxChildren() int { 244 return tree.m 245} 246 247func (tree *Tree) minChildren() int { 248 return (tree.m + 1) / 2 // ceil(m/2) 249} 250 251func (tree *Tree) maxEntries() int { 252 return tree.maxChildren() - 1 253} 254 255func (tree *Tree) minEntries() int { 256 return tree.minChildren() - 1 257} 258 259func (tree *Tree) middle() int { 260 return (tree.m - 1) / 2 // "-1" to favor right nodes to have more keys when splitting 261} 262 263// search searches only within the single node among its entries 264func (tree *Tree) search(node *Node, key interface{}) (index int, found bool) { 265 low, high := 0, len(node.Entries)-1 266 var mid int 267 for low <= high { 268 mid = (high + low) / 2 269 compare := tree.Comparator(key, node.Entries[mid].Key) 270 switch { 271 case compare > 0: 272 low = mid + 1 273 case compare < 0: 274 high = mid - 1 275 case compare == 0: 276 return mid, true 277 } 278 } 279 return low, false 280} 281 282// searchRecursively searches recursively down the tree starting at the startNode 283func (tree *Tree) searchRecursively(startNode *Node, key interface{}) (node *Node, index int, found bool) { 284 if tree.Empty() { 285 return nil, -1, false 286 } 287 node = startNode 288 for { 289 index, found = tree.search(node, key) 290 if found { 291 return node, index, true 292 } 293 if tree.isLeaf(node) { 294 return nil, -1, false 295 } 296 node = node.Children[index] 297 } 298} 299 300func (tree *Tree) insert(node *Node, entry *Entry) (inserted bool) { 301 if tree.isLeaf(node) { 302 return tree.insertIntoLeaf(node, entry) 303 } 304 return tree.insertIntoInternal(node, entry) 305} 306 307func (tree *Tree) insertIntoLeaf(node *Node, entry *Entry) (inserted bool) { 308 insertPosition, found := tree.search(node, entry.Key) 309 if found { 310 node.Entries[insertPosition] = entry 311 return false 312 } 313 // Insert entry's key in the middle of the node 314 node.Entries = append(node.Entries, nil) 315 copy(node.Entries[insertPosition+1:], node.Entries[insertPosition:]) 316 node.Entries[insertPosition] = entry 317 tree.split(node) 318 return true 319} 320 321func (tree *Tree) insertIntoInternal(node *Node, entry *Entry) (inserted bool) { 322 insertPosition, found := tree.search(node, entry.Key) 323 if found { 324 node.Entries[insertPosition] = entry 325 return false 326 } 327 return tree.insert(node.Children[insertPosition], entry) 328} 329 330func (tree *Tree) split(node *Node) { 331 if !tree.shouldSplit(node) { 332 return 333 } 334 335 if node == tree.Root { 336 tree.splitRoot() 337 return 338 } 339 340 tree.splitNonRoot(node) 341} 342 343func (tree *Tree) splitNonRoot(node *Node) { 344 middle := tree.middle() 345 parent := node.Parent 346 347 left := &Node{Entries: append([]*Entry(nil), node.Entries[:middle]...), Parent: parent} 348 right := &Node{Entries: append([]*Entry(nil), node.Entries[middle+1:]...), Parent: parent} 349 350 // Move children from the node to be split into left and right nodes 351 if !tree.isLeaf(node) { 352 left.Children = append([]*Node(nil), node.Children[:middle+1]...) 353 right.Children = append([]*Node(nil), node.Children[middle+1:]...) 354 setParent(left.Children, left) 355 setParent(right.Children, right) 356 } 357 358 insertPosition, _ := tree.search(parent, node.Entries[middle].Key) 359 360 // Insert middle key into parent 361 parent.Entries = append(parent.Entries, nil) 362 copy(parent.Entries[insertPosition+1:], parent.Entries[insertPosition:]) 363 parent.Entries[insertPosition] = node.Entries[middle] 364 365 // Set child left of inserted key in parent to the created left node 366 parent.Children[insertPosition] = left 367 368 // Set child right of inserted key in parent to the created right node 369 parent.Children = append(parent.Children, nil) 370 copy(parent.Children[insertPosition+2:], parent.Children[insertPosition+1:]) 371 parent.Children[insertPosition+1] = right 372 373 tree.split(parent) 374} 375 376func (tree *Tree) splitRoot() { 377 middle := tree.middle() 378 379 left := &Node{Entries: append([]*Entry(nil), tree.Root.Entries[:middle]...)} 380 right := &Node{Entries: append([]*Entry(nil), tree.Root.Entries[middle+1:]...)} 381 382 // Move children from the node to be split into left and right nodes 383 if !tree.isLeaf(tree.Root) { 384 left.Children = append([]*Node(nil), tree.Root.Children[:middle+1]...) 385 right.Children = append([]*Node(nil), tree.Root.Children[middle+1:]...) 386 setParent(left.Children, left) 387 setParent(right.Children, right) 388 } 389 390 // Root is a node with one entry and two children (left and right) 391 newRoot := &Node{ 392 Entries: []*Entry{tree.Root.Entries[middle]}, 393 Children: []*Node{left, right}, 394 } 395 396 left.Parent = newRoot 397 right.Parent = newRoot 398 tree.Root = newRoot 399} 400 401func setParent(nodes []*Node, parent *Node) { 402 for _, node := range nodes { 403 node.Parent = parent 404 } 405} 406 407func (tree *Tree) left(node *Node) *Node { 408 if tree.Empty() { 409 return nil 410 } 411 current := node 412 for { 413 if tree.isLeaf(current) { 414 return current 415 } 416 current = current.Children[0] 417 } 418} 419 420func (tree *Tree) right(node *Node) *Node { 421 if tree.Empty() { 422 return nil 423 } 424 current := node 425 for { 426 if tree.isLeaf(current) { 427 return current 428 } 429 current = current.Children[len(current.Children)-1] 430 } 431} 432 433// leftSibling returns the node's left sibling and child index (in parent) if it exists, otherwise (nil,-1) 434// key is any of keys in node (could even be deleted). 435func (tree *Tree) leftSibling(node *Node, key interface{}) (*Node, int) { 436 if node.Parent != nil { 437 index, _ := tree.search(node.Parent, key) 438 index-- 439 if index >= 0 && index < len(node.Parent.Children) { 440 return node.Parent.Children[index], index 441 } 442 } 443 return nil, -1 444} 445 446// rightSibling returns the node's right sibling and child index (in parent) if it exists, otherwise (nil,-1) 447// key is any of keys in node (could even be deleted). 448func (tree *Tree) rightSibling(node *Node, key interface{}) (*Node, int) { 449 if node.Parent != nil { 450 index, _ := tree.search(node.Parent, key) 451 index++ 452 if index < len(node.Parent.Children) { 453 return node.Parent.Children[index], index 454 } 455 } 456 return nil, -1 457} 458 459// delete deletes an entry in node at entries' index 460// ref.: https://en.wikipedia.org/wiki/B-tree#Deletion 461func (tree *Tree) delete(node *Node, index int) { 462 // deleting from a leaf node 463 if tree.isLeaf(node) { 464 deletedKey := node.Entries[index].Key 465 tree.deleteEntry(node, index) 466 tree.rebalance(node, deletedKey) 467 if len(tree.Root.Entries) == 0 { 468 tree.Root = nil 469 } 470 return 471 } 472 473 // deleting from an internal node 474 leftLargestNode := tree.right(node.Children[index]) // largest node in the left sub-tree (assumed to exist) 475 leftLargestEntryIndex := len(leftLargestNode.Entries) - 1 476 node.Entries[index] = leftLargestNode.Entries[leftLargestEntryIndex] 477 deletedKey := leftLargestNode.Entries[leftLargestEntryIndex].Key 478 tree.deleteEntry(leftLargestNode, leftLargestEntryIndex) 479 tree.rebalance(leftLargestNode, deletedKey) 480} 481 482// rebalance rebalances the tree after deletion if necessary and returns true, otherwise false. 483// Note that we first delete the entry and then call rebalance, thus the passed deleted key as reference. 484func (tree *Tree) rebalance(node *Node, deletedKey interface{}) { 485 // check if rebalancing is needed 486 if node == nil || len(node.Entries) >= tree.minEntries() { 487 return 488 } 489 490 // try to borrow from left sibling 491 leftSibling, leftSiblingIndex := tree.leftSibling(node, deletedKey) 492 if leftSibling != nil && len(leftSibling.Entries) > tree.minEntries() { 493 // rotate right 494 node.Entries = append([]*Entry{node.Parent.Entries[leftSiblingIndex]}, node.Entries...) // prepend parent's separator entry to node's entries 495 node.Parent.Entries[leftSiblingIndex] = leftSibling.Entries[len(leftSibling.Entries)-1] 496 tree.deleteEntry(leftSibling, len(leftSibling.Entries)-1) 497 if !tree.isLeaf(leftSibling) { 498 leftSiblingRightMostChild := leftSibling.Children[len(leftSibling.Children)-1] 499 leftSiblingRightMostChild.Parent = node 500 node.Children = append([]*Node{leftSiblingRightMostChild}, node.Children...) 501 tree.deleteChild(leftSibling, len(leftSibling.Children)-1) 502 } 503 return 504 } 505 506 // try to borrow from right sibling 507 rightSibling, rightSiblingIndex := tree.rightSibling(node, deletedKey) 508 if rightSibling != nil && len(rightSibling.Entries) > tree.minEntries() { 509 // rotate left 510 node.Entries = append(node.Entries, node.Parent.Entries[rightSiblingIndex-1]) // append parent's separator entry to node's entries 511 node.Parent.Entries[rightSiblingIndex-1] = rightSibling.Entries[0] 512 tree.deleteEntry(rightSibling, 0) 513 if !tree.isLeaf(rightSibling) { 514 rightSiblingLeftMostChild := rightSibling.Children[0] 515 rightSiblingLeftMostChild.Parent = node 516 node.Children = append(node.Children, rightSiblingLeftMostChild) 517 tree.deleteChild(rightSibling, 0) 518 } 519 return 520 } 521 522 // merge with siblings 523 if rightSibling != nil { 524 // merge with right sibling 525 node.Entries = append(node.Entries, node.Parent.Entries[rightSiblingIndex-1]) 526 node.Entries = append(node.Entries, rightSibling.Entries...) 527 deletedKey = node.Parent.Entries[rightSiblingIndex-1].Key 528 tree.deleteEntry(node.Parent, rightSiblingIndex-1) 529 tree.appendChildren(node.Parent.Children[rightSiblingIndex], node) 530 tree.deleteChild(node.Parent, rightSiblingIndex) 531 } else if leftSibling != nil { 532 // merge with left sibling 533 entries := append([]*Entry(nil), leftSibling.Entries...) 534 entries = append(entries, node.Parent.Entries[leftSiblingIndex]) 535 node.Entries = append(entries, node.Entries...) 536 deletedKey = node.Parent.Entries[leftSiblingIndex].Key 537 tree.deleteEntry(node.Parent, leftSiblingIndex) 538 tree.prependChildren(node.Parent.Children[leftSiblingIndex], node) 539 tree.deleteChild(node.Parent, leftSiblingIndex) 540 } 541 542 // make the merged node the root if its parent was the root and the root is empty 543 if node.Parent == tree.Root && len(tree.Root.Entries) == 0 { 544 tree.Root = node 545 node.Parent = nil 546 return 547 } 548 549 // parent might underflow, so try to rebalance if necessary 550 tree.rebalance(node.Parent, deletedKey) 551} 552 553func (tree *Tree) prependChildren(fromNode *Node, toNode *Node) { 554 children := append([]*Node(nil), fromNode.Children...) 555 toNode.Children = append(children, toNode.Children...) 556 setParent(fromNode.Children, toNode) 557} 558 559func (tree *Tree) appendChildren(fromNode *Node, toNode *Node) { 560 toNode.Children = append(toNode.Children, fromNode.Children...) 561 setParent(fromNode.Children, toNode) 562} 563 564func (tree *Tree) deleteEntry(node *Node, index int) { 565 copy(node.Entries[index:], node.Entries[index+1:]) 566 node.Entries[len(node.Entries)-1] = nil 567 node.Entries = node.Entries[:len(node.Entries)-1] 568} 569 570func (tree *Tree) deleteChild(node *Node, index int) { 571 if index >= len(node.Children) { 572 return 573 } 574 copy(node.Children[index:], node.Children[index+1:]) 575 node.Children[len(node.Children)-1] = nil 576 node.Children = node.Children[:len(node.Children)-1] 577} 578