Consider you want to find a value in a tree structure. Why would a Binary Search Tree (BST) be better than a plain Binary Tree?
Think about how the order of elements affects the speed of finding a value.
A BST keeps nodes in sorted order, so searching can skip half the tree at each step, making it faster than searching a plain Binary Tree where nodes are unordered.
Given the following trees, what will be the output when searching for value 7?
/* BST structure: 5
/ \
3 8
/ \ / \
1 4 7 9
Plain Binary Tree structure:
5
/ \
8 3
/ \ \
9 7 1
Search function returns true if found, false otherwise. */Check if value 7 exists in both trees.
Value 7 is present in both trees, so searching returns true for both.
Consider the search algorithm for a plain Binary Tree and a BST. Why might the plain Binary Tree search take longer?
/* Plain Binary Tree search checks every node because nodes are unordered.
BST search decides to go left or right based on value comparison.
*/Think about how ordering helps skip parts of the tree.
Without order, the plain Binary Tree search must check every node, making it slower than BST search which can skip branches.
Given these trees, what will be the in-order traversal output after inserting value 6?
/* Initial BST:
5
/ \
3 8
/ \ \
1 4 9
Initial Plain Binary Tree:
5
/ \
3 8
/ \
1 9
Insert 6 into both trees.
In-order traversal prints nodes in ascending order for BST.
*/Remember that in-order traversal of BST is sorted, but plain Binary Tree is not guaranteed sorted.
BST insertion keeps order, so in-order traversal is sorted including 6. Plain Binary Tree insertion does not guarantee order, so traversal order differs.
You need a data structure to store numbers that allows fast search and also lets you retrieve numbers in sorted order efficiently. Which is the best choice?
Think about which structure supports both fast search and sorted order retrieval.
Binary Search Tree supports fast search by ordering nodes and allows in-order traversal to retrieve sorted data. Plain Binary Tree and linked list do not guarantee order or fast search. Hash tables do not maintain order.
