BAEL - 3482

Added unit test
This commit is contained in:
TINO 2019-12-21 16:55:42 +03:00
parent ebbe8d5ded
commit 64e55aea15
3 changed files with 44 additions and 27 deletions

View File

@ -3,16 +3,14 @@ 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);
int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length);
while (lastLeafsParentIndex >= 0) {
heapify(lastLeafsParentIndex);
lastLeafsParentIndex--;
@ -23,12 +21,12 @@ public class MinHeap {
int leftNodeIndex = getLeftNodeIndex(index);
int rightNodeIndex = getRightNodeIndex(index);
int smallestElementIndex = index;
if (leftNodeIndex < heapSize && heapNodes[leftNodeIndex].element < heapNodes[index].element)
if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element)
smallestElementIndex = leftNodeIndex;
if (rightNodeIndex < heapSize && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element)
if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element)
smallestElementIndex = rightNodeIndex;
if (smallestElementIndex != index) {
swap(index, smallestElementIndex);
heapify(smallestElementIndex);
@ -51,7 +49,7 @@ public class MinHeap {
return heapNodes[0];
}
void hepifyFromRoot(HeapNode root) {
void heapifyFromRoot() {
heapify(0);
}

View File

@ -2,12 +2,6 @@ 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;
@ -33,24 +27,14 @@ public class MinHeapMerge {
} else {
root.element = Integer.MAX_VALUE;
}
minHeap.hepifyFromRoot(root);
minHeap.heapifyFromRoot();
}
return resultingArray;
}
static void merge(int[][] array) {
static int[] 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);
return createMinHeapAndMergeArrays(array, heapNodes, resultingArraySize);
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.algorithms.minheapmerge;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class MinHeapmergeUnitTest {
private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } };
private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 };
@Test
public void givenSortedArraysWhenMergedShouldReturnASingleSortedarray() {
int[] resultArray = MinHeapMerge.merge(inputArray);
assertThat(resultArray.length, is(equalTo(10)));
assertThat(resultArray, is(equalTo(expectedArray)));
}
@Test
public void givenEmptyHeapNodesAndInputArrayWhenPopulatedShouldPopulateAndReturnResultArrayLength() {
int size = MinHeapMerge.populateHeapNodesAndDetermineResultingArrayLength(inputArray, new HeapNode[inputArray.length]);
assertThat(size, is(equalTo(10)));
}
@Test
public void givenArrayAndHeapNodesAndResultArraySizeWhenMergedShouldReturnASingleSortedarray() {
HeapNode[] heapNodes = new HeapNode[inputArray.length];
int size = MinHeapMerge.populateHeapNodesAndDetermineResultingArrayLength(inputArray, heapNodes);
int[] resultArray = MinHeapMerge.createMinHeapAndMergeArrays(inputArray, heapNodes, size);
assertThat(resultArray.length, is(equalTo(10)));
assertThat(resultArray, is(equalTo(expectedArray)));
}
}