Please refer all previous articles on Proto Van Emde Boas Tree first.
Successor Query Procedure:
- Base case: For Proto-VEB of size 2 the only possibility is that key is 0 and if the next key is present then it is its successor or there is no successor. So the same procedure is applied.
- First, we will look in the present cluster (means the cluster in which the query key is present) if there is any key greater than query key is present then we will be the successor so we return it.
- If above is not the case then we will recursively call successor procedure over summary to find next true value in summary. If there is no next true value in summary then we will return -1 as a sign that no larger key is present.
- In the above operation if we find any next true value then we will find the minimum key present in that cluster which will be the successor of the query key.
See the image below for basic understanding of the operation of Successor query:
Procedure for Predecessor is same as successor with some minor changes you should try to understand it from the above description for successor query. See the image below for basic understanding:
Below is the implementation:
Recurrence Relation for Successor and Predecessor Queries:
T(u) = T(u) = 2T()) + O(log2())
Time Complexity : O(log2(u)*log2(log2(u)))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.