Concept Flow - Why BST Over Plain Binary Tree
Start with Tree
Is Tree Binary?
Yes
Is Tree Sorted?
Plain Binary
Search: O(n)
Slower
Shows decision from any tree to binary tree, then to BST, highlighting search speed difference.
class Node { public int val; public Node left, right; } // BST insert and search are faster than plain binary tree search
| Step | Operation | Nodes in Tree | Pointer Changes | Visual State |
|---|---|---|---|---|
| 1 | Create root node with value 10 | 10 | root -> Node(10) | 10 / . . |
| 2 | Insert 5 in plain binary tree (no order) | 10, 5 | root.left -> Node(5) | 10 / 5 . |
| 3 | Insert 15 in plain binary tree (no order) | 10, 5, 15 | root.right -> Node(15) | 10 / \ 5 15 |
| 4 | Search for 15 in plain binary tree (no order) | 10, 5, 15 | Traverse nodes in any order | Search may check 10 -> 5 -> 15 |
| 5 | Create BST root node with value 10 | 10 | root -> Node(10) | 10 / . . |
| 6 | Insert 5 in BST (5 < 10, go left) | 10, 5 | root.left -> Node(5) | 10 / 5 . |
| 7 | Insert 15 in BST (15 > 10, go right) | 10, 5, 15 | root.right -> Node(15) | 10 / \ 5 15 |
| 8 | Search for 15 in BST (compare 10 < 15, go right) | 10, 5, 15 | Traverse 10 -> 15 | Search path: 10 -> 15 |
| 9 | Search for 5 in BST (compare 10 > 5, go left) | 10, 5, 15 | Traverse 10 -> 5 | Search path: 10 -> 5 |
| 10 | End of demonstration | 10, 5, 15 | No changes | BST search faster due to ordered structure |
| Variable | Start | After Step 2 | After Step 3 | After Step 5 | After Step 6 | After Step 7 | After Step 8 |
|---|---|---|---|---|---|---|---|
| PlainTreeRoot | null | Node(10) | Node(10) | Node(10) | Node(10) | Node(10) | Node(10) |
| PlainTreeLeft | null | Node(5) | Node(5) | Node(5) | Node(5) | Node(5) | Node(5) |
| PlainTreeRight | null | null | Node(15) | Node(15) | Node(15) | Node(15) | Node(15) |
| BSTRoot | null | null | null | Node(10) | Node(10) | Node(10) | Node(10) |
| BSTLeft | null | null | null | null | Node(5) | Node(5) | Node(5) |
| BSTRight | null | null | null | null | null | Node(15) | Node(15) |
Why BST Over Plain Binary Tree: - Plain binary tree has no order, search is O(n) - BST keeps nodes ordered: left < parent < right - BST search uses comparisons, faster O(log n) average - Insertions in BST maintain order - BST is preferred for efficient search and insert
class Node {
public int data; public Node left, right;
public Node(int val) { data = val; left = right = null; }
}
class BST {
public Node root;
public void Insert(int val) {
root = InsertRec(root, val);
}
private Node InsertRec(Node root, int val) {
if (root == null) return new Node(val);
if (val < root.data) root.left = InsertRec(root.left, val);
else root.right = InsertRec(root.right, val);
return root;
}
public void Inorder(Node root) {
if (root != null) {
Inorder(root.left);
Console.Write(root.data + " ");
Inorder(root.right);
}
}
}
var tree = new BST();
tree.Insert(10);
tree.Insert(5);
tree.Insert(15);
tree.Inorder(tree.root);class Node {
public int data; public Node left, right;
public Node(int val) { data = val; }
}
class BST {
public Node root;
public void Insert(int val) {
root = InsertRec(root, val);
}
private Node InsertRec(Node root, int val) {
if (root == null) return new Node(val);
if (val < root.data) root.left = InsertRec(root.left, val);
else root.right = InsertRec(root.right, val);
return root;
}
}Pass the test and this pattern moves unknown