Consider you want to find a number in a tree structure. Why would using a Binary Search Tree (BST) be better than using a plain Binary Tree?
Think about how sorting helps in quickly finding an item.
A Binary Search Tree keeps its elements in sorted order. This means when searching, you can decide to go left or right child based on comparison, effectively skipping half the nodes each time. A plain Binary Tree has no order, so you might have to check every node.
Given the following trees, what will be the output when searching for value 7?
Plain Binary Tree (no order): 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 (linked left to right)
BST: Root 4, left child 2, right child 6, left child of 2 is 1, right child of 2 is 3, left child of 6 is 5, right child of 6 is 7
Assume search prints "Found" if value exists, else "Not Found".
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } // Plain Binary Tree search (traverses all nodes) bool SearchPlain(Node root, int target) { if (root == null) return false; if (root.val == target) return true; return SearchPlain(root.left, target) || SearchPlain(root.right, target); } // BST search (uses ordering) bool SearchBST(Node root, int target) { if (root == null) return false; if (root.val == target) return true; if (target < root.val) return SearchBST(root.left, target); else return SearchBST(root.right, target); } // Output?
Both trees contain the value 7, but search methods differ.
Both trees contain the value 7. The plain binary tree search checks all nodes and finds 7. The BST search uses ordering and finds 7 by going right from 4, then right from 6.
Given the following code snippets for searching a value in a plain Binary Tree and a BST, why does the plain Binary Tree search take longer time?
bool SearchPlain(Node root, int target) { if (root == null) return false; if (root.val == target) return true; return SearchPlain(root.left, target) || SearchPlain(root.right, target); } bool SearchBST(Node root, int target) { if (root == null) return false; if (root.val == target) return true; if (target < root.val) return SearchBST(root.left, target); else return SearchBST(root.right, target); }
Think about how ordering helps reduce search steps.
The plain Binary Tree search must check every node because there is no order to guide it. The BST search uses the property that left child is smaller and right child is larger, so it can skip half the nodes each step, making it faster.
You have a large dataset of unique numbers and need to perform many search queries quickly. Which data structure is better and why?
Think about how sorting affects search speed.
A Binary Search Tree keeps data sorted, allowing fast search by comparing values and moving left or right. Plain Binary Trees and linked lists require checking many elements, and unsorted arrays require scanning all elements for search.
Consider inserting the values [5, 3, 7, 2, 4, 6, 8] into an empty tree. What is the printed in-order traversal (left-root-right) of the tree after insertion for each type?
// Plain Binary Tree insertion: insert as left child if empty, else right child // BST insertion: insert maintaining BST property // In-order traversal prints nodes in sorted order for BST
Remember in-order traversal of BST prints sorted values.
In a plain Binary Tree, insertion order determines structure, so in-order traversal prints nodes in insertion order. In a BST, in-order traversal always prints nodes sorted from smallest to largest.
