A Binary Search Tree (BST) is a hierarchical node structure where each parent node has at most two children. The left subtree contains values smaller than the parent, and the right subtree contains values larger, allowing efficient binary search scans.
Compare target 30 with root key 50. Since 30 < 50, branch to the left subtree.
Target: 30 < Root: 50 -> Move to Left ChildIf left child pointer is null, allocate new node(30) as left child of 50.
Root 50 -> Left Child: Node(30)Compare target 70 with root key 50. Since 70 > 50, branch to the right subtree.
Target: 70 > Root: 50 -> Right Child: Node(70)Imagine walking into a library. To find book 50, you look at section 40. Since 50 > 40, you ignore everything to the left and search to the right.
~9 steps (Logarithmic)
O(log N) Tree Height Time
O(N) Tree Nodes Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Search | O(log N) | O(1) |
| Insertion | O(log N) | O(1) |
| Skewed Worst Case | O(N) | O(N) |
class Node {
constructor(val) {
this.val = val;
this.left = null; this.right = null;
}
}
// Insert recursive check
function insert(root, val) {
if(!root) return new Node(val);
if(val < root.val) root.left = insert(root.left, val);
else root.right = insert(root.right, val);
return root;
}Maintains dynamically updated sorted maps inside compiler memory blocks.
Models key database indices (B-Trees are derived extensions).
Structures hierarchical folder directory paths.
Validate your conceptual understanding, operation mechanics, and core concepts of Binary Tree.
If a BST becomes skewed (looks like a line), search time degrades to O(N). Use self-balancing variants (like AVL or Red-Black trees) to keep it O(log N)!
BST initialized. Insert values to watch nodes traverse to their correct children slots.