Can you guys help me with the homework question I'm stuck in?
A local minimum in a complete binary tree is defined as a node that is smaller than all its neighbors (neighbors = parent, left child, right child). I need to find a local minimum in a given complete binary tree, each of its nodes which has a different number, in O (logn) time of complication.
Well, since the requirement is O (logn), I tried to think about how to go through one path through a tree to a leaf. Or maybe I could only look at half the tree each time in recursion, and that way it would do logn.
So to speak, I have it in a tree:
70 / \ 77 60
There are 3 cases:
1) the root is smaller than the left and right child //, then I finished
2) the root is less than the left
3) the root is less than the right
The above tree is case 2. Therefore, let's βthrow awayβ the left subtree, because there is no way that 77 could be a βlocal minimumβ, since it is larger than its parent. So, we have the right subtree left. And so on, until we find a local minimum.
The problem is that when we drop this left subtree, we can skip the next local minimum below. Here is an example:
70 / \ 77 60 / \ / \ 1 8 9 14 / \ / \ / \ / \ 3 4 5 6 2 7 15 13
So, in this case, the only local minimum is β1β, but we skipped it, because at first we decided to search for the correct subtree of the root.