Sorry, I may not be able to help you with the code.<br><br>But I can point you to a very simple priority queue solution using gb_tree proposed by Ulf Wiger:<br><a href="http://www.erlang.org/pipermail/erlang-questions/2007-June/027585.html" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

http://www.erlang.org/pipermail/erlang-questions/2007-June/027585.html</a><br><br>I would be interested to know about has any other solutions.<br><br><div><span class="gmail_quote">On 8/30/07, <b class="gmail_sendername">
Jeffrey Chen</b> <<a href="mailto:bitcowboy@gmail.com">bitcowboy@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'm trying to implement a priority queue using heap struct. But I<br>can't do that efficient. Can anybody help me? I'll post my code below.<br>Thanks.<br><br><br>CODE:<br>exchange([], _, _, _, _, _) -><br>    [];
<br>exchange([_ | Rest], IndexA, ValueA, IndexB, ValueB, Index) when<br>IndexA == Index -><br>    [ValueB | exchange(Rest, IndexA, ValueA, IndexB, ValueB, Index + 1)];<br>exchange([_ | Rest], IndexA, ValueA, IndexB, ValueB, Index) when
<br>IndexB == Index -><br>    [ValueA | exchange(Rest, IndexA, ValueA, IndexB, ValueB, Index + 1)];<br>exchange([Head | Rest], IndexA, ValueA, IndexB, ValueB, Index) -><br>    [Head | exchange(Rest, IndexA, ValueA, IndexB, ValueB, Index + 1)].
<br><br><br>%%% Heap %%%<br><br>max_heapify(Heap, Index) when Index * 2 > length(Heap) -><br>    Heap;<br>max_heapify(Heap, Index) -><br>    IndexValue = lists:nth(Index, Heap),<br><br>    LeftIndex  = 2 * Index,
<br>    LeftValue  = lists:nth(LeftIndex, Heap),<br><br>    if<br>        2 * Index + 1 > length(Heap) -><br>            RightIndex = LeftIndex,<br>            RightValue = LeftValue;<br>        true -><br>            RightIndex = 2 * Index + 1,
<br>            RightValue = lists:nth(RightIndex, Heap)<br>    end,<br><br>    if<br>        LeftValue > IndexValue, LeftValue >= RightValue -><br>            LargestIndex = LeftIndex,<br>            LargestValue = LeftValue;
<br>        RightValue > IndexValue, RightValue >= LeftValue -><br>            LargestIndex = RightIndex,<br>            LargestValue = RightValue;<br>        true -><br>            LargestIndex = Index,<br>            LargestValue = IndexValue
<br>    end,<br><br>    if<br>        LargestIndex /= Index -><br>            NewHeap = exchange(Heap, Index, IndexValue, LargestIndex,<br>LargestValue, 1),<br>            max_heapify(NewHeap, LargestIndex);<br>        true ->
<br>            Heap<br>    end.<br><br>build_heap(L, 0) -><br>    L;<br>build_heap(L, Index) -><br>    NewL = max_heapify(L, Index),<br>    build_heap(NewL, Index - 1).<br><br>make_heap([]) -><br>    [];<br>make_heap(L) ->
<br>    build_heap(L, trunc(length(L) / 2)).<br><br>heap_pop([Head | Rest]) when  length(Rest) == 0 -><br>    {Head, []};<br>heap_pop([Head | Rest]) -><br>    {RestBody, RestTail} = lists:split(length(Rest) - 1, Rest),
<br>    {Head, max_heapify(RestTail ++ RestBody, 1)}.<br><br>test_heap_pop([]) -><br>    done;<br>test_heap_pop(Heap) -><br>    {P, H} = heap_pop(Heap),<br>    io:format("~w, ~w~n", [P, H]),<br>    test_heap_pop(H).
<br><br>adjust_heap_node(Heap, Index) when trunc(Index / 2) == 0 -><br>    Heap;<br>adjust_heap_node(Heap, Index) -><br>    IndexValue = lists:nth(Index, Heap),<br>    ParentIndex = trunc(Index / 2),<br>    ParentValue = lists:nth(ParentIndex, Heap),
<br><br>    if<br>        IndexValue > ParentValue -><br>            NewHeap = exchange(Heap, Index, IndexValue, ParentIndex,<br>ParentValue, 1),<br>            adjust_heap_node(NewHeap, ParentIndex);<br>        true ->
<br>            Heap<br>    end.<br><br>heap_push(Heap, Push) -><br>    adjust_heap_node(Heap ++ [Push], length(Heap) + 1).<br><br>--<br>Victory won't come to me, unless I go to it.<br>_______________________________________________
<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions">http://www.erlang.org/mailman/listinfo/erlang-questions
</a><br></blockquote></div><br>