Heaps are a data structure used to implement priority queues. I'm not sure yet what that means, but whatever. 

This routine from tibasicdev adds an element to a max-heap stored in L1, where the top node is L1(1) and children are 2I and 2I+1. 

Code:
1+dim(L1->I 
N->L1(I 
While N>L1(max(1,int(I.5 
    int(I.5->J 
    L1(J->L1(I 
    N->L1(J 
    J->I 
End


The following (untested and very unoptimized) snippet *probably* removes the top element from a max-heap. 

Code:
L1(dim(L1->N 
dim(L1)-1->dim(L1 
1->I 
While I2<dim(L1) and Ans 
    If N<max(L1(2I),L1(2I+1 
    Then 
        L1(2I+(L1(2I)<L1(2I+1->L1(I 
        1 
        Else 
        N->L1(I 
        0 
    End 
End


Will update with better code.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement