namastey-avl-tree
v1.0.0
Published
A comprehensive JavaScript package for implementing AVL Tree data structure, offering balanced tree operations for efficient data management.
Downloads
2
Readme
namastey-avl-tree
Brief Description
namastey-avl-tree
is a JavaScript package that implements the AVL Tree data structure. AVL Trees are self-balancing binary search trees that ensure the height difference between the left and right subtrees (balance factor) is no more than one, which guarantees O(log n) time complexity for insertions, deletions, and lookups.
Features
- insertValue(value): Inserts a value into the AVL Tree, maintaining the balance of the tree.
- searchValue(value): Searches for a value in the AVL Tree and returns the node if found, or
null
if not found. - inOrder(node): Performs an in-order traversal of the tree, printing the values in ascending order.
- getHeight(node): Returns the height of a given node.
- getBalanceFactor(node): Returns the balance factor of a given node.
- rightRotate(node): Performs a right rotation on the subtree rooted with the given node.
- leftRotate(node): Performs a left rotation on the subtree rooted with the given node.
Installation
To install the namastey-avl-tree
package globally, run:
npm install -g namastey-avl-tree
Examples
Inserting Values
const AVLTree = require('namastey-avl-tree');
const tree = new AVLTree();
tree.insertValue(10);
tree.insertValue(20);
tree.insertValue(5);
tree.insertValue(6);
tree.insertValue(15);
tree.inOrder(tree.root); // Output: 5, 6, 10, 15, 20
Searching for a Value
const result = tree.searchValue(15);
console.log(result ? 'Found' : 'Not Found'); // Output: Found
Tree Balancing Example
tree.insertValue(25);
tree.insertValue(30);
tree.inOrder(tree.root); // Output: 5, 6, 10, 15, 20, 25, 30