Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [Code] Minimum Complete Binary Heap

Author
Message
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 8th Jan 2015 21:52 Edited at: 9th Jan 2015 00:15
Reading: Heap Binary Heap

Functions
new_Heap() - O(1)
Heap_size() - O(1)
Heap_insert() - O(log n)
Heap_removeMin() - O(log n)
Heap_peekMin() - O(1)

Code
Main.agc


Heap.Implementation.agc

___________________________________________________________________________________
Hi everyone! I thought I'd share my code for a minimum sorted complete binary heap (A fancy priority queue)
To use it with your own code:
1: Find and replace (Ctrl + H) "HeapElement_UDT" with your own UDT you wish to use.
2: Modify Heap_elementLessThan() and Heap_elementGreaterThan() to observe the variable you wish to sort by
(If using multiple different element UDTs with heaps) 3: Rename ALL heap functions to be specific to the UDT they store (Heap_size() is optional here)
|--> ex: MyUDTHeap_insert()

Final note: The Main.agc I included is just an example program I quickly put together. You shouldn't really inspect the contents of a heap as if it is an array. The functions provided are the proper way of interacting with a heap. However, I wanted to display how the heap stored its elements in this example.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 8th Jan 2015 22:39
Very nice. Useful for path-finding.

Onwards and sometimes upwards

Login to post a reply

Server time is: 2024-11-25 14:58:12
Your offset time is: 2024-11-25 14:58:12