Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to pass all tests in this Tester.cs:using System; using System.Collections.Generic; namespace _ 8 . _ 1 C _ Heaps { class Tester {

I need to pass all tests in this Tester.cs:using System;
using System.Collections.Generic;
namespace _8._1C_Heaps
{
class Tester
{
private class IntAscendingComparer : IComparer
{
public int Compare(int A, int B)
{
return Math.Sign(A - B);
}
}
private class IntDescendingComparer : IComparer
{
public int Compare(int A, int B)
{
return -1* Math.Sign(A - B);
}
}
static void Main(string[] args)
{
//------------------------ test instance (begin)
string[] names = new string[]{ "Kelly", "Cindy", "John", "Andrew", "Richard", "Michael", "Guy", "Elicia", "Tom", "Iman", "Simon", "Vicky", "Kevin", "David" };
int[] IDs = new int[]{1,6,5,7,8,3,10,4,2,9,14,12,11,13};
int[] certificateAdd = new int[]{1,2,3,4,5,3,7,2,2,10,11,12,13,14};
int[] certificateDelete = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int[] certificateMinHeapBuild = new int[]{1,8,6,9,5,3,7,4,2,10,11,12,13,14};
int[] certificateMaxHeapBuild = new int[]{11,10,14,4,5,12,7,8,9,2,1,6,13,3};
//------------------------ test instance (end)
Heap minHeap = null;
Heap maxHeap = null;
IHeapifyable[] nodes = null;
string result ="";
// test 1
try
{
Console.WriteLine("
Test A: Create a min-heap by calling 'minHeap = new Heap(new IntAscendingComparer());'");
minHeap = new Heap(new IntAscendingComparer());
Console.WriteLine(" :: SUCCESS: min-heap's state "+ minHeap.ToString());
result = result +"A";
}
catch (Exception exception)
{
try { Console.WriteLine(" :: FAIL: min-heap's state "+ minHeap.ToString()); } catch {};
Console.WriteLine(exception.ToString());
result = result +"-";
}
// test 2
try
{
Console.WriteLine("
Test B: Run a sequence of operations: ");
for (int i =0; i < Math.Min(names.Length, IDs.Length); i++)
{
Console.WriteLine("
Insert a node with name {0}(data) and ID {1}(key).", names[i], IDs[i]);
IHeapifyable node = minHeap.Insert(IDs[i], names[i]);
if (!(node.Position == certificateAdd[i] && minHeap.Count == i +1)) throw new Exception("The min-heap has a wrong structure");
Console.WriteLine(" :: SUCCESS: min-heap's state "+ minHeap.ToString());
}
result = result +"B";
}
catch (Exception exception)
{
try { Console.WriteLine(" :: FAIL: min-heap's state "+ minHeap.ToString()); } catch {};
Console.WriteLine(exception.ToString());
result = result +"-";
}
// test 3
try
{
Console.WriteLine("
Test C: Run a sequence of operations: ");
for (int i =0; i < certificateDelete.Length; i++)
{
Console.WriteLine("
Delete the minimum element from the min-heap.");
IHeapifyable node = minHeap.Delete();
if (node.Key != certificateDelete[i]) throw new Exception("The extracted node has a wrong key");
if (minHeap.Count != certificateDelete.Length - i -1) throw new Exception("The heap has a wrong number of elements");
if (certificateDelete.Length - i -1>0)
{
if ((minHeap.Min().Key != certificateDelete[i +1]) && (minHeap.Min().Position !=1)) throw new Exception("The min-heap has a wrong structure");
}
Console.WriteLine(" :: SUCCESS: min-heap's state "+ minHeap.ToString());
}
result = result +"C";
}
catch (Exception exception)
{
try { Console.WriteLine(" :: FAIL: min-heap's state "+ minHeap.ToString()); } catch {};
Console.WriteLine(exception.ToString());
result = result +"-";
}
// test 4
try
{
Console.WriteLine("
Test D: Delete the minimum element from the min-heap.");
IHeapifyable node = minHeap.Delete();
Console.WriteLine("Last operation is invalid and must throw

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions