Skip to content

Commit ed4a4e9

Browse files
committed
Removed depth passed to rebalanceLeaf
1 parent d2ad380 commit ed4a4e9

8 files changed

Lines changed: 42 additions & 41 deletions

File tree

docs/classes/BTree.html

Lines changed: 22 additions & 22 deletions
Large diffs are not rendered by default.

docs/classes/KeyBound.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

docs/classes/KeyRange.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

docs/classes/Path.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

docs/classes/PathBranch.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

docs/variables/NodeCapacity.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>NodeCapacity | digitree</title><meta name="description" content="Documentation for digitree"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">digitree</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">NodeCapacity</a></li></ul><h1>Variable NodeCapacity<code class="tsd-tag">Const</code></h1></div><div class="tsd-signature"><span class="tsd-kind-variable">NodeCapacity</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">64</span></div><div class="tsd-comment tsd-typography"><p>Node capacity. Not configurable - not worth the runtime memory, when almost nobody will touch this</p>
2-
</div><div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Digithought/Digitree/blob/421aecf8943ed037e589899feacbea50a0973a41/src/b-tree.ts#L6">b-tree.ts:6</a></li></ul></aside></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">digitree</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
2+
</div><div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Digithought/Digitree/blob/d2ad38027c664f3d05c800f699d918bb64adfd22/src/b-tree.ts#L6">b-tree.ts:6</a></li></ul></aside></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">digitree</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "digitree",
3-
"version": "1.4.2",
3+
"version": "1.4.3",
44
"description": "Lightweight B+Tree implementation in TypeScript",
55
"type": "module",
66
"main": "./dist/index.js",

src/b-tree.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ export class BTree<TKey, TEntry> {
436436
const pathBranch = path.branches.at(-1)!;
437437
this.updatePartition(pathBranch.index, path, path.branches.length - 1, this.keyFromEntry(path.leafNode.entries[path.leafIndex]));
438438
}
439-
const newRoot = this.rebalanceLeaf(path, path.branches.length);
439+
const newRoot = this.rebalanceLeaf(path);
440440
if (newRoot) {
441441
this._root = newRoot;
442442
}
@@ -584,29 +584,30 @@ export class BTree<TKey, TEntry> {
584584
return new Split<TKey>(newPartition, newBranch, delta);
585585
}
586586

587-
private rebalanceLeaf(path: Path<TKey, TEntry>, depth: number): ITreeNode | undefined {
588-
if (depth === 0 || path.leafNode.entries.length >= (NodeCapacity >>> 1)) {
587+
private rebalanceLeaf(path: Path<TKey, TEntry>): ITreeNode | undefined {
588+
if (path.leafNode.entries.length >= (NodeCapacity >>> 1)) {
589589
return undefined;
590590
}
591591

592592
const leaf = path.leafNode;
593-
const parent = path.branches.at(depth - 1)!;
593+
const parent = path.branches.at(-1)!;
594+
const depth = path.branches.length - 1;
594595
const pIndex = parent.index;
595596
const pNode = parent.node;
596597

597598
const rightSib = pNode.nodes[pIndex + 1] as LeafNode<TEntry> | undefined;
598599
if (rightSib && rightSib.entries.length > (NodeCapacity >>> 1)) { // Attempt to borrow from right sibling
599600
const entry = rightSib.entries.shift()!;
600601
leaf.entries.push(entry);
601-
this.updatePartition(pIndex + 1, path, depth - 1, this.keyFromEntry(rightSib.entries[0]!));
602+
this.updatePartition(pIndex + 1, path, depth, this.keyFromEntry(rightSib.entries[0]!));
602603
return undefined;
603604
}
604605

605606
const leftSib = pNode.nodes[pIndex - 1] as LeafNode<TEntry> | undefined;
606607
if (leftSib && leftSib.entries.length > (NodeCapacity >>> 1)) { // Attempt to borrow from left sibling
607608
const entry = leftSib.entries.pop()!;
608609
leaf.entries.unshift(entry);
609-
this.updatePartition(pIndex, path, depth - 1, this.keyFromEntry(entry));
610+
this.updatePartition(pIndex, path, depth, this.keyFromEntry(entry));
610611
path.leafIndex += 1;
611612
return undefined;
612613
}
@@ -616,9 +617,9 @@ export class BTree<TKey, TEntry> {
616617
pNode.partitions.splice(pIndex, 1);
617618
pNode.nodes.splice(pIndex + 1, 1);
618619
if (pIndex === 0) { // 0th node of parent, update parent key
619-
this.updatePartition(pIndex, path, depth - 1, this.keyFromEntry(leaf.entries[0]!));
620+
this.updatePartition(pIndex, path, depth, this.keyFromEntry(leaf.entries[0]!));
620621
}
621-
return this.rebalanceBranch(path, depth - 1);
622+
return this.rebalanceBranch(path, depth);
622623
}
623624

624625
if (leftSib && leftSib.entries.length + leaf.entries.length <= NodeCapacity) { // Attempt to merge into left sibling (leaf deleted)
@@ -627,7 +628,7 @@ export class BTree<TKey, TEntry> {
627628
leftSib.entries.push(...leaf.entries);
628629
pNode.partitions.splice(pIndex - 1, 1);
629630
pNode.nodes.splice(pIndex, 1);
630-
return this.rebalanceBranch(path, depth - 1);
631+
return this.rebalanceBranch(path, depth);
631632
}
632633
}
633634

0 commit comments

Comments
 (0)