libvieter/src/heap
Jef Roosens ab418e57b3
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/test Pipeline was successful Details
ci/woodpecker/pr/test-mem Pipeline was successful Details
test: also test with -O3 which can produce extra errors
2023-01-26 12:24:34 +01:00
..
README.md docs(heap): add readme 2023-01-26 11:03:41 +01:00
vieter_heap.c refactor(heap): some better variable names; some more tests 2023-01-25 22:12:22 +01:00
vieter_heap_internal.h refactor(heap): some better variable names; some more tests 2023-01-25 22:12:22 +01:00
vieter_heap_tree.c test: also test with -O3 which can produce extra errors 2023-01-26 12:24:34 +01:00
vieter_heap_tree.h test(heap): add random test that exposes some faults 2023-01-24 21:19:08 +01:00

README.md

This min-heap implementation is a pretty standard binomial heap.

Representation in memory

A heap consists of one or more binomial trees, each with a different order k and 2^k total nodes. This heap can contain 2^64 - 1 elements at most, which is far more than your memory can contain, but it's still fun to mention.

A tree does not have its own memory structure; a node that's the root of a binomial tree is simply called the tree.

Each node has the following layout:

typedef struct vieter_heap_node {
    uint64_t key;
    void *data;
    struct vieter_heap_node *largest_order;
    union {
      struct vieter_heap_node *next_tree;
      struct vieter_heap_node *next_largest_order;
    } ptr;
    uint8_t order;
} vieter_heap_node;

Each node has a pointer to its child with the largest order (if the node's order is 0, this pointer will be NULL). Each non-root node has a pointer to its sibling with the next-highest order. These pointers allow the children of a binomial tree to be recombined into a new tree, once their root has been pop'ed.

Roots point to the binomial tree in the heap with the next largest order.