Learn hierarchical tree structures, the Binary Search Tree ordering invariant, and O(log N) lookup and insertion mechanics.
Learn how dynamic arrays achieve O(1) amortized insertion, contiguous memory layout, and geometric doubling strategies.
A Binary Tree is a non-linear data structure where each node has at most two children (left and right).
A Binary Search Tree (BST) enforces a strict ordering rule on every node:
8 (Root)
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function searchBST(root, val) {
if (!root || root.val === val) return root;
// Search left if target is smaller, right if larger
return val < root.val ? searchBST(root.left, val) : searchBST(root.right, val);
}
function insertBST(root, val) {
if (!root) return new TreeNode(val);
if (val < root.val) {
root.left = insertBST(root.left, val);
} else {
root.right = insertBST(root.right, val);
}
return root;
}
isValidBST(node, min, max).Use BSTs for ordered dictionary lookups. Next, let's explore Tree Traversals: Pre-order, In-order, and Post-order!