From ebbe8d5dedc0f64b4ec73791ec7f5992c8f48436 Mon Sep 17 00:00:00 2001 From: TINO Date: Sat, 14 Dec 2019 17:40:21 +0300 Subject: [PATCH] BAEL- 3482 Initial commit --- .../algorithms/minheapmerge/HeapNode.java | 13 ++++ .../algorithms/minheapmerge/MinHeap.java | 63 +++++++++++++++++++ .../algorithms/minheapmerge/MinHeapMerge.java | 56 +++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeapMerge.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java new file mode 100644 index 0000000000..9e8439cb87 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java @@ -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; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java new file mode 100644 index 0000000000..d19ffa6e46 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java @@ -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; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeapMerge.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeapMerge.java new file mode 100644 index 0000000000..62cb46321c --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeapMerge.java @@ -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); + } +}