-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_heap.c
157 lines (143 loc) · 3.16 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX_SIZE 10
typedef struct Node{
int data;
struct Node* left;
struct Node* right;
} Node;
Node* root = NULL;
int heap_size = 0;
void swap(Node* a, Node* b){
int temp = a->data;
a->data = b->data;
b->data = temp;
}
int calc_height(int num_nodes){ // calculates the height of a given node
return log(num_nodes)/log(2);
}
char* dec_to_bin(int x){ // converts decimal to binary (10 bits)
static char bits[MAX_SIZE] = "0000000000";
int counter = MAX_SIZE-1;
while(x != 0){
if(x%2 == 1){
bits[counter] = (char)49;
}else{
bits[counter] = (char)48;
}
counter--;
x /= 2;
}
return bits;
}
Node* create_node(int data){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
Node* get_node(int number){ // returns pointer to specified node
char* bin_val = dec_to_bin(number);
struct Node* trav = root;
int counter = MAX_SIZE - calc_height(number);
while(counter < MAX_SIZE){
if(bin_val[counter] == '0'){
trav = trav->left;
}else{
trav = trav->right;
}
counter++;
}
return trav;
}
void insert(int data){
Node* temp = create_node(data);
if(root == NULL){
root = temp;
}else{
int new_heap_size = heap_size + 1;
char* bin_val = dec_to_bin(new_heap_size);
struct Node* trav = root;
int counter = MAX_SIZE - calc_height(new_heap_size);
while(counter < MAX_SIZE - 1){
if(bin_val[counter] == '0'){
trav = trav->left;
}else{
trav = trav->right;
}
counter++;
}
if(bin_val[counter] == '0'){
trav->left = temp;
}else{
trav->right = temp;
}
}
heap_size++;
int current = heap_size;
int parent = heap_size/2;
while(parent > 0 && get_node(parent)->data > get_node(current)->data){
swap(get_node(parent), get_node(current));
current = parent;
parent = current/2;
}
}
void traverse(){ // level order traversal
for(int i = 1; i <= heap_size; i++){
printf("%d ", get_node(i)->data);
}
}
void heapify(int current)
{
int min = current;
int left = 2*current;
int right = 2*current + 1;
if(left < heap_size && get_node(current)->data > get_node(left)->data){
min = left;
}
if(right < heap_size && get_node(min)->data > get_node(right)->data){
min = right;
}
if(min != current){
swap(get_node(min), get_node(current));
heapify(min);
}
}
int extract_min(){
int min = root->data;
swap(root, get_node(heap_size));
free(get_node(heap_size));
heap_size--;
heapify(1);
return min;
}
int is_empty(){
if(root == NULL){
return 1;
}else{
return 0;
}
}
int main(){
printf("%d\n", is_empty());
insert(5);
insert(6);
insert(7);
insert(19);
insert(20);
insert(33);
insert(44);
insert(2);
insert(15);
traverse();
printf("\n%d\n", extract_min());
traverse();
printf("\n%d\n", is_empty());
insert(100);
insert(3);
insert(4);
traverse();
return 0;
}