Imagine you have a list of numbers stored in a linked list and the same numbers stored in a Binary Search Tree (BST). Why does searching for a number in the BST usually take less time?
Think about how BST compares values to decide which branch to follow.
A BST keeps values in a sorted structure where each node's left child is smaller and right child is larger. This lets you ignore half the tree at each step, reducing search time to about log(n) steps, unlike a linked list which checks each item one by one.
What is the average number of steps needed to find an element in a balanced Binary Search Tree with n nodes?
Think about how the tree height relates to the number of nodes.
In a balanced BST, the height is about log n, so searching takes O(log n) time on average.
Consider a BST where all nodes are inserted in increasing order, making it unbalanced. What happens to the search efficiency in this case?
Think about the shape of the tree and how many nodes you must check.
If the BST is unbalanced and looks like a linked list, you must check each node one by one, making search time O(n).
Which statement correctly compares searching in a BST and a hash table?
Think about order and average search times for each data structure.
BST keeps data sorted and allows O(log n) search if balanced. Hash tables provide faster average O(1) search but do not keep data sorted.
Explain why the time it takes to search for a value in a BST depends on the height of the tree rather than the total number of nodes.
Consider how you move through the tree when searching.
Searching in a BST involves moving from the root down to a leaf or the target node. Each step moves one level down, so the number of steps equals the height of the tree.