The runtime for BST search, insert, and delete depends on where the value is in the tree. We only have a reference to the root node to start, so reaching values that are farther down in the tree will require more comparisons. For example, with search, the best case is when the value we’re looking for is in the root node - we’ll find it instantly, in O(1) time. The worst case is when the value is in the lowest leaf node (or not in the BST at all, which will require us to search down to the lowest leaf node to be sure). How far down that leaf node is depends on the height of the tree. And the height of the tree depends on the order in which we inserted values to construct the tree. Say we wanted to insert the numbers 1 - 8 into an empty BST. Give an insertion order that results in a valid BST with the maximum possible height and draw the resulting tree and then with the same number range, give an insertion order that results in a valid BST with the minimum possible height and draw the resulting tree.
The runtime for BST search, insert, and delete depends on where the value is in the tree. We only have
a reference to the root node to start, so reaching values that are farther down in the tree will require
more comparisons.
For example, with search, the best case is when the value we’re looking for is in the root node - we’ll
find it instantly, in O(1) time. The worst case is when the value is in the lowest leaf node (or not in the
BST at all, which will require us to search down to the lowest leaf node to be sure).
How far down that leaf node is depends on the height of the tree. And the height of the tree depends
on the order in which we inserted values to construct the tree.
Say we wanted to insert the numbers 1 - 8 into an empty BST. Give an insertion order
that results in a valid BST with the maximum possible height and draw the resulting tree and then with the same number range, give an insertion order that results in a valid BST with
the minimum possible height and draw the resulting tree.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images