3

Given an AVL Tree that stores a set G of n different natural numbers. In each node stored the number of nodes in the sub-tree rooted at that node.

How can we find the minimal m that is not in G and greater than a given p in time complexity of O(log n)?

Example:

If G={21,22,23,24,26,27,29,30} then: p=20 => m=25 p=22 => m=25 p=25 => m=28 p=29 => m=31

Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
Xtreme Joe
  • 115
  • 6

1 Answers1

2

Imagine that you're solving this problem on an array rather than an AVL tree. How might you go about doing this? First, start by doing a binary search over the array to find the smallest number s greater than p. If that number is greater than p+1, just return p+1. Otherwise, we're in the middle of some run of elements and need to find the smallest number that's past the run. To do so, you can think about solving the following problem: find the smallest element x such that x - s is greater than the index of x minus the index of s. This can be done by doing a binary search on the remaining half of the array: if the difference between the current element and s is equal to the distance between the current element and s, go right, otherwise go left. Once you've found this element x, go to the element before it, add one, and that's your answer.

To solve this problem on the AVL tree, you essentially adapt the above algorithm to use tree searches rather than binary searches. The fact that you have subtree size information means that you can determine the index of any element in the tree using the standard order statistics technique. I imagine the final algorithm will be a bit messy, but it will faithfully simulate the above algorithm in time O(log n).

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • @XtremeJoe it's not. One search to find the first value >= p, then another to find the missing element. – Jordi Vermeulen May 17 '16 at 18:26
  • @XtremeJoe Can you elaborate? This should run in time O(log n). – templatetypedef May 17 '16 at 18:33
  • @templatetypedef in the array example it's O(log n) because you can get the index in O(1). But in the tree, getting the index is O(log n). and the search itself is O(log n)... (unless we update the index while we are searching for the element? and then it is okay, right?) – Xtreme Joe May 17 '16 at 18:37
  • @XtremeJoe Rather than trying to implement the binary search step by using a "true" binary search and mapping indices to nodes, which would take time O((log n)^2), instead just do a walk on the tree to simulate the binary search by either going left or going right from each node. – templatetypedef May 17 '16 at 18:43