daniberg.com

posts github chip8 rtc guitar

Maxheap and JavaFX

I was goofying around playing with different tools and languages to render images and I've decided to give JavaFX a try. The idea was to render a simple data structure.

So, I've implemented a max-heap in java and rendering code using javafx. The source code can be found here.

The max-heap implementation is straightforward and the api contains the usual suspects

void add(int i);

/** O(n) bulk insertion. */
static MaxHeap addAll(List xs);

/** Remove the greatest value from the heap. */
int remove();

/** Returns the sorted values in O(n lg n) in sorted . addAll. */
List sorted();

The rendering code is more interesting and written in scala. It's approximately 150 loc.

The main function creates a heap with random values, renders it, and finally saves the result into the file /tmp/maxheap.png

Here's a sample output.

maxheap

It's easy to tweak the main function to generate heaps of different sizes and values.

©2023 daniberg.com