namastey-b-tree
v1.0.0
Published
A JavaScript package for implementing the B-Tree data structure, providing efficient data storage and retrieval.
Downloads
4
Maintainers
Readme
namastey-b-tree
A JavaScript package implementing the B-Tree data structure.
Description
namastey-b-tree
is a simple and efficient implementation of the B-Tree data structure, supporting various key operations. B-Trees are self-balancing trees that maintain sorted data and allow searches, sequential access, insertions, and deletions in logarithmic time.
Features
- traverse(): Traverse the B-Tree and print all keys in sorted order.
- search(key): Search for a key in the B-Tree. Returns the node containing the key or
null
if not found. - insert(key): Insert a new key into the B-Tree. Handles splitting nodes if necessary.
Installation
To install the package globally, use the following command:
npm install -g namastey-b-tree
Examples
const BTree = require('namastey-b-tree');
const tree = new BTree(3); // Creating a B-Tree with a minimum degree of 3
tree.insert(10);
tree.insert(20);
tree.insert(5);
tree.insert(6);
tree.insert(12);
tree.insert(30);
tree.insert(7);
tree.insert(17);
console.log('Traversal of the constructed B-Tree:');
tree.traverse();
// Output: 5 6 7 10 12 17 20 30
console.log('Search result for key 6:', tree.search(6));
// Output: Node containing key 6
console.log('Search result for key 15:', tree.search(15));
// Output: null