3

I'm trying to create some rank tree based on AVL tree with special requirements, lets assume that I have AVL tree with nodes, each node has 2 fields:

id, priority

my AVL tree is sorted by id, also I have a function:

foo(currentId, delta)

which lowers the priority of the all ids which is less or equal to currentId by delta this function has to work with complexity O(log n), so my question is which additional information do I need to store to be able to pop the node with the highest priority at any stage with complexity O(1) (after using foo also!).

P.S. I tried to store info about max priority in the right subtree, max priority in the left subtree, but it needs a lot of changes after foo. It will take more than just O(log n). Thanks in advance for any good ideas or good books about Trees.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
rookie
  • 7,723
  • 15
  • 49
  • 59
  • Any reason for using AVL trees? It seems like various priority queue implementations might be nicer here. – templatetypedef Jan 07 '11 at 00:17
  • @templatetypedef: can You give me please link with useful info? don't know what are You talking about... – rookie Jan 07 '11 at 00:18
  • Are you calling `foo()` after every pop? What values of `delta` are you using relative to the `id`'s? A walked-through example of what you're trying to achieve will help us understand what you're trying to achieve here. – moinudin Jan 07 '11 at 00:32
  • @marcog: no randomly, about id is always > 0, there is no other info... – rookie Jan 07 '11 at 00:39
  • If everything is completely random and you have nothing else to go on, your best average case is brute forcing the highest priority search. If you have more info, let us know in a worked example and we can help you further. – moinudin Jan 07 '11 at 00:43

1 Answers1

1

Rather than store the maximum priority that appears in each subtree, for each node c with parent p, store in c the difference between the maximum priority in c's subtree and the maximum priority in p's subtree. This way, you can update a range of priorities in O(log n) time, and still find the element of maximum priority in O(log n) time.

jonderry
  • 23,013
  • 32
  • 104
  • 171
  • Why is it so important to have O(1) instead of O(log n), since you're already paying that much during the other operations on the tree? If you want, you can probably amortize the cost of removing the high-priority nodes down to O(1) if you pay double for the insertion events and calls to foo. This is just accounting though. – jonderry Jan 07 '11 at 08:19