Size: 851 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// cs/q/heap/heap.h
#ifndef CS_Q_HEAP_HEAP_H
#define CS_Q_HEAP_HEAP_H

#include <stdbool.h>

typedef struct Heap {
  int size;
  int capacity;
  int* values;
} Heap;

// Creates a new heap with the given, fixed capacity.
Heap* NewHeap(int capacity);
// Applies the heap property downward from the given root.
void HeapifyDown(Heap* heap, int i);
// Applies the heap property upward from the given child.
void HeapifyUp(Heap* heap, int i);
// Tries to insert a new value into the heap. Returns true
// if a value was inserted.
// O(log(n)).
bool Insert(Heap* heap, int value);
// Presents the max element in this heap without removing.
int PeekMax(Heap* heap);
// Removes and returns the max value in the heap.
// O(log(n)).
int PopMax(Heap* heap);
// Prints out a representation of the heap.
// void PrintHeap(Heap* heap);

#endif  // CS_Q_HEAP_HEAP_H
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.