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
Inserting a key into a tree in Javascript
In a Binary Search Tree (BST), inserting a new key follows a simple rule: smaller values go to the left subtree, and larger values go to the right subtree. The first insertion creates the root node, while subsequent insertions traverse the tree to find the correct position.
How BST Insertion Works
The insertion process starts at the root and compares the new value with each node. If the value is smaller, we move left; if larger, we move right. When we reach a null position (leaf node), we insert the new node there.
Iterative Insertion Method
The iterative approach uses a while loop to traverse the tree until finding the correct insertion point:
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
insertIter(data) {
let node = new Node(data);
// Check if the tree is empty
if (this.root === null) {
// Insert as the first element
this.root = node;
return;
}
let currNode = this.root;
while (true) {
if (data < currNode.data) {
// Set the value here as we've reached a leaf node
if (currNode.left === null) {
currNode.left = node;
break;
} else {
currNode = currNode.left;
}
} else {
// Set the value here as we've reached a leaf node
if (currNode.right === null) {
currNode.right = node;
break;
} else {
currNode = currNode.right;
}
}
}
}
// Helper method to display tree structure
inOrder(node = this.root) {
if (node !== null) {
this.inOrder(node.left);
console.log(node.data);
this.inOrder(node.right);
}
}
}
// Test the iterative insertion
let BST = new BinarySearchTree();
BST.insertIter(10);
BST.insertIter(15);
BST.insertIter(5);
BST.insertIter(3);
BST.insertIter(7);
console.log("Tree structure (in-order):");
BST.inOrder();
Tree structure (in-order): 3 5 7 10 15
Recursive Insertion Method
The recursive approach leverages the tree's natural recursive structure. It uses a helper function to handle the recursion:
// Helper function outside the class
function insertRecHelper(root, node) {
if (node.data < root.data) {
// Set the value here as we've reached a leaf node
if (root.left === null) {
root.left = node;
} else {
// Recursively call this function with the left subtree
insertRecHelper(root.left, node);
}
} else {
// Set the value here as we've reached a leaf node
if (root.right === null) {
root.right = node;
} else {
// Recursively call this function with the right subtree
insertRecHelper(root.right, node);
}
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
insertRec(data) {
let node = new Node(data);
// Check if the tree is empty
if (this.root === null) {
// Insert as the first element
this.root = node;
} else {
insertRecHelper(this.root, node);
}
}
inOrder(node = this.root) {
if (node !== null) {
this.inOrder(node.left);
console.log(node.data);
this.inOrder(node.right);
}
}
}
// Test the recursive insertion
let BST2 = new BinarySearchTree();
BST2.insertRec(10);
BST2.insertRec(15);
BST2.insertRec(5);
BST2.insertRec(50);
BST2.insertRec(3);
BST2.insertRec(7);
BST2.insertRec(12);
console.log("Recursive tree structure (in-order):");
BST2.inOrder();
Recursive tree structure (in-order): 3 5 7 10 12 15 50
Comparison of Methods
| Method | Space Complexity | Readability | Performance |
|---|---|---|---|
| Iterative | O(1) | Good | Slightly faster |
| Recursive | O(h) - height dependent | Excellent | Function call overhead |
Key Points
- Both methods maintain the BST property: left
- Time complexity is O(h) where h is the tree height
- The iterative method uses constant space, while recursive uses call stack space
- Recursive code is more intuitive but has function call overhead
Conclusion
BST insertion can be implemented both iteratively and recursively. The iterative approach is more memory-efficient, while the recursive method is more readable and follows the tree's natural structure. Both maintain O(log n) average time complexity for balanced trees.
