-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaxHeap.cpp
130 lines (103 loc) · 2.78 KB
/
MaxHeap.cpp
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: MaxHeap.cpp
* Author: carloco
*
* Created on December 20, 2017, 2:41 PM
*/
#include "MaxHeap.h"
#include <iostream>
#include <limits>
using namespace std;
/*MaxHeap::MaxHeap(const MaxHeap& orig) {
}
//Destructor
MaxHeap::~MaxHeap() {
}*/
//Constructor con el tamannio del Heap
MaxHeap::MaxHeap(int maxsize) {
heap = new double *[maxsize];
for (int i = 0; i < maxsize; i++) {
heap[i] = new double[2];
heap[i][0] = 0;//indice del punto mas cercano
heap[i][1] = numeric_limits<double>::max();//distancia del punto a la consulta
}
maxElementos = maxsize;
}
void MaxHeap::Intercambia(int i, int j) {
double temp[2];
temp[0] = heap[i][0];
temp[1] = heap[i][1];
heap[i][0] = heap[j][0];
heap[i][1] = heap[j][1];
heap[j][0] = temp[0];
heap[j][1] = temp[1];
}
void MaxHeap::maxHeapify(int i, int j) {
if ((2 * i + 1) <= j) {
int k;
if ((2 * i + 2) <= j) {
k = (heap[2 * i + 2][1] >= heap[2 * i + 1][1]) ? 2 * i + 2 : 2 * i + 1;
} else {
k = 2 * i + 1;
}
//Ordena en base a la distancia del punto
if (heap[i][1] < heap[k][1]) {
Intercambia(i, k);
maxHeapify(k, j);
}
}
}
void MaxHeap::insert(long posElement, float distanceToQuery) {
//verifica que llene el heap
/*if(numElementos < maxElementos) {
heap[numElementos][0] = posElement;
heap[numElementos][1] = distanceToQuery;
numElementos++;
buildHeap();
}else {
//Se asume que el punto que viene es mayor al maxheap
heap[0][0] = posElement;
heap[0][1] = distanceToQuery;
maxHeapify(0, maxElementos - 1);
}*/
if(numElementos < maxElementos) {
numElementos++;
}
heap[0][0] = posElement;
heap[0][1] = distanceToQuery;
maxHeapify(0, maxElementos - 1);
}
void MaxHeap::buildHeap() {
//for (int i = (sizeof (heap) / sizeof (heap[0])) / 2; i >= 0; i--) {
//cout << "Max Elementos: " << maxElementos << endl;
for (int i = (maxElementos * 0.5); i >= 0; i--) {
//cout << "I: " << i << endl;
maxHeapify(i, maxElementos - 1);
}
}
double MaxHeap::getDistanceMax() {
return heap[0][1];
}
double** MaxHeap::getArray() {
return heap;
}
int MaxHeap::getCantidad() {
return numElementos;
}
void MaxHeap::showArray() {
for(int i=0; i<maxElementos; i++) {
cout << heap[i][0] << "|";
}
cout << endl;
}
void MaxHeap::imprimirDistancias() {
for(int i=0; i<maxElementos; i++) {
cout << heap[i][1] << "|";
}
cout << endl;
}