This project provides an implementation of a balanced Binary Search Tree (BST) in JavaScript. The code includes a Tree
class to handle the BST and a Node
class to define each node within the tree.
This BST implementation allows for efficient insertion, deletion, searching, and traversal of elements. Additionally, the tree can rebalance itself to maintain optimal performance.
- Node insertion and deletion
- Balancing of the tree
- Tree traversal (in-order, pre-order, post-order, level-order)
- Height, depth, and balance checking
- Automatic rebalancing
The Node
class represents each node in the BST.
class Node {
constructor(value = null, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}
}
The Node
class represents each node in the BST.
cclass Tree {
constructor(array) {
this.root = this.buildTree(array);
}
// Additional methods...
}
- Clone the repository:
git clone https://github.com/Jaizzer/balanced-binary-search-tree.git
- Include the Node and Tree classes in your JavaScript project.
- Initialize a new Tree instance with an array of values:
let tree = new Tree([1, 2, 3, 4, 5, 6, 7]);
tree.insert(8); // Inserts 8 into the tree
tree.delete(3); // Deletes the node with value 3 from the tree
tree.isBalanced(); // Returns true if the tree is balanced, false otherwise
tree.rebalance(); // Rebalances the tree if it is unbalanced
buildTree(array) // Builds a balanced BST from a sorted array.
insert(data) // Inserts a new node into the BST.
delete(data) // Deletes a node from the BST.
find(data) // Finds a node with the specified value.
isBalanced() // Checks if the tree is balanced.
rebalance() // Rebalances the tree if it is unbalanced.
inOrder(callback) // In-order traversal of the BST.
preOrder(callback) // Pre-order traversal of the BST.
postOrder(callback) // Post-order traversal of the BST.
levelOrder(callback) // Level-order traversal of the BST.