0

Let A = {a1, a2, . . . , an} be a set of distinct items totally ordered such that ai< aj iff i < j. Assume that over a long sequence of m accesses to items in A, ai is accessed q(i)≥1 times. I want an algorithm to find a binary search tree T where each item ai resides in a leaf of T such that if we serve the accesses using T, each time going from the root to the corresponding leaf, the total time it takes to serve the whole sequence is minimized. the algorithm indeed must constructs a tree that minimizes the total access time.

I think that max-heap of frequencies is one option to use but I'm not sure that it is the best.

my solution to the problem is this: first sort the array A descending then create a binary search tree from the sorted array (A[0] as the root , left[i]=2*i , right[i]=2*(i+1)).

is my solution optimum?

abdolahS
  • 663
  • 12
  • 35

1 Answers1

0

Ad hoc I would say that this problem is equivalent to constructing a Huffman tree.

UPDATE

No, it is not, because the Huffman tree does not take into account and preserve the ordering. But it seems at least quite related.

Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143