-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path126-heaps.js
107 lines (93 loc) · 3.63 KB
/
126-heaps.js
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
// Implementar en Javascript un Heap con todos sus metodos correspondientes
class MinHeap {
constructor() {
this.heap = [];
}
// Función para obtener el índice del padre de un nodo en el heap
parentIndex(index) {
return Math.floor((index - 1) / 2);
}
// Función para obtener el índice del hijo izquierdo de un nodo en el heap
leftChildIndex(index) {
return index * 2 + 1;
}
// Función para obtener el índice del hijo derecho de un nodo en el heap
rightChildIndex(index) {
return index * 2 + 2;
}
// Función para intercambiar dos elementos en el heap
swap(index1, index2) {
const temp = this.heap[index1];
this.heap[index1] = this.heap[index2];
this.heap[index2] = temp;
}
// Función para insertar un nuevo elemento en el heap
insert(value) {
// Agregar el nuevo elemento al final del heap
this.heap.push(value);
// Reajustar el heap para mantener la propiedad de heap
this.heapifyUp();
}
// Función para reajustar el heap hacia arriba (hacia la raíz)
heapifyUp() {
let currentIndex = this.heap.length - 1;
while (currentIndex > 0) {
const parentIndex = this.parentIndex(currentIndex);
// Si el valor del nodo actual es menor que el valor del padre, intercambiamos los nodos
if (this.heap[currentIndex] < this.heap[parentIndex]) {
this.swap(currentIndex, parentIndex);
currentIndex = parentIndex;
} else {
break; // Si el valor del nodo actual es mayor o igual al valor del padre, el heap está en orden
}
}
}
// Función para extraer el elemento mínimo (raíz) del heap
extractMin() {
if (this.heap.length === 0) {
return null;
}
if (this.heap.length === 1) {
return this.heap.pop();
}
const minValue = this.heap[0];
// Reemplazar la raíz con el último elemento del heap
this.heap[0] = this.heap.pop();
// Reajustar el heap para mantener la propiedad de heap
this.heapifyDown();
return minValue;
}
// Función para reajustar el heap hacia abajo (hacia las hojas)
heapifyDown() {
let currentIndex = 0;
while (true) {
const leftChildIndex = this.leftChildIndex(currentIndex);
const rightChildIndex = this.rightChildIndex(currentIndex);
let smallestIndex = currentIndex;
// Encontrar el índice del hijo con el valor más pequeño
if (leftChildIndex < this.heap.length && this.heap[leftChildIndex] < this.heap[smallestIndex]) {
smallestIndex = leftChildIndex;
}
if (rightChildIndex < this.heap.length && this.heap[rightChildIndex] < this.heap[smallestIndex]) {
smallestIndex = rightChildIndex;
}
// Si el valor del nodo actual es mayor o igual que los valores de los hijos, el heap está en orden
if (smallestIndex === currentIndex) {
break;
}
// Si el valor del nodo actual es mayor que el valor del hijo más pequeño, intercambiamos los nodos
this.swap(currentIndex, smallestIndex);
currentIndex = smallestIndex;
}
}
}
// Ejemplo de uso de MinHeap
const minHeap = new MinHeap();
minHeap.insert(3);
minHeap.insert(2);
minHeap.insert(1);
minHeap.insert(5);
minHeap.insert(4);
console.log(minHeap.heap); // Output: [1, 2, 3, 5, 4]
console.log(minHeap.extractMin()); // Output: 1
console.log(minHeap.heap); // Output: [2, 4, 3, 5]