-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_heap.c
102 lines (74 loc) · 1.71 KB
/
min_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
//
// main.c
// heap_change_priority
//
// Created by Minjeong's Mac on 2016. 12. 10..
// Copyright (c) 2016년 Minjeong's Mac. All rights reserved.
//
#include <stdio.h>
#define MAX_ELEMENT 200
typedef struct{
int key;
}element;
typedef struct{
element heap[MAX_ELEMENT];
int heap_size;
}HeapType;
void init(HeapType *h)
{
h->heap_size = 0;
}
void insert_min_heap(HeapType *h,element item){
int i;
i = ++(h->heap_size);
while((i!=1)&&(item.key<h->heap[i/2].key)){
h->heap[i] = h->heap[i/2];
i/=2;
}
h->heap[i]=item;
}
element delete_min_heap(HeapType *h)
{
int parent,child;
element item,temp;
item = h->heap[1];
temp = h->heap[(h->heap_size)--];
parent = 1;
child = 2;
while(child<=h->heap_size)
{
if((child<h->heap_size)&&
(h->heap[child].key)>h->heap[child+1].key)
child++;
if(temp.key <= h->heap[child].key)
break;
h->heap[parent] = h->heap[child];
parent = child;
child *= 2;
}
h->heap[parent] = temp;
return item;
}
void main(){
element e1 = {10};
element e2 = {5};
element e3 = {30};
element e4 = {89};
element e5 = {102};
element e6 = {37};
element e7,e8,e9;
HeapType h1;
init(&h1);
insert_min_heap(&h1,e1);
insert_min_heap(&h1,e2);
insert_min_heap(&h1,e3);
insert_min_heap(&h1,e4);
insert_min_heap(&h1,e5);
insert_min_heap(&h1,e6);
e7 = delete_min_heap(&h1);
printf("%d\n",e7.key);
e8 = delete_min_heap(&h1);
printf("%d\n",e8.key);
e9 = delete_min_heap(&h1);
printf("%d\n",e9.key);
}