github.com/alexandrestein/gods@v1.0.1/trees/btree/btree.go (about) 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 17 package btree 18 19 import ( 20 "bytes" 21 "fmt" 22 "strings" 23 24 "github.com/alexandrestein/gods/trees" 25 "github.com/alexandrestein/gods/utils" 26 ) 27 28 func assertTreeImplementation() { 29 var _ trees.Tree = (*Tree)(nil) 30 } 31 32 // Tree holds elements of the B-tree 33 type Tree struct { 34 Root *Node // Root node 35 Comparator utils.Comparator // Key comparator 36 ComparatorType utils.ComparatorType // Type comparator 37 size int // Total number of keys in the tree 38 m int // order (maximum number of children) 39 } 40 41 // Node is a single element within the tree 42 type Node struct { 43 Parent *Node 44 Entries []*Entry // Contained keys in node 45 Children []*Node // Children nodes 46 } 47 48 // Entry represents the key-value pair contained within nodes 49 type Entry struct { 50 Key interface{} 51 Value interface{} 52 } 53 54 // NewWith instantiates a B-tree with the order (maximum number of children) and a custom key comparator. 55 func NewWith(order int, comparator utils.Comparator, comparatorType utils.ComparatorType) *Tree { 56 if order < 3 { 57 panic("Invalid order, should be at least 3") 58 } 59 return &Tree{m: order, Comparator: comparator, ComparatorType: comparatorType} 60 } 61 62 // NewWithIntComparator instantiates a B-tree with the order (maximum number of children) and the IntComparator, i.e. keys are of type int. 63 func NewWithIntComparator(order int) *Tree { 64 return NewWith(order, utils.IntComparator, utils.IntComparatorType) 65 } 66 67 // NewWithStringComparator instantiates a B-tree with the order (maximum number of children) and the StringComparator, i.e. keys are of type string. 68 func NewWithStringComparator(order int) *Tree { 69 return NewWith(order, utils.StringComparator, utils.StringComparatorType) 70 } 71 72 // Put inserts key-value pair node into the tree. 73 // If key already exists, then its value is updated with the new value. 74 // Key should adhere to the comparator's type assertion, otherwise method panics. 75 func (tree *Tree) Put(key interface{}, value interface{}) { 76 entry := &Entry{Key: key, Value: value} 77 78 if tree.Root == nil { 79 tree.Root = &Node{Entries: []*Entry{entry}, Children: []*Node{}} 80 tree.size++ 81 return 82 } 83 84 if tree.insert(tree.Root, entry) { 85 tree.size++ 86 } 87 } 88 89 // Get searches the node in the tree by key and returns its value or nil if key is not found in tree. 90 // Second return parameter is true if key was found, otherwise false. 91 // Key should adhere to the comparator's type assertion, otherwise method panics. 92 func (tree *Tree) Get(key interface{}) (value interface{}, found bool) { 93 node, index, found := tree.searchRecursively(tree.Root, key) 94 if found { 95 return node.Entries[index].Value, true 96 } 97 return nil, false 98 } 99 100 // GetClosest does the same as above but if the given key does not exists, 101 // it tries to find the elements next to it. 102 // If the given key exists, it returns twice the value and found is true. 103 // If the given key is between tow other keys the previous value will be first and the next at the second place. 104 // If the given key is less than the first key, it returns prev as nil and next as the first value. 105 // If the given key is higher than the last key, it returns prev as the last value and next as nil. 106 // If empty it returns nil, nil, false. 107 func (tree *Tree) GetClosest(key interface{}) (prev, next interface{}, found bool) { 108 return tree.getClosest(key, false) 109 } 110 111 // GetClosestKeys does the same as above but returns the keys 112 func (tree *Tree) GetClosestKeys(key interface{}) (prev, next interface{}, found bool) { 113 return tree.getClosest(key, true) 114 } 115 116 func (tree *Tree) getClosest(key interface{}, keyWanted bool) (prev, next interface{}, found bool) { 117 // The first part of the function is almost a clone of tree.searchRecursively 118 if tree.Empty() { 119 return nil, nil, false 120 } 121 122 iterator, found := tree.IteratorAt(key) 123 if found { 124 if keyWanted { 125 return iterator.Key(), iterator.Key(), true 126 } 127 return iterator.Value(), iterator.Value(), true 128 } 129 130 prevKey, prevValue, nextKey, nextValue, found := tree.findClosestWithIterator(key, iterator) 131 if keyWanted { 132 return prevKey, nextKey, found 133 } 134 return prevValue, nextValue, found 135 } 136 137 // IteratorAt gets the iterator at the key if found or just before the wanted 138 // value if afterWanted is not true. 139 // If true it returns the first value after if any otherways it returnes 140 // the last value of the tree. 141 func (tree *Tree) IteratorAt(key interface{}) (Iterator, bool) { 142 node := tree.Root 143 var index int 144 var found bool 145 146 for { 147 // Found the value in the active node 148 index, found = tree.search(node, key) 149 if found { 150 // Returns the interator at the right position 151 return Iterator{ 152 tree: tree, 153 node: node, 154 entry: node.Entries[index], 155 position: between, 156 }, true 157 } 158 159 // The key is not in the node but this is a leaf node. 160 // This means that the key is not in the tree and the returned pointer will 161 // be at the first position of the closest keys. 162 if tree.isLeaf(node) { 163 iter := tree.Iterator() 164 iter.Next() 165 return iter, false 166 } 167 168 // Update the active node with the right child. 169 node = node.Children[index] 170 } 171 } 172 173 func (tree *Tree) findClosestWithIterator(key interface{}, iterator Iterator) (prevKey, prevValue, nextKey, nextValue interface{}, found bool) { 174 // Reads given node to define the value right after the asked key. 175 for { 176 // If comparator returns a negative value, this means that 177 // the given key is after the looking key. 178 if tree.Comparator(key, iterator.Key()) < 0 { 179 nextKey = iterator.Key() 180 nextValue = iterator.Value() 181 break 182 } 183 184 // If last there is no after value but we find the value just before. 185 if !iterator.Next() { 186 iterator.Prev() 187 prevKey = iterator.Key() 188 prevValue = iterator.Value() 189 return 190 } 191 } 192 // Gets the previous value. If not ok there is not previous value. 193 if !iterator.Prev() { 194 return 195 } 196 197 prevKey = iterator.Key() 198 prevValue = iterator.Value() 199 200 return 201 } 202 203 // Remove remove the node from the tree by key. 204 // Key should adhere to the comparator's type assertion, otherwise method panics. 205 func (tree *Tree) Remove(key interface{}) { 206 node, index, found := tree.searchRecursively(tree.Root, key) 207 if found { 208 tree.delete(node, index) 209 tree.size-- 210 } 211 } 212 213 // Empty returns true if tree does not contain any nodes 214 func (tree *Tree) Empty() bool { 215 return tree.size == 0 216 } 217 218 // Size returns number of nodes in the tree. 219 func (tree *Tree) Size() int { 220 return tree.size 221 } 222 223 // Keys returns all keys in-order 224 func (tree *Tree) Keys() []interface{} { 225 keys := make([]interface{}, tree.size) 226 it := tree.Iterator() 227 for i := 0; it.Next(); i++ { 228 keys[i] = it.Key() 229 } 230 return keys 231 } 232 233 // Values returns all values in-order based on the key. 234 func (tree *Tree) Values() []interface{} { 235 values := make([]interface{}, tree.size) 236 it := tree.Iterator() 237 for i := 0; it.Next(); i++ { 238 values[i] = it.Value() 239 } 240 return values 241 } 242 243 // Clear removes all nodes from the tree. 244 func (tree *Tree) Clear() { 245 tree.Root = nil 246 tree.size = 0 247 } 248 249 // Height returns the height of the tree. 250 func (tree *Tree) Height() int { 251 return tree.Root.height() 252 } 253 254 // Left returns the left-most (min) node or nil if tree is empty. 255 func (tree *Tree) Left() *Node { 256 return tree.left(tree.Root) 257 } 258 259 // LeftKey returns the left-most (min) key or nil if tree is empty. 260 func (tree *Tree) LeftKey() interface{} { 261 if left := tree.Left(); left != nil { 262 return left.Entries[0].Key 263 } 264 return nil 265 } 266 267 // LeftValue returns the left-most value or nil if tree is empty. 268 func (tree *Tree) LeftValue() interface{} { 269 if left := tree.Left(); left != nil { 270 return left.Entries[0].Value 271 } 272 return nil 273 } 274 275 // Right returns the right-most (max) node or nil if tree is empty. 276 func (tree *Tree) Right() *Node { 277 return tree.right(tree.Root) 278 } 279 280 // RightKey returns the right-most (max) key or nil if tree is empty. 281 func (tree *Tree) RightKey() interface{} { 282 if right := tree.Right(); right != nil { 283 return right.Entries[len(right.Entries)-1].Key 284 } 285 return nil 286 } 287 288 // RightValue returns the right-most value or nil if tree is empty. 289 func (tree *Tree) RightValue() interface{} { 290 if right := tree.Right(); right != nil { 291 return right.Entries[len(right.Entries)-1].Value 292 } 293 return nil 294 } 295 296 // String returns a string representation of container (for debugging purposes) 297 func (tree *Tree) String() string { 298 var buffer bytes.Buffer 299 if _, err := buffer.WriteString("BTree\n"); err != nil { 300 } 301 if !tree.Empty() { 302 tree.output(&buffer, tree.Root, 0, true) 303 } 304 return buffer.String() 305 } 306 307 func (entry *Entry) String() string { 308 return fmt.Sprintf("%v", entry.Key) 309 } 310 311 func (tree *Tree) output(buffer *bytes.Buffer, node *Node, level int, isTail bool) { 312 for e := 0; e < len(node.Entries)+1; e++ { 313 if e < len(node.Children) { 314 tree.output(buffer, node.Children[e], level+1, true) 315 } 316 if e < len(node.Entries) { 317 if _, err := buffer.WriteString(strings.Repeat(" ", level)); err != nil { 318 } 319 if _, err := buffer.WriteString(fmt.Sprintf("%v", node.Entries[e].Key) + "\n"); err != nil { 320 } 321 } 322 } 323 } 324 325 func (node *Node) height() int { 326 height := 0 327 for ; node != nil; node = node.Children[0] { 328 height++ 329 if len(node.Children) == 0 { 330 break 331 } 332 } 333 return height 334 } 335 336 func (tree *Tree) isLeaf(node *Node) bool { 337 return len(node.Children) == 0 338 } 339 340 func (tree *Tree) isFull(node *Node) bool { 341 return len(node.Entries) == tree.maxEntries() 342 } 343 344 func (tree *Tree) shouldSplit(node *Node) bool { 345 return len(node.Entries) > tree.maxEntries() 346 } 347 348 func (tree *Tree) maxChildren() int { 349 return tree.m 350 } 351 352 func (tree *Tree) minChildren() int { 353 return (tree.m + 1) / 2 // ceil(m/2) 354 } 355 356 func (tree *Tree) maxEntries() int { 357 return tree.maxChildren() - 1 358 } 359 360 func (tree *Tree) minEntries() int { 361 return tree.minChildren() - 1 362 } 363 364 func (tree *Tree) middle() int { 365 return (tree.m - 1) / 2 // "-1" to favor right nodes to have more keys when splitting 366 } 367 368 // search searches only within the single node among its entries 369 func (tree *Tree) search(node *Node, key interface{}) (index int, found bool) { 370 low, high := 0, len(node.Entries)-1 371 var mid int 372 for low <= high { 373 mid = (high + low) / 2 374 compare := tree.Comparator(key, node.Entries[mid].Key) 375 switch { 376 case compare > 0: 377 low = mid + 1 378 case compare < 0: 379 high = mid - 1 380 case compare == 0: 381 return mid, true 382 } 383 } 384 return low, false 385 } 386 387 // searchRecursively searches recursively down the tree starting at the startNode 388 func (tree *Tree) searchRecursively(startNode *Node, key interface{}) (node *Node, index int, found bool) { 389 if tree.Empty() { 390 return nil, -1, false 391 } 392 node = startNode 393 for { 394 index, found = tree.search(node, key) 395 if found { 396 return node, index, true 397 } 398 if tree.isLeaf(node) { 399 return nil, -1, false 400 } 401 node = node.Children[index] 402 } 403 } 404 405 func (tree *Tree) insert(node *Node, entry *Entry) (inserted bool) { 406 if tree.isLeaf(node) { 407 return tree.insertIntoLeaf(node, entry) 408 } 409 return tree.insertIntoInternal(node, entry) 410 } 411 412 func (tree *Tree) insertIntoLeaf(node *Node, entry *Entry) (inserted bool) { 413 insertPosition, found := tree.search(node, entry.Key) 414 if found { 415 node.Entries[insertPosition] = entry 416 return false 417 } 418 // Insert entry's key in the middle of the node 419 node.Entries = append(node.Entries, nil) 420 copy(node.Entries[insertPosition+1:], node.Entries[insertPosition:]) 421 node.Entries[insertPosition] = entry 422 tree.split(node) 423 return true 424 } 425 426 func (tree *Tree) insertIntoInternal(node *Node, entry *Entry) (inserted bool) { 427 insertPosition, found := tree.search(node, entry.Key) 428 if found { 429 node.Entries[insertPosition] = entry 430 return false 431 } 432 return tree.insert(node.Children[insertPosition], entry) 433 } 434 435 func (tree *Tree) split(node *Node) { 436 if !tree.shouldSplit(node) { 437 return 438 } 439 440 if node == tree.Root { 441 tree.splitRoot() 442 return 443 } 444 445 tree.splitNonRoot(node) 446 } 447 448 func (tree *Tree) splitNonRoot(node *Node) { 449 middle := tree.middle() 450 parent := node.Parent 451 452 left := &Node{Entries: append([]*Entry(nil), node.Entries[:middle]...), Parent: parent} 453 right := &Node{Entries: append([]*Entry(nil), node.Entries[middle+1:]...), Parent: parent} 454 455 // Move children from the node to be split into left and right nodes 456 if !tree.isLeaf(node) { 457 left.Children = append([]*Node(nil), node.Children[:middle+1]...) 458 right.Children = append([]*Node(nil), node.Children[middle+1:]...) 459 setParent(left.Children, left) 460 setParent(right.Children, right) 461 } 462 463 insertPosition, _ := tree.search(parent, node.Entries[middle].Key) 464 465 // Insert middle key into parent 466 parent.Entries = append(parent.Entries, nil) 467 copy(parent.Entries[insertPosition+1:], parent.Entries[insertPosition:]) 468 parent.Entries[insertPosition] = node.Entries[middle] 469 470 // Set child left of inserted key in parent to the created left node 471 parent.Children[insertPosition] = left 472 473 // Set child right of inserted key in parent to the created right node 474 parent.Children = append(parent.Children, nil) 475 copy(parent.Children[insertPosition+2:], parent.Children[insertPosition+1:]) 476 parent.Children[insertPosition+1] = right 477 478 tree.split(parent) 479 } 480 481 func (tree *Tree) splitRoot() { 482 middle := tree.middle() 483 484 left := &Node{Entries: append([]*Entry(nil), tree.Root.Entries[:middle]...)} 485 right := &Node{Entries: append([]*Entry(nil), tree.Root.Entries[middle+1:]...)} 486 487 // Move children from the node to be split into left and right nodes 488 if !tree.isLeaf(tree.Root) { 489 left.Children = append([]*Node(nil), tree.Root.Children[:middle+1]...) 490 right.Children = append([]*Node(nil), tree.Root.Children[middle+1:]...) 491 setParent(left.Children, left) 492 setParent(right.Children, right) 493 } 494 495 // Root is a node with one entry and two children (left and right) 496 newRoot := &Node{ 497 Entries: []*Entry{tree.Root.Entries[middle]}, 498 Children: []*Node{left, right}, 499 } 500 501 left.Parent = newRoot 502 right.Parent = newRoot 503 tree.Root = newRoot 504 } 505 506 func setParent(nodes []*Node, parent *Node) { 507 for _, node := range nodes { 508 node.Parent = parent 509 } 510 } 511 512 func (tree *Tree) left(node *Node) *Node { 513 if tree.Empty() { 514 return nil 515 } 516 current := node 517 for { 518 if tree.isLeaf(current) { 519 return current 520 } 521 current = current.Children[0] 522 } 523 } 524 525 func (tree *Tree) right(node *Node) *Node { 526 if tree.Empty() { 527 return nil 528 } 529 current := node 530 for { 531 if tree.isLeaf(current) { 532 return current 533 } 534 current = current.Children[len(current.Children)-1] 535 } 536 } 537 538 // leftSibling returns the node's left sibling and child index (in parent) if it exists, otherwise (nil,-1) 539 // key is any of keys in node (could even be deleted). 540 func (tree *Tree) leftSibling(node *Node, key interface{}) (*Node, int) { 541 if node.Parent != nil { 542 index, _ := tree.search(node.Parent, key) 543 index-- 544 if index >= 0 && index < len(node.Parent.Children) { 545 return node.Parent.Children[index], index 546 } 547 } 548 return nil, -1 549 } 550 551 // rightSibling returns the node's right sibling and child index (in parent) if it exists, otherwise (nil,-1) 552 // key is any of keys in node (could even be deleted). 553 func (tree *Tree) rightSibling(node *Node, key interface{}) (*Node, int) { 554 if node.Parent != nil { 555 index, _ := tree.search(node.Parent, key) 556 index++ 557 if index < len(node.Parent.Children) { 558 return node.Parent.Children[index], index 559 } 560 } 561 return nil, -1 562 } 563 564 // delete deletes an entry in node at entries' index 565 // ref.: https://en.wikipedia.org/wiki/B-tree#Deletion 566 func (tree *Tree) delete(node *Node, index int) { 567 // deleting from a leaf node 568 if tree.isLeaf(node) { 569 deletedKey := node.Entries[index].Key 570 tree.deleteEntry(node, index) 571 tree.rebalance(node, deletedKey) 572 if len(tree.Root.Entries) == 0 { 573 tree.Root = nil 574 } 575 return 576 } 577 578 // deleting from an internal node 579 leftLargestNode := tree.right(node.Children[index]) // largest node in the left sub-tree (assumed to exist) 580 leftLargestEntryIndex := len(leftLargestNode.Entries) - 1 581 node.Entries[index] = leftLargestNode.Entries[leftLargestEntryIndex] 582 deletedKey := leftLargestNode.Entries[leftLargestEntryIndex].Key 583 tree.deleteEntry(leftLargestNode, leftLargestEntryIndex) 584 tree.rebalance(leftLargestNode, deletedKey) 585 } 586 587 // rebalance rebalances the tree after deletion if necessary and returns true, otherwise false. 588 // Note that we first delete the entry and then call rebalance, thus the passed deleted key as reference. 589 func (tree *Tree) rebalance(node *Node, deletedKey interface{}) { 590 // check if rebalancing is needed 591 if node == nil || len(node.Entries) >= tree.minEntries() { 592 return 593 } 594 595 // try to borrow from left sibling 596 leftSibling, leftSiblingIndex := tree.leftSibling(node, deletedKey) 597 if leftSibling != nil && len(leftSibling.Entries) > tree.minEntries() { 598 // rotate right 599 node.Entries = append([]*Entry{node.Parent.Entries[leftSiblingIndex]}, node.Entries...) // prepend parent's separator entry to node's entries 600 node.Parent.Entries[leftSiblingIndex] = leftSibling.Entries[len(leftSibling.Entries)-1] 601 tree.deleteEntry(leftSibling, len(leftSibling.Entries)-1) 602 if !tree.isLeaf(leftSibling) { 603 leftSiblingRightMostChild := leftSibling.Children[len(leftSibling.Children)-1] 604 leftSiblingRightMostChild.Parent = node 605 node.Children = append([]*Node{leftSiblingRightMostChild}, node.Children...) 606 tree.deleteChild(leftSibling, len(leftSibling.Children)-1) 607 } 608 return 609 } 610 611 // try to borrow from right sibling 612 rightSibling, rightSiblingIndex := tree.rightSibling(node, deletedKey) 613 if rightSibling != nil && len(rightSibling.Entries) > tree.minEntries() { 614 // rotate left 615 node.Entries = append(node.Entries, node.Parent.Entries[rightSiblingIndex-1]) // append parent's separator entry to node's entries 616 node.Parent.Entries[rightSiblingIndex-1] = rightSibling.Entries[0] 617 tree.deleteEntry(rightSibling, 0) 618 if !tree.isLeaf(rightSibling) { 619 rightSiblingLeftMostChild := rightSibling.Children[0] 620 rightSiblingLeftMostChild.Parent = node 621 node.Children = append(node.Children, rightSiblingLeftMostChild) 622 tree.deleteChild(rightSibling, 0) 623 } 624 return 625 } 626 627 // merge with siblings 628 if rightSibling != nil { 629 // merge with right sibling 630 node.Entries = append(node.Entries, node.Parent.Entries[rightSiblingIndex-1]) 631 node.Entries = append(node.Entries, rightSibling.Entries...) 632 deletedKey = node.Parent.Entries[rightSiblingIndex-1].Key 633 tree.deleteEntry(node.Parent, rightSiblingIndex-1) 634 tree.appendChildren(node.Parent.Children[rightSiblingIndex], node) 635 tree.deleteChild(node.Parent, rightSiblingIndex) 636 } else if leftSibling != nil { 637 // merge with left sibling 638 entries := append([]*Entry(nil), leftSibling.Entries...) 639 entries = append(entries, node.Parent.Entries[leftSiblingIndex]) 640 node.Entries = append(entries, node.Entries...) 641 deletedKey = node.Parent.Entries[leftSiblingIndex].Key 642 tree.deleteEntry(node.Parent, leftSiblingIndex) 643 tree.prependChildren(node.Parent.Children[leftSiblingIndex], node) 644 tree.deleteChild(node.Parent, leftSiblingIndex) 645 } 646 647 // make the merged node the root if its parent was the root and the root is empty 648 if node.Parent == tree.Root && len(tree.Root.Entries) == 0 { 649 tree.Root = node 650 node.Parent = nil 651 return 652 } 653 654 // parent might underflow, so try to rebalance if necessary 655 tree.rebalance(node.Parent, deletedKey) 656 } 657 658 func (tree *Tree) prependChildren(fromNode *Node, toNode *Node) { 659 children := append([]*Node(nil), fromNode.Children...) 660 toNode.Children = append(children, toNode.Children...) 661 setParent(fromNode.Children, toNode) 662 } 663 664 func (tree *Tree) appendChildren(fromNode *Node, toNode *Node) { 665 toNode.Children = append(toNode.Children, fromNode.Children...) 666 setParent(fromNode.Children, toNode) 667 } 668 669 func (tree *Tree) deleteEntry(node *Node, index int) { 670 copy(node.Entries[index:], node.Entries[index+1:]) 671 node.Entries[len(node.Entries)-1] = nil 672 node.Entries = node.Entries[:len(node.Entries)-1] 673 } 674 675 func (tree *Tree) deleteChild(node *Node, index int) { 676 if index >= len(node.Children) { 677 return 678 } 679 copy(node.Children[index:], node.Children[index+1:]) 680 node.Children[len(node.Children)-1] = nil 681 node.Children = node.Children[:len(node.Children)-1] 682 }