-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapBinario.c
62 lines (46 loc) · 1.11 KB
/
HeapBinario.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
#include <stdio.h>
#include <stdlib.h>
int getLeftIndex(int index){
return index * 2 + 1;
}
int getRightIndex(int index){
return index * 2 + 2;
}
int getIndexPai(int index){
return (index - 1) / 2;
}
void shiftUP(int *heap, int tamanho){
int indexPai = getIndexPai(tamanho - 1);
int valor = heap[tamanho - 1];
while(indexPai > 0 && valor < heap[indexPai]){
int temp = heap[indexPai];
heap[indexPai] = valor;
heap[tamanho - 1] = temp;
tamanho--;
indexPai = getIndexPai(tamanho - 1);
}
}
void insert(int* tamanho, int numero, int* heap){
if(heap[0] == 0){
printf("O heap esta vazio.");
return;
}
heap[*tamanho] = numero;
shiftUP(heap, *tamanho);
*tamanho ++;
}
int* criarHeap(int tamanho, int sizeHeap, int raiz){
if(tamanho == 0){
return NULL;
}
int* heap = (int*)calloc(tamanho, (sizeof(int)));
if(heap == NULL){
printf("Erro de alocacao de memoria.");
return NULL;
}
heap[0] = raiz;
return heap;
}
int main(){
return 0;
}