Commit b4187ded authored by Christophe Rhodes's avatar Christophe Rhodes
Browse files

updates to 13 14 15 16 17 18 19 20

parent fdfc9b05
#+TITLE: Algorithms & Data Structures: Lab 13
#+SUBTITLE: week of 28th January 2019
#+include: ../labsheet.org
* Setup
** Saving your work from last week
By now you should be familiar with the operations needed to save
your work. Make sure you commit your work to version control
often, and always have a backup copy, ideally remotely (for example
in your own account on the department's gitlab installation.)
** Downloading this week's distribution
Once you have successfully saved your changes from last week, you
can get my updates by doing
#+begin_example
git pull
#+end_example
which /should/ automatically merge in new content. After the =git
pull= command, you should have a new directory containing this
week's material (named =13/=) alongside your existing directories.
* Binary heap
** Implementing the class
I have provided you with a skeleton implementation (as usual) of a
heap, along with tests.
1. check that you can compile the skeleton implementation.
2. run the tests, and check that all the tests fail.
3. implement the methods required. I suggest attempting them in
the following order:
1. the parent / left-child / right-child helper routines;
2. the simple constructor (with a single int argument, being the
size of the heap storage area);
3. insert
4. maximum
5. the incremental constructor
6. maxHeapify and buildMaxHeap
7. the direct (non-incremental) constructor
After each step, run the tests once more, and check that the
number of failing tests has decreased.
** Counting constructor operations
Write code to construct heaps with 10, 100, 1000, 10000 elements.
Count the number of \textsc{swap} or element move operations when
using each of the incremental and the direct constructors. What is
the worst case input for constructing a max-heap? Is it the same
for the incremental and or the direct constructor? Complete the
following table.
| N | worst case (incremental) | worst case (direct) |
|-------+--------------------------+---------------------|
| 1 | 0 | 0 |
| 2 | 1 | 1 |
| 3 | 2 | 1 |
| 10 | | |
| 100 | | |
| 1000 | | |
| 10000 | | |
Verify empirically the time complexity results given in the
lectures for the two different constructors? Is one of them always
cheaper (in terms of element move operations) than the other?
** Implement heapsort
Implement heapsort. You will need to:
1. extend the skeleton to add a method with an appropriate signature;
2. implement the heapsort method itself;
3. write tests to check the functionality of heapsort;
4. add the tests to the test suite by editing the Makefile
appropriately.
(Before you start, think about which order you will do these things
in, and why.)
#include "Heap.hpp"
unsigned int parent(unsigned int i) {
return 0;
}
unsigned int left(unsigned int i) {
return 0;
}
unsigned int right(unsigned int i) {
return 0;
}
Heap::Heap(unsigned int len) {
}
Heap::Heap(std::vector<int> source, bool incremental) {
}
void Heap::maxHeapify(int i) {
}
void Heap::buildMaxHeap() {
}
int Heap::maximum() {
return 0;
}
void Heap::insert(int k) {
}
int Heap::extractMax() {
return 0;
}
#ifndef HEAP_HPP
#define HEAP_HPP
#include <vector>
unsigned int parent(unsigned int);
unsigned int left(unsigned int);
unsigned int right(unsigned int);
class Heap {
public: // for cppunit testing purposes
std::vector<int> array;
unsigned int heap_size;
public:
Heap(unsigned int);
Heap(std::vector<int>, bool = false);
void maxHeapify(int);
void buildMaxHeap();
void insert(int);
int maximum();
int extractMax();
};
#endif
#include <cppunit/extensions/HelperMacros.h>
#include "Heap.hpp"
class HeapConstructorsTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(HeapConstructorsTest);
CPPUNIT_TEST(testMinimalConstructors);
CPPUNIT_TEST(testIncrementalConstructor);
CPPUNIT_TEST(testDirectConstructor);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {};
void tearDown() {};
void testMinimalConstructors();
void testIncrementalConstructor();
void testDirectConstructor();
};
void HeapConstructorsTest::testMinimalConstructors() {
Heap h = Heap(0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("heap size violation for 0-length heap", 0U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("array length violation for 0-length heap", 0UL, (unsigned long) h.array.size());
std::vector<int> v = std::vector<int>(0);
h = Heap(v);
CPPUNIT_ASSERT_EQUAL_MESSAGE("heap size violation for 0-length heap", 0U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("array length violation for 0-length heap", 0UL, (unsigned long) h.array.size());
h = Heap(v, false);
CPPUNIT_ASSERT_EQUAL_MESSAGE("heap size violation for 0-length heap", 0U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("array length violation for 0-length heap", 0UL, (unsigned long) h.array.size());
h = Heap(v, true);
CPPUNIT_ASSERT_EQUAL_MESSAGE("heap size violation for 0-length heap", 0U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("array length violation for 0-length heap", 0UL, (unsigned long) h.array.size());
}
void HeapConstructorsTest::testIncrementalConstructor() {
std::vector<int> v = {17};
Heap h = Heap(v, true);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap size of a new one-element heap should be 1", 1U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the one-element heap's array should be the value in the initializer vector", 17, h.array[0]);
std::vector<int> w = {18,14};
h = Heap(w, true);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap size of a new two-element heap should be 2", 2U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the larger value in the initializer vector", 18, h.array[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the smaller value in the initializer vector", 14, h.array[1]);
std::vector<int> x = {14,18};
h = Heap(x, true);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap size of a new two-element heap should be 2", 2U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the larger value in the initializer vector", 18, h.array[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the smaller value in the initializer vector", 14, h.array[1]);
std::vector<int> z = {10, 8, 14, 15, 12, 5, 6, 1};
h = Heap(z, true);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap from figure 4.16 of Ida's subject guide should have length 8", 8U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the heap in figure 4.16 of Ida's subject guide is wrong", 15, h.array[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The second element of the heap in figure 4.16 of Ida's subject guide is wrong", 14, h.array[1]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The third element of the heap in figure 4.16 of Ida's subject guide is wrong", 10, h.array[2]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The fourth element of the heap in figure 4.16 of Ida's subject guide is wrong", 8, h.array[3]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The fifth element of the heap in figure 4.16 of Ida's subject guide is wrong", 12, h.array[4]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The sixth element of the heap in figure 4.16 of Ida's subject guide is wrong", 5, h.array[5]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The seventh element of the heap in figure 4.16 of Ida's subject guide is wrong", 6, h.array[6]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The eighth element of the heap in figure 4.16 of Ida's subject guide is wrong", 1, h.array[7]);
}
void HeapConstructorsTest::testDirectConstructor() {
std::vector<int> v = {17};
Heap h = Heap(v, false);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap size of a new one-element heap should be 1", 1U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the one-element heap's array should be the value in the initializer vector", 17, h.array[0]);
std::vector<int> w = {18,14};
h = Heap(w, false);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap size of a new two-element heap should be 2", 2U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the larger value in the initializer vector", 18, h.array[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the larger value in the initializer vector", 14, h.array[1]);
std::vector<int> x = {14,18};
h = Heap(x, false);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap size of a new two-element heap should be 2", 2U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the larger value in the initializer vector", 18, h.array[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the two-element heap's array should be the larger value in the initializer vector", 14, h.array[1]);
std::vector<int> z = {10, 8, 14, 15, 12, 5, 6, 1};
h = Heap(z, false);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The heap directly constructed from figure 4.16 of Ida's subject guide should have length 8", 8U, h.heap_size);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The first element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 15, h.array[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The second element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 12, h.array[1]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The third element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 14, h.array[2]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The fourth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 8, h.array[3]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The fifth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 10, h.array[4]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The sixth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 5, h.array[5]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The seventh element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 6, h.array[6]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The eighth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", 1, h.array[7]);
}
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(HeapConstructorsTest, "Constructors");
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("Constructors");
#include <cmath>
#include <cppunit/extensions/HelperMacros.h>
#include "Heap.hpp"
class HeapIndexFunctionsTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(HeapIndexFunctionsTest);
CPPUNIT_TEST(testLeft);
CPPUNIT_TEST(testRight);
CPPUNIT_TEST(testParent);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {};
void tearDown() {};
void testLeft();
void testRight();
void testParent();
};
void HeapIndexFunctionsTest::testLeft() {
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the left child of the node at position 0?", 1U, left(0));
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the left child of the node at position 1?", 3U, left(1));
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the left child of the node at position 2?", 5U, left(2));
for (int i = 0; i < 100; i++) {
unsigned int x = rand() % 32768;
CPPUNIT_ASSERT_EQUAL_MESSAGE("a random choice of parent has an erroneous child!", 2*x+1, left(x));
}
}
void HeapIndexFunctionsTest::testRight() {
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the right child of the node at position 0?", 2U, right(0));
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the right child of the node at position 1?", 4U, right(1));
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the right child of the node at position 2?", 6U, right(2));
for (int i = 0; i < 100; i++) {
unsigned int x = rand() % 32768;
CPPUNIT_ASSERT_EQUAL_MESSAGE("a random choice of parent has an erroneous child!", 2*x+2, right(x));
}
}
void HeapIndexFunctionsTest::testParent() {
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the parent of the node at position 1?", 0U, parent(1));
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the parent of the node at position 2?", 0U, parent(2));
CPPUNIT_ASSERT_EQUAL_MESSAGE("what is the index of the parent of the node at position 3?", 1U, parent(3));
for (int i = 0; i < 100; i++) {
unsigned int x = 1 + rand() % 32767;
CPPUNIT_ASSERT_EQUAL_MESSAGE("a random choice of child has an erroneous parent!", (x-1)/2, parent(x));
}
}
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(HeapIndexFunctionsTest, "IndexFunctions");
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("IndexFunctions");
#include <cppunit/extensions/HelperMacros.h>
#include "Heap.hpp"
#include "HeapTest.hpp"
class HeapInsertTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(HeapInsertTest);
CPPUNIT_TEST(testInsertIntoZeroHeap);
CPPUNIT_TEST(testInsertIntoHeap);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {};
void tearDown() {};
void testInsertIntoZeroHeap();
void testInsertIntoHeap();
};
void HeapInsertTest::testInsertIntoZeroHeap() {
Heap h = Heap(0);
h.insert(1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The array should have a size of 1", 1UL, (unsigned long) h.array.size());
CPPUNIT_ASSERT_EQUAL_MESSAGE("The object inserted into a zero heap should be at the root", 1, h.array[0]);
}
void HeapInsertTest::testInsertIntoHeap() {
std::vector<int> v = {1, -3, 17, 4, 2};
Heap h = Heap(v);
CPPUNIT_ASSERT_MESSAGE("The heap constructed violates the heap property", check_heap(h));
CPPUNIT_ASSERT_EQUAL_MESSAGE("The array should have a size of 5", 5UL, (unsigned long) h.array.size());
CPPUNIT_ASSERT_EQUAL_MESSAGE("The root of the heap should be the maximum element", 17, h.array[0]);
h.insert(-5);
CPPUNIT_ASSERT_MESSAGE("After inserting -5, the heap is no longer a heap", check_heap(h));
CPPUNIT_ASSERT_EQUAL_MESSAGE("The array should have a size of 6", 6UL, (unsigned long) h.array.size());
CPPUNIT_ASSERT_EQUAL_MESSAGE("Inserting something smaller than all elements should put the new element at the end", -5, h.array[5]);
h.insert(200);
CPPUNIT_ASSERT_MESSAGE("After inserting 200, the heap is no longer a heap", check_heap(h));
CPPUNIT_ASSERT_EQUAL_MESSAGE("The array should have a size of 7", 7UL, (unsigned long) h.array.size());
CPPUNIT_ASSERT_EQUAL_MESSAGE("Inserting something larger than all elements should make it bubble up to the root", 200, h.array[0]);
}
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(HeapInsertTest, "Insert");
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("Insert");
#include <cppunit/extensions/HelperMacros.h>
#include "Heap.hpp"
#include "HeapTest.hpp"
class HeapMaximumTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(HeapMaximumTest);
CPPUNIT_TEST(testMaximum);
CPPUNIT_TEST(testExtractMax);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {};
void tearDown() {};
void testMaximum();
void testExtractMax();
};
void HeapMaximumTest::testMaximum() {
Heap h = Heap(0);
h.insert(1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The object inserted into a zero heap should be the maximum", 1, h.maximum());
std::vector<int> v = {1, 49, 3, 578, 2, -1000};
h = Heap(v);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The largest element in the initialization vector should be the maximum", 578, h.maximum());
}
void HeapMaximumTest::testExtractMax() {
std::vector<int> v = {17};
Heap h = Heap(v);
CPPUNIT_ASSERT_EQUAL_MESSAGE("The maximum extracted should be the single value", 17, h.extractMax());
CPPUNIT_ASSERT_EQUAL_MESSAGE("After removing the maximum from the heap, the heap should be empty", 0U, h.heap_size);
std::vector<int> w = {15,14,10,8,12,5,6,1}; // heap-ordered already
h = Heap(w);
CPPUNIT_ASSERT_MESSAGE("The heap formed from w is not a heap", check_heap(h));
CPPUNIT_ASSERT_EQUAL_MESSAGE("The maximum extracted is wrong", 15, h.extractMax());
CPPUNIT_ASSERT_MESSAGE("After extracting, the heap property is violated", check_heap(h));
CPPUNIT_ASSERT_EQUAL_MESSAGE("After extracting, the current maximum is wrong", 14, h.maximum());
}
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(HeapMaximumTest, "Maximum");
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("Maximum");
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include "Heap.hpp"
bool check_heap(Heap& h) {
if (h.array.size() < h.heap_size)
return false;
for (unsigned int i = 0; i < h.heap_size; i++) {
unsigned int l = left(i);
if(l < h.heap_size && h.array[l] > h.array[i])
return false;
unsigned int r = right(i);
if(r < h.heap_size && h.array[r] > h.array[i])
return false;
}
return true;
}
#ifndef HEAPTEST_HPP
#define HEAPTEST_HPP
#include "Heap.hpp"
bool check_heap(Heap&);
#endif
include ../../cpp.mk
HeapTest: Heap.o HeapTest.o HeapIndexFunctionsTest.o HeapConstructorsTest.o HeapInsertTest.o HeapMaximumTest.o ../../00/cpp/RunTests.o
test: HeapTest
./HeapTest
.PHONY: test
import java.util.ArrayList;
import java.util.List;
class Heap {
// public for JUnit testing purposes
public ArrayList<Integer> array;
public int heap_size;
public Heap(int size) {
}
public Heap(List<Integer> source) {
this(source, false);
}
public Heap(List<Integer> source, boolean incremental) {
}
public static int parent(int index) {
return 0;
}
public static int left(int index) {
return 0;
}
public static int right(int index) {
return 0;
}
public void maxHeapify(int i) {
}
public void buildMaxHeap() {
}
public void insert(Integer k) {
}
public Integer maximum() {
return 0;
}
public Integer extractMax() {
return 0;
}
}
import static java.util.Arrays.asList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
public class HeapConstructorsTest {
@Test
public void testMinimalConstructors() {
Heap h = new Heap(0);
assertEquals("heap size violation for 0-length heap", 0, h.heap_size);
assertEquals("array length violation for 0-length heap", 0, h.array.size());
List<Integer> v = asList();
h = new Heap(v);
assertEquals("heap size violation for 0-length heap", 0, h.heap_size);
assertEquals("array length violation for 0-length heap", 0, h.array.size());
h = new Heap(v, false);
assertEquals("heap size violation for 0-length heap", 0, h.heap_size);
assertEquals("array length violation for 0-length heap", 0, h.array.size());
h = new Heap(v, true);
assertEquals("heap size violation for 0-length heap", 0, h.heap_size);
assertEquals("array length violation for 0-length heap", 0, h.array.size());
}
@Test
public void testIncrementalConstructor() {
List<Integer> v = asList(17);
Heap h = new Heap(v, true);
assertEquals("The heap size of a new one-element heap should be 1", 1, h.heap_size);
assertEquals("The first element of the one-element heap's array should be the value in the initializer list", Integer.valueOf(17), h.array.get(0));
List<Integer> w = asList(18,14);
h = new Heap(w, true);
assertEquals("The heap size of a new two-element heap should be 2", 2, h.heap_size);
assertEquals("The first element of the one-element heap's array should be the larger value in the initializer vector", Integer.valueOf(18), h.array.get(0));
assertEquals("The first element of the one-element heap's array should be the smaller value in the initializer vector", Integer.valueOf(14), h.array.get(1));
List<Integer> x = asList(18,14);
h = new Heap(x, true);
assertEquals("The heap size of a new two-element heap should be 2", 2, h.heap_size);
assertEquals("The first element of the one-element heap's array should be the larger value in the initializer vector", Integer.valueOf(18), h.array.get(0));
assertEquals("The first element of the one-element heap's array should be the smaller value in the initializer vector", Integer.valueOf(14), h.array.get(1));
List<Integer> z = asList(10, 8, 14, 15, 12, 5, 6, 1);
h = new Heap(z, true);
assertEquals("The heap from figure 4.16 of Ida's subject guide should have length 8", 8, h.heap_size);
assertEquals("The first element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(15), h.array.get(0));
assertEquals("The second element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(14), h.array.get(1));
assertEquals("The third element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(10), h.array.get(2));
assertEquals("The fourth element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(8), h.array.get(3));
assertEquals("The fifth element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(12), h.array.get(4));
assertEquals("The sixth element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(5), h.array.get(5));
assertEquals("The seventh element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(6), h.array.get(6));
assertEquals("The eighth element of the heap in figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(1), h.array.get(7));
}
@Test
public void testDirectConstructor() {
List<Integer> v = asList(17);
Heap h = new Heap(v, false);
assertEquals("The heap size of a new one-element heap should be 1", 1, h.heap_size);
assertEquals("The first element of the one-element heap's array should be the value in the initializer list", Integer.valueOf(17), h.array.get(0));
List<Integer> w = asList(18,14);
h = new Heap(w, false);
assertEquals("The heap size of a new two-element heap should be 2", 2, h.heap_size);
assertEquals("The first element of the one-element heap's array should be the larger value in the initializer vector", Integer.valueOf(18), h.array.get(0));
assertEquals("The first element of the one-element heap's array should be the smaller value in the initializer vector", Integer.valueOf(14), h.array.get(1));
List<Integer> x = asList(18,14);
h = new Heap(x, false);
assertEquals("The heap size of a new two-element heap should be 2", 2, h.heap_size);
assertEquals("The first element of the one-element heap's array should be the larger value in the initializer vector", Integer.valueOf(18), h.array.get(0));
assertEquals("The first element of the one-element heap's array should be the smaller value in the initializer vector", Integer.valueOf(14), h.array.get(1));
List<Integer> z = asList(10, 8, 14, 15, 12, 5, 6, 1);
h = new Heap(z, false);
assertEquals("The heap directly constructed from figure 4.16 of Ida's subject guide should have length 8", 8, h.heap_size);
assertEquals("The first element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(15), h.array.get(0));
assertEquals("The second element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(12), h.array.get(1));
assertEquals("The third element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(14), h.array.get(2));
assertEquals("The fourth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(8), h.array.get(3));
assertEquals("The fifth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(10), h.array.get(4));
assertEquals("The sixth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(5), h.array.get(5));
assertEquals("The seventh element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(6), h.array.get(6));
assertEquals("The eighth element of the heap directly constructed from the contents of figure 4.16 of Ida's subject guide is wrong", Integer.valueOf(1), h.array.get(7));
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
public class HeapIndexFunctionsTest {
@Test
public void testLeft() {
assertEquals("what is the index of the left child of the node at position 0?", 1, Heap.left(0));
assertEquals("what is the index of the left child of the node at position 1?", 3, Heap.left(1));
assertEquals("what is the index of the left child of the node at position 2?", 5, Heap.left(2));
Random r = new Random();
for (int i = 0; i < 100; i++) {
int x = r.nextInt(32768);
assertEquals("a random choice of parent has an erroneous child!", 2*x+1, Heap.left(x));
}
}
@Test
public void testRight() {
assertEquals("what is the index of the right child of the node at position 0?", 2, Heap.right(0));
assertEquals("what is the index of the right child of the node at position 1?", 4, Heap.right(1));
assertEquals("what is the index of the right child of the node at position 2?", 6, Heap.right(2));
Random r = new Random();
for (int i = 0; i < 100; i++) {
int x = r.nextInt(32768);
assertEquals("a random choice of parent has an erroneous child!", 2*x+2, Heap.right(x));
}
}
@Test
public void testParent() {
assertEquals("what is the index of the parent of the node at position 1?", 0, Heap.parent(1));
assertEquals("what is the index of the parent of the node at position 2?", 0, Heap.parent(2));
assertEquals("what is the index of the parent of the node at position 3?", 1, Heap.parent(3));
Random r = new Random();
for (int i = 0; i < 100; i++) {
int x = 1 + r.nextInt(32767);
assertEquals("a random choice of child has an erroneous parent!", (x-1)/2, Heap.parent(x));
}
}
}
import static java.util.Arrays.asList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotEquals;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;