-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathheap.c
139 lines (123 loc) · 3.19 KB
/
heap.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Created by Once on 2019/11/25.
//
#include "heap.h"
#include <stdlib.h>
Heap* new_heap(int length, int (*compare)(void*, void*)){
if(length < 3 || compare == NULL){
perror("length too small for null exception");
return NULL;
}
Heap *heap = (Heap*)malloc(sizeof(Heap));
if(!heap){
perror("alloc for heap error");
return NULL;
}
heap->length = length;
heap->size = 0;
heap->compare = compare;
heap->array = (HNode*)malloc(sizeof(HNode) * length);
if(!heap->array){
perror("alloc for array error");
free(heap);
return NULL;
}
return heap;
}
int heap_is_empty(Heap *heap){
if(heap == NULL)
return 1;
return heap->size == 0;
}
int heap_is_full(Heap *heap){
if(heap == NULL)
return 1;
return heap->size == heap->length;
}
static int parent(int i){
return (i - 1) / 2;
}
static int left(int i){
return 2 * i + 1;
}
static int right(int i){
return 2 * i + 2;
}
static void swap(Heap *heap, int i, int j){
Object value = heap->array[i].value;
heap->array[i].value = heap->array[j].value;
heap->array[j].value = value;
}
void heap_push(Heap *heap, Object value){
if(heap == NULL){
perror("null pointer exception");
return;
}
if(heap_is_full(heap)){
heap->length += heap->length;
heap->array = (HNode*)realloc(heap->array, heap->length);
}
heap->size++;
int i = heap->size - 1;
heap->array[i].value = value;
while(i != 0 && heap->compare(heap->array[i].value, heap->array[parent(i)].value)){
swap(heap, i, parent(i));
i = parent(i);
}
}
Object heap_top(Heap *heap){
if(heap == NULL || heap->size == 0)
return NULL;
return heap->array[0].value;
}
static void heapify(Heap *heap, int i){
int l = left(i);
int r = right(i);
int small_max = i;
if(l < heap->size && heap->compare(heap->array[l].value, heap->array[i].value))
small_max = l;
if(r < heap->size && heap->compare(heap->array[r].value, heap->array[small_max].value))
small_max = r;
if(i != small_max){
swap(heap, i, small_max);
heapify(heap, small_max);
}
}
void heap_pop(Heap *heap){
if(heap == NULL || heap->size == 0)
return;
if(heap->size == 1){
heap->size--;
return;
}
swap(heap, 0, heap->size - 1);
heap->size--;
heapify(heap, 0);
}
void heap_min_decrease_key_just(Heap *heap, int i){
if(heap == NULL || heap->size == 0 || i < 0)
return;
while(i != 0 && heap->compare(heap->array[i].value, heap->array[parent(i)].value)){
swap(heap, i, parent(i));
i = parent(i);
}
}
void heap_max_increase_key_just(Heap *heap, int i){
if(heap == NULL || heap->size == 0 || i < 0)
return;
while(i != 0 && heap->compare(heap->array[i].value, heap->array[parent(i)].value)){
swap(heap, i, parent(i));
i = parent(i);
}
}
void heap_max_decrease_key_just(Heap *heap, int i){
if(heap == NULL || heap->size == 0 || i < 0)
return;
heapify(heap, i);
}
void heap_clear(Heap *heap){
if(heap == NULL)
return;
free(heap->array);
free(heap);
}