namastey-binary-tree is a JavaScript package that implements the Binary Tree data structure along with various important methods such as insertion, traversal (in-order, pre-order, post-order), finding minimum and maximum values, and searching for a value.
- insert(value): Inserts a new node into the Binary Tree.
- inOrder(): Performs an in-order traversal of the Binary Tree and returns the result as an array.
- preOrder(): Performs a pre-order traversal of the Binary Tree and returns the result as an array.
- postOrder(): Performs a post-order traversal of the Binary Tree and returns the result as an array.
- findMin(): Finds and returns the minimum value in the Binary Tree.
- findMax(): Finds and returns the maximum value in the Binary Tree.
-
search(value): Searches for a value in the Binary Tree and returns
true
if found, otherwisefalse
.
To install the package globally, use the following command:
npm install -g namastey-binary-tree
const BinaryTree = require('namastey-binary-tree');
const tree = new BinaryTree();
// Insert nodes into the tree
tree.insert(50);
tree.insert(30);
tree.insert(70);
tree.insert(20);
tree.insert(40);
tree.insert(60);
tree.insert(80);
// Perform in-order traversal
console.log('In-order Traversal:', tree.inOrder()); // Output: [20, 30, 40, 50, 60, 70, 80]
// Find minimum and maximum values
console.log('Minimum Value:', tree.findMin()); // Output: 20
console.log('Maximum Value:', tree.findMax()); // Output: 80
// Search for a value in the tree
console.log('Search for 60:', tree.search(60)); // Output: true
console.log('Search for 25:', tree.search(25)); // Output: false