1

I was writing a custom heap implementation for Udacity course and needed a heap that would return a metric value for an element through both heap index and key of an element.

I have ended up with a list for a heap that contains tuples (Metric, Key).

But to get the right element via key I had to create a separate map and maintain it's validity for each change in the heap.

So in the end instead of having functions with two parameters like heapup(heap, i), I had to pass map to all the functions - heapup(heap, i, map).

I am wondering if there is a simpler way to do this through procedures, lists and dictionaries. Or Heap object will be required to hide the Map inside?

 def heapup(L, i, map):

    if i == 0: return i # we reached the top!
    if i >= len(L): return i
    if L[parent(i)] > L[i]:
       # print "going up"
        (L[i], L[parent(i)]) = (L[parent(i)], L[i])
        map[L[i][1]] = i
        map[L[parent(i)][1]] = parent(i)
        return up_heapify(L, parent(i), map)
    else:
     #   print "going down"
        if leaf(L,i): return i
        if one_child(L,i): return i # we could only come this way
        if L[i] > L[left(i)]: # compare with left child
            (L[i], L[left(i)]) = (L[left(i)], L[i])
            map[L[i][1]] = i
            map[L[left(i)][1]] = left(i)
            return left(i)
        if L[i] > L[right(i)]: # compare with right child
            (L[i], L[right(i)]) = (L[right(i)], L[i])
            map[L[i][1]] = i
            map[L[right(i)][1]] = right(i)
            return right(i)

I would like to get rid of map in that function but still be able to get items values from heap by their key which I now can do like this:

item = heap[map[key]]

For example:

if

L = [(3,'A'), (5, 'D'), (4, 'G') ...] 

then

map = {'A':0, 'D':1, 'G': 2, ...} 

so I can get a value of an element like this:

L[map['G']] 

which will give me 4

9
  • 1
    I'm afraid I really don't understand what you're doing. What is map exactly? What does "Metric" mean in this context? Can you show the code you've created so far? Commented Jan 16, 2013 at 11:52
  • I'm not sure what you want, but look at OrderedDict Commented Jan 16, 2013 at 11:59
  • please elaborate the question Commented Jan 16, 2013 at 12:07
  • basically L consists of tuples (x,y) where x - is a metric, and y is a key of the element. As you can see I have to update map every time I change position of elements in the heap. that will allow me to get element from the heap by Key like this: heap[map[key]] Commented Jan 16, 2013 at 12:13
  • @Blckknght metric is some numeric value that is used to sort elements in the heap Commented Jan 16, 2013 at 12:14

1 Answer 1

3

Use heapq http://docs.python.org/2/library/heapq.html with description:

"This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm."

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.