Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Creating a BinaryTree using Javascript
A Binary Search Tree (BST) is a hierarchical data structure where each node has at most two children. Let's learn how to create and represent a binary search tree in JavaScript by building a complete BST class with essential operations.
Basic Structure
We'll start by creating the BinarySearchTree class and defining a Node class for individual tree elements.
class BinarySearchTree {
constructor() {
// Initialize a root element to null
this.root = null;
}
}
BinarySearchTree.prototype.Node = class {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
// Create a new BST instance
const bst = new BinarySearchTree();
console.log("Empty BST created:", bst.root);
Empty BST created: null
Adding Nodes (Insert Operation)
The insert method adds new nodes while maintaining BST properties: left children are smaller, right children are larger.
BinarySearchTree.prototype.insert = function(data) {
const newNode = new this.Node(data);
if (this.root === null) {
this.root = newNode;
} else {
this.insertNode(this.root, newNode);
}
};
BinarySearchTree.prototype.insertNode = function(node, newNode) {
if (newNode.data < node.data) {
if (node.left === null) {
node.left = newNode;
} else {
this.insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
this.insertNode(node.right, newNode);
}
}
};
// Example usage
const bst = new BinarySearchTree();
bst.insert(50);
bst.insert(30);
bst.insert(70);
bst.insert(20);
bst.insert(40);
console.log("Root data:", bst.root.data);
console.log("Left child:", bst.root.left.data);
console.log("Right child:", bst.root.right.data);
Root data: 50 Left child: 30 Right child: 70
Tree Traversal Methods
Let's implement in-order traversal to display the tree elements in sorted order:
BinarySearchTree.prototype.inOrder = function(node = this.root, result = []) {
if (node !== null) {
this.inOrder(node.left, result);
result.push(node.data);
this.inOrder(node.right, result);
}
return result;
};
// Create and populate BST
const bst = new BinarySearchTree();
[50, 30, 70, 20, 40, 60, 80].forEach(val => bst.insert(val));
console.log("In-order traversal:", bst.inOrder());
console.log("Tree structure (sorted):", bst.inOrder().join(" ? "));
In-order traversal: [ 20, 30, 40, 50, 60, 70, 80 ] Tree structure (sorted): 20 ? 30 ? 40 ? 50 ? 60 ? 70 ? 80
Search Operation
The search method efficiently finds nodes by comparing values and traversing left or right:
BinarySearchTree.prototype.search = function(data, node = this.root) {
if (node === null) {
return false;
}
if (data === node.data) {
return true;
}
if (data < node.data) {
return this.search(data, node.left);
} else {
return this.search(data, node.right);
}
};
// Test search functionality
const bst = new BinarySearchTree();
[50, 30, 70, 20, 40].forEach(val => bst.insert(val));
console.log("Search 30:", bst.search(30));
console.log("Search 25:", bst.search(25));
console.log("Search 70:", bst.search(70));
Search 30: true Search 25: false Search 70: true
Complete Binary Search Tree
Here's a complete example demonstrating our BST with all operations:
class BinarySearchTree {
constructor() {
this.root = null;
}
insert(data) {
const newNode = new this.Node(data);
if (this.root === null) {
this.root = newNode;
} else {
this.insertNode(this.root, newNode);
}
}
insertNode(node, newNode) {
if (newNode.data < node.data) {
node.left === null ? node.left = newNode : this.insertNode(node.left, newNode);
} else {
node.right === null ? node.right = newNode : this.insertNode(node.right, newNode);
}
}
inOrder(node = this.root, result = []) {
if (node !== null) {
this.inOrder(node.left, result);
result.push(node.data);
this.inOrder(node.right, result);
}
return result;
}
}
BinarySearchTree.prototype.Node = class {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
// Demo usage
const myBST = new BinarySearchTree();
[15, 25, 10, 7, 22, 17, 13, 5, 9, 27].forEach(num => myBST.insert(num));
console.log("BST In-Order:", myBST.inOrder());
console.log("Root node:", myBST.root.data);
BST In-Order: [ 5, 7, 9, 10, 13, 15, 17, 22, 25, 27 ] Root node: 15
Conclusion
We've successfully created a Binary Search Tree in JavaScript with insert, search, and traversal operations. This BST maintains sorted order and provides efficient O(log n) average-case performance for basic operations.
