It is highly recommended to read the previous articles on Van Emde Boas Tree first.
Procedure for successor:
- Base case: If the size of the tree is 2 then if query-key is 0 and key – 1 is present in the tree then return 1, as it will be the successor. Otherwise return null.
- If the key is less than minimum then we can easily say that minimum will be the successor of the query-key.
- Recursive case:
- We first search for the successor in the cluster in which the key is present.
- If we find any successor in the cluster then generate its index and return it.
- Otherwise, search for the next cluster, with at least one key present, in summary, and return the index the minimum of that cluster.
See query for the successor of 0 the in below image:
Below image represents the successor of 1 query over VEB tree containing key 1 & 2:
Procedure for Predecessor:
- Base case: If the size of the tree is 2 then if query-key is 1 and key-0 is present in the tree then return 0, as it will be the predecessor. Otherwise return null.
- If the key is greater than the maximum then we can easily say that maximum will be the predecessor of the query-key.
- Recursive case:
- We first search for the predecessor in the cluster in which the key is present.
- If we find any predecessor in the cluster then generate its index and return it.
- Otherwise, search for the previous cluster, with at least one key present, in summary. If any cluster is present then return the index of the maximum of that cluster.
- If no cluster with that property is present then see if due to lazy propagation, the minimum of the tree(In which the cluster is present) is less than the key, if yes then return minimum otherwise return null.
Below image represents query predecessor of key-2:
3 4 6
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.