/************************************************* * heap.h * * Header for an implementation of a simple heap, also known as a * "priority queue", a partially ordered structure which has quick * access to its largest (descending=TRUE) or smallest * (descending=FALSE) node. * * The data contained within the node isn't used by any of the heap * functions; use it for any additional information you need to store. * * Since "struct node_struct" and "struct heap_struct" are defined * within heap.c, only routines in heap.c can access its internals * directly; from other files that include this header only what's * defined by this API is available. * * Copyright Jim Mahoney (mahoney@marlboro.edu) April 2007 * This software may be used under the GPL or Artistic license. * * $Id: heap.h 12567 2007-04-09 16:12:05Z mahoney $ *************************************************/ #ifndef __HEAP__ #define __HEAP__ 1 // define "bool" type #include "utility.h" // "node" and "heap" types. typedef struct node_struct *node; typedef struct heap_struct *heap; // Node creation, getters, and setters. node new_node(int weight, void* data); void set_node_data(node n, void* data); void* get_node_data(node n); int get_node_weight(node n); // Heap creation (from an array of nodes) and access to internals. // descending=TRUE says that pop_heap should return node with largest weight, // while descending=FALSE means it returns smallest. heap new_heap(int n_nodes, node* nodes, bool descending); int get_heap_size(heap h); // Add a node to a heap. void push_heap(heap h, node n); // Remove and return the node with largest (descending==TRUE) or // smallest (descending==FALSE) weight. If size==0, return NULL. node pop_heap(heap h); // Measure "steps" in inner loop within new_heap, push_heap, and/or pop_heap void reset_step_counter(); int get_step_counter(); // Debugging routines. void print_heap(heap h); bool heap_check(heap h); // TRUE => parents and children ordered correctly #endif