bst-ds
v1.0.1
Published
Binary Search Tree data structure for JavaScript
Downloads
206
Maintainers
Readme
bst-ds
Binary Search Tree (BST) data structure for JavaScript developers.
Install
npm install bst-dsMethods
| Method | Description | Example |
|---|---|---|
| insert(value) | Insert a value | tree.insert(10) |
| search(value) | Returns true/false | tree.search(10) |
| delete(value) | Delete a value | tree.delete(10) |
| inorder() | Left→Root→Right | tree.inorder() |
| preorder() | Root→Left→Right | tree.preorder() |
| postorder() | Left→Right→Root | tree.postorder() |
| min() | Smallest value | tree.min() |
| max() | Largest value | tree.max() |
| height() | Height of tree | tree.height() |
Usage
const { BST } = require('bst-ds');Example
const tree = new BST();
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(3);
console.log(tree.inorder()); // [3, 5, 10, 15]
console.log(tree.search(5)); // true
console.log(tree.height()); // 2
console.log(tree.min()); // 3
console.log(tree.max()); // 15
tree.delete(5);
console.log(tree.inorder()); // [3, 10, 15]License
MIT