Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the minimum gap between two numbers in an AVL tree

I have a data structures homework, that in addition to the regular AVL tree functions, I have to add a function that returns the minimum gap between any two numbers in the AVL tree (the nodes in the AVL actually represent numbers.)

Lets say we have the numbers (as nodes) 1 5 12 20 23 21 in the AVL tree, the function should return the minimum gap between any two numbers. In this situation it should return "1" which is |20-21| or |21-20|.

It should be done in O(1).

Tried to think alot about it, and I know there is a trick but just couldn't find it, I have spent hours on this.

There was another task which is to find the maximum gap, which is easy, it is the difference between the minimal and maximal number.

like image 847
TheNotMe Avatar asked Sep 08 '12 12:09

TheNotMe


People also ask

How do you find the minimum number of nodes in AVL tree?

If height of AVL tree is h, maximum number of nodes can be 2h+1 – 1. Minimum number of nodes in a tree with height h can be represented as: N(h) = N(h-1) + N(h-2) + 1 for n>2 where N(0) = 1 and N(1) = 2.

What is the minimal number of leaves in AVL tree with height of 6?

So, minimum number of nodes required to construct AVL tree of height-6 = 33.

What is the minimum number of nodes in an AVL tree of height 15?

The minimum number of nodes is 12. initially empty AVL tree has keys 1 through 7 inserted in order.

What is the maximum height of a 14 nodes AVL tree?

If there are n nodes in AVL tree, maximum height can't exceed 1.44*log2n.


1 Answers

You need to extend the data structure otherwise you cannot obtain a O(1) search of the minimum gap between the numbers composing the tree.

You have the additional constrain to not increase the time complexity of insert/delete/search function and I assume that you don't want to increase space complexity too.

Let consider a generic node r, with a left subtree r.L and a right subtree r.R; we will extend the information in node r additional number r.x defined as the minimum value between:

  • (only if r.L is not empty) r value and the value of the rightmost leaf on r.L
  • (only if r.L is deeper than 1) the x value of the r.L root node
  • (only if r.R is not empty) r value and the value of the leftmost leaf on r.R
  • (only if r.R is deeper than 1) the x value of the r.R root node

(or undefined if none of the previous condition is valid, in the case of a leaf node)

Additionally, in order to make fast insert/delete we need to add in each internal node the references to its leftmost and rightmost leaf nodes.

You can see that with these additions:

  • the space complexity increase by a constant factor only
  • the insert/delete functions need to update the x values and the leftmost and rightmost leafs of the roots of every altered subtree, but is trivial to implement in a way that need not more than O(log(n))
  • the x value of the tree root is the value that the function needs to return, therefore you can implement it in O(1)

The minimum gap in the tree is the x value of the root node, more specifically, for each subtree the minimum gap in the subtree elements only is the subtree root x value.

The proof of this statement can be made by recursion: Let consider a tree rooted by the node r, with a left subtree r.L and a right subtree r.R. The inductive hypothesis is that the roots of r.L and r.R x values are the values of the minimum gaps between the node values of the subtree. It's obvious that the minimum gap can be found considering only the pairs of nodes with values adjacent in the value sorted list; the pairs formed by values stored by the nodes of r.L have their minimum gap in the r.L root x value, the same is true considering the right subtree. Given that (any value of nodes in r.L) < value of L root node < (any value of nodes in r.R), the only pairs of adjacent values not considered are two:

  1. the pair composed by the root node value and the higher r.L node value
  2. the pair composed by the root node value and the lower r.R node value

The r.L node with the higher value is its rightmost leaf by the AVL tree properties, and the r.R node with the lower value is its leftmost leaf. Assigning to r x value the minimum value between the four values (r.L root x value, r.R root x value, (r - r.L root) gap, (r - r.R root) gap) is the same to assign the smaller gap between consecutive node values in the whole tree, that is equivalent to the smaller gap between any possible pair of node values. The cases where one or two of the subtree is empty are trivial. The base cases of a tree made of only one or three nodes, it is trivial to see that the x value of the tree root is the minimum gap value.

like image 71
pangon Avatar answered Sep 27 '22 17:09

pangon