Skip to content

Commit e97c46d

Browse files
new binary heap implementation
1 parent aa780a7 commit e97c46d

File tree

3 files changed

+289
-5
lines changed

3 files changed

+289
-5
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
CC=gcc
22
CFLGAS=-Wall
3-
OBJ = binaryHeap.o
3+
OBJ = binaryHeap_2.o
44

55
%.o: %.c
66
$(CC) -c -o $@ $< $(CFLAGS)
77

8-
binaryHeap: $(OBJ)
8+
binaryHeap_2: $(OBJ)
99
$(CC) -o $@ $^ $(CFLAGS)
1010

1111
clean:
12-
rm -f binaryHeap binaryHeap.o
12+
rm -f binaryHeap_2 binaryHeap_2.o

Data_Struct_Implementation/binaryHeap/README.md

Lines changed: 147 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,156 @@
11
## Heap (Binary Implementation)
2-
#### Usage
2+
### Usage
33
```
44
make
55
./binaryHeap
66
```
77

8-
#### Code
8+
### Code
9+
10+
### **Max Heap**
11+
```c
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
#include <stdint.h>
15+
16+
typedef struct max_heap {
17+
int curIndex;
18+
int capacity;
19+
int *data;
20+
} MAX_HEAP, *pMAX_HEAP;
21+
22+
static void swap(int *a, int *b) {
23+
int tmp = *a;
24+
*a = *b;
25+
*b = tmp;
26+
}
27+
28+
#define LEFT_CHILD(i) (2*i+1)
29+
#define RIGHT_CHILD(i) (2*i+2)
30+
#define PARENT(i) ((i-1)/2)
31+
32+
pMAX_HEAP max_head_init(int size) {
33+
pMAX_HEAP new_heap = (pMAX_HEAP) malloc(sizeof(MAX_HEAP));
34+
35+
new_heap->curIndex = 0;
36+
new_heap->capacity = size;
37+
new_heap->data = malloc(sizeof(int)*size);
38+
39+
return new_heap;
40+
}
41+
42+
int get_max(pMAX_HEAP heap) {
43+
if (heap->curIndex == 0) {
44+
printf("Heap is Empty!\n");
45+
return -1;
46+
}
47+
48+
return heap->data[0];
49+
}
50+
51+
void up_heapify(pMAX_HEAP heap, int index) {
52+
int parent_index = PARENT(index);
53+
54+
if (index == 0 || heap->data[parent_index] >= heap->data[index])
55+
return;
56+
57+
swap(&heap->data[parent_index], &heap->data[index]);
58+
up_heapify(heap, parent_index);
59+
}
60+
61+
void down_heapify(pMAX_HEAP heap, int index) {
62+
int left_i = LEFT_CHILD(index);
63+
int right_i = RIGHT_CHILD(index);
64+
int target_index = index;
65+
66+
if (left_i < heap->curIndex && heap->data[left_i] > heap->data[index])
67+
target_index = left_i;
68+
69+
if (right_i < heap->curIndex && heap->data[right_i] > heap->data[index])
70+
target_index = right_i;
71+
72+
if (target_index == index)
73+
return;
74+
75+
swap(&heap->data[target_index], &heap->data[index]);
76+
down_heapify(heap, target_index);
77+
}
78+
79+
int insert(pMAX_HEAP heap, int value) {
80+
if (heap->curIndex == heap->capacity) {
81+
printf("Heap is Full!\n");
82+
return -1;
83+
}
84+
85+
heap->data[heap->curIndex] = value;
86+
heap->curIndex ++;
87+
88+
up_heapify(heap, heap->curIndex-1);
89+
90+
return 0;
91+
}
92+
93+
int pop(pMAX_HEAP heap) {
94+
if (heap->curIndex == 0) {
95+
printf("Heap is Empty!\n");
96+
return -1;
97+
}
98+
99+
swap(&heap->data[0], &heap->data[heap->curIndex-1]);
100+
heap->curIndex --;
101+
102+
down_heapify(heap, 0);
103+
104+
return 0;
105+
}
106+
107+
int main(int argc, int **argv) {
108+
pMAX_HEAP max_heap = max_head_init(5);
109+
110+
insert(max_heap, 1);
111+
printf("Max val: %d\n", get_max(max_heap));
112+
113+
insert(max_heap, 2);
114+
printf("Max val: %d\n", get_max(max_heap));
115+
116+
insert(max_heap, 3);
117+
printf("Max val: %d\n", get_max(max_heap));
118+
119+
insert(max_heap, 4);
120+
printf("Max val: %d\n", get_max(max_heap));
121+
122+
insert(max_heap, 5);
123+
printf("Max val: %d\n", get_max(max_heap));
124+
125+
insert(max_heap, 6);
126+
printf("Max val: %d\n", get_max(max_heap));
127+
128+
pop(max_heap);
129+
printf("Max val: %d\n", get_max(max_heap));
130+
131+
pop(max_heap);
132+
printf("Max val: %d\n", get_max(max_heap));
133+
134+
pop(max_heap);
135+
printf("Max val: %d\n", get_max(max_heap));
136+
137+
pop(max_heap);
138+
printf("Max val: %d\n", get_max(max_heap));
139+
140+
pop(max_heap);
141+
printf("Max val: %d\n", get_max(max_heap));
142+
143+
pop(max_heap);
144+
printf("Max val: %d\n", get_max(max_heap));
145+
146+
insert(max_heap, 6);
147+
printf("Max val: %d\n", get_max(max_heap));
148+
149+
return 0;
150+
}
151+
```
152+
153+
#### **Priority Queue**
9154
```c
10155
#include <stdlib.h>
11156
#include <stdio.h>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
5+
typedef struct max_heap {
6+
int curIndex;
7+
int capacity;
8+
int *data;
9+
} MAX_HEAP, *pMAX_HEAP;
10+
11+
static void swap(int *a, int *b) {
12+
int tmp = *a;
13+
*a = *b;
14+
*b = tmp;
15+
}
16+
17+
#define LEFT_CHILD(i) (2*i+1)
18+
#define RIGHT_CHILD(i) (2*i+2)
19+
#define PARENT(i) ((i-1)/2)
20+
21+
pMAX_HEAP max_head_init(int size) {
22+
pMAX_HEAP new_heap = (pMAX_HEAP) malloc(sizeof(MAX_HEAP));
23+
24+
new_heap->curIndex = 0;
25+
new_heap->capacity = size;
26+
new_heap->data = malloc(sizeof(int)*size);
27+
28+
return new_heap;
29+
}
30+
31+
int get_max(pMAX_HEAP heap) {
32+
if (heap->curIndex == 0) {
33+
printf("Heap is Empty!\n");
34+
return -1;
35+
}
36+
37+
return heap->data[0];
38+
}
39+
40+
void up_heapify(pMAX_HEAP heap, int index) {
41+
int parent_index = PARENT(index);
42+
43+
if (index == 0 || heap->data[parent_index] >= heap->data[index])
44+
return;
45+
46+
swap(&heap->data[parent_index], &heap->data[index]);
47+
up_heapify(heap, parent_index);
48+
}
49+
50+
void down_heapify(pMAX_HEAP heap, int index) {
51+
int left_i = LEFT_CHILD(index);
52+
int right_i = RIGHT_CHILD(index);
53+
int target_index = index;
54+
55+
if (left_i < heap->curIndex && heap->data[left_i] > heap->data[index])
56+
target_index = left_i;
57+
58+
if (right_i < heap->curIndex && heap->data[right_i] > heap->data[index])
59+
target_index = right_i;
60+
61+
if (target_index == index)
62+
return;
63+
64+
swap(&heap->data[target_index], &heap->data[index]);
65+
down_heapify(heap, target_index);
66+
}
67+
68+
int insert(pMAX_HEAP heap, int value) {
69+
if (heap->curIndex == heap->capacity) {
70+
printf("Heap is Full!\n");
71+
return -1;
72+
}
73+
74+
heap->data[heap->curIndex] = value;
75+
heap->curIndex ++;
76+
77+
up_heapify(heap, heap->curIndex-1);
78+
79+
return 0;
80+
}
81+
82+
int pop(pMAX_HEAP heap) {
83+
if (heap->curIndex == 0) {
84+
printf("Heap is Empty!\n");
85+
return -1;
86+
}
87+
88+
swap(&heap->data[0], &heap->data[heap->curIndex-1]);
89+
heap->curIndex --;
90+
91+
down_heapify(heap, 0);
92+
93+
return 0;
94+
}
95+
96+
int main(int argc, int **argv) {
97+
pMAX_HEAP max_heap = max_head_init(5);
98+
99+
insert(max_heap, 1);
100+
printf("Max val: %d\n", get_max(max_heap));
101+
102+
insert(max_heap, 2);
103+
printf("Max val: %d\n", get_max(max_heap));
104+
105+
insert(max_heap, 3);
106+
printf("Max val: %d\n", get_max(max_heap));
107+
108+
insert(max_heap, 4);
109+
printf("Max val: %d\n", get_max(max_heap));
110+
111+
insert(max_heap, 5);
112+
printf("Max val: %d\n", get_max(max_heap));
113+
114+
insert(max_heap, 6);
115+
printf("Max val: %d\n", get_max(max_heap));
116+
117+
pop(max_heap);
118+
printf("Max val: %d\n", get_max(max_heap));
119+
120+
pop(max_heap);
121+
printf("Max val: %d\n", get_max(max_heap));
122+
123+
pop(max_heap);
124+
printf("Max val: %d\n", get_max(max_heap));
125+
126+
pop(max_heap);
127+
printf("Max val: %d\n", get_max(max_heap));
128+
129+
pop(max_heap);
130+
printf("Max val: %d\n", get_max(max_heap));
131+
132+
pop(max_heap);
133+
printf("Max val: %d\n", get_max(max_heap));
134+
135+
insert(max_heap, 6);
136+
printf("Max val: %d\n", get_max(max_heap));
137+
138+
return 0;
139+
}

0 commit comments

Comments
 (0)