55 lines
1.5 KiB
C
55 lines
1.5 KiB
C
/**
|
|
* Generic test class for declaring
|
|
* a simple binary heap
|
|
* _ _ _ _
|
|
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
|
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
|
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
|
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
|
* |___/
|
|
* ____ __ __ _
|
|
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
|
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
|
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
|
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
|
* |___/
|
|
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
|
* Copyright (c) Sven Vogel
|
|
*/
|
|
|
|
#ifndef _BINHEAP_H_
|
|
#define _BINHEAP_H_
|
|
|
|
#include <stdlib.h>
|
|
#include "../utils/arrays.h"
|
|
|
|
/**
|
|
* Maximum binary heap for signed integers
|
|
*/
|
|
typedef struct maxbinheap {
|
|
// amount of elements that fit into the heap
|
|
unsigned int capacity;
|
|
// number of inserted elements
|
|
unsigned int size;
|
|
// data of heap
|
|
int* array;
|
|
} Maxbinheap;
|
|
|
|
// create new heap
|
|
Maxbinheap newMaxbinheap(unsigned int capacity);
|
|
// free memory
|
|
void destroyMaxbinheap(Maxbinheap *heap);
|
|
// create a new binary heap from the array
|
|
Maxbinheap fromArray(const int* array, unsigned int len);
|
|
|
|
// add new element
|
|
void insert(Maxbinheap *heap, int element);
|
|
|
|
// remove maximum element
|
|
int removeMax(Maxbinheap *heap);
|
|
|
|
// sort array using an inplace binary heap
|
|
void heapsort(int* array, const int len);
|
|
|
|
#endif
|