-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityQueue.cpp
More file actions
83 lines (71 loc) · 1.65 KB
/
Copy pathpriorityQueue.cpp
File metadata and controls
83 lines (71 loc) · 1.65 KB
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 100
typedef int element;
typedef struct {
element heap[N];
int heapSize;
} HeapType;
void init(HeapType *H) {
H->heapSize = 0;
}
void upHeap(HeapType *H) {
int i = H->heapSize;
element key = H->heap[i];
while(i!=1 && H->heap[i] > H->heap[i/2]) {
H->heap[i] = H->heap[i/2];
}
H->heap[i] = key;
}
void InsertHeap(HeapType *H, element e) {
H->heapSize++;
H->heap[H->heapSize] = e;
upHeap(H);
}
void print(HeapType *H, int i) {
for(int i=1; i<=H->heapSize; i++) {
printf("[%d] ", H->heap[i]);
}
printf("\n");
}
void downHeap(HeapType *H) {
element key = H->heap[1];
int parent = 1, child = 2;
while(child <= H->heapSize) {
if(child < H->heapSize && H->heap[child+1] > H->heap[child]) {
child++;
}
if(key >= H->heap[child]) break;
H->heap[parent] = H->heap[child];
parent = child;
child *=2;
}
H->heap[parent] = key;
}
element removeHeap(HeapType *H) {
element key = H->heap[1];
H->heap[1] = H->heap[H->heapSize];
H->heapSize--;
downHeap(H);
return key;
}
void HeapSort(HeapType *H) {
}
int main() {
HeapType H;
init(&H);
InsertHeap(&H, 9); InsertHeap(&H, 7); InsertHeap(&H, 6);
InsertHeap(&H, 5); InsertHeap(&H, 4); InsertHeap(&H, 3);
InsertHeap(&H, 2); InsertHeap(&H, 2); InsertHeap(&H, 1);
InsertHeap(&H, 3);
print(&H, 1);
element k = removeHeap(&H);
// printf("%d ", k);
print(&H, 1);
removeHeap(&H);
print(&H, 1);
return 0;
}