parent
1b81033840
commit
ebbe8d5ded
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class HeapNode {
|
||||
|
||||
int element;
|
||||
int arrayIndex;
|
||||
int nextElementIndex = 1;
|
||||
|
||||
public HeapNode(int element, int arrayIndex) {
|
||||
this.element = element;
|
||||
this.arrayIndex = arrayIndex;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class MinHeap {
|
||||
|
||||
HeapNode[] heapNodes;
|
||||
int heapSize;
|
||||
|
||||
public MinHeap(HeapNode heapNodes[]) {
|
||||
this.heapSize = heapNodes.length;
|
||||
this.heapNodes = heapNodes;
|
||||
heapifyFromLastLeafsParent();
|
||||
}
|
||||
|
||||
void heapifyFromLastLeafsParent() {
|
||||
int lastLeafsParentIndex = getParentNodeIndex(heapSize);
|
||||
while (lastLeafsParentIndex >= 0) {
|
||||
heapify(lastLeafsParentIndex);
|
||||
lastLeafsParentIndex--;
|
||||
}
|
||||
}
|
||||
|
||||
void heapify(int index) {
|
||||
int leftNodeIndex = getLeftNodeIndex(index);
|
||||
int rightNodeIndex = getRightNodeIndex(index);
|
||||
int smallestElementIndex = index;
|
||||
if (leftNodeIndex < heapSize && heapNodes[leftNodeIndex].element < heapNodes[index].element)
|
||||
smallestElementIndex = leftNodeIndex;
|
||||
|
||||
if (rightNodeIndex < heapSize && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element)
|
||||
smallestElementIndex = rightNodeIndex;
|
||||
|
||||
if (smallestElementIndex != index) {
|
||||
swap(index, smallestElementIndex);
|
||||
heapify(smallestElementIndex);
|
||||
}
|
||||
}
|
||||
|
||||
int getParentNodeIndex(int index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
int getLeftNodeIndex(int index) {
|
||||
return (2 * index + 1);
|
||||
}
|
||||
|
||||
int getRightNodeIndex(int index) {
|
||||
return (2 * index + 2);
|
||||
}
|
||||
|
||||
HeapNode getRootNode() {
|
||||
return heapNodes[0];
|
||||
}
|
||||
|
||||
void hepifyFromRoot(HeapNode root) {
|
||||
heapify(0);
|
||||
}
|
||||
|
||||
void swap(int i, int j) {
|
||||
HeapNode temp = heapNodes[i];
|
||||
heapNodes[i] = heapNodes[j];
|
||||
heapNodes[j] = temp;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class MinHeapMerge {
|
||||
|
||||
static void printArray(int[] array) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
System.out.print(array[i] + " ");
|
||||
}
|
||||
}
|
||||
|
||||
static int populateHeapNodesAndDetermineResultingArrayLength(int[][] array, HeapNode[] heapNodes) {
|
||||
int resultSize = 0;
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
HeapNode node = new HeapNode(array[i][0], i);
|
||||
heapNodes[i] = node;
|
||||
resultSize += array[i].length;
|
||||
}
|
||||
return resultSize;
|
||||
}
|
||||
|
||||
static int[] createMinHeapAndMergeArrays(int[][] array, HeapNode[] heapNodes, int resultingArraySize) {
|
||||
|
||||
MinHeap minHeap = new MinHeap(heapNodes);
|
||||
int[] resultingArray = new int[resultingArraySize];
|
||||
|
||||
for (int i = 0; i < resultingArraySize; i++) {
|
||||
HeapNode root = minHeap.getRootNode();
|
||||
resultingArray[i] = root.element;
|
||||
|
||||
if (root.nextElementIndex < array[root.arrayIndex].length) {
|
||||
root.element = array[root.arrayIndex][root.nextElementIndex++];
|
||||
} else {
|
||||
root.element = Integer.MAX_VALUE;
|
||||
}
|
||||
minHeap.hepifyFromRoot(root);
|
||||
}
|
||||
return resultingArray;
|
||||
}
|
||||
|
||||
static void merge(int[][] array) {
|
||||
HeapNode[] heapNodes = new HeapNode[array.length];
|
||||
int resultingArraySize = populateHeapNodesAndDetermineResultingArrayLength(array, heapNodes);
|
||||
|
||||
int[] resultingArray = createMinHeapAndMergeArrays(array, heapNodes, resultingArraySize);
|
||||
|
||||
printArray(resultingArray);
|
||||
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
int[][] array = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 650, 200 }, { 3, 4 }, { 1001, 6456, 23000 } };
|
||||
|
||||
merge(array);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue