public void Should_Increase_Key() { var A_correct1 = new List <int> { 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 }; var A_correct2 = new List <int> { 16, 15, 10, 14, 7, 9, 3, 2, 8, 1 }; // var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapHighToLow = new Heap(A, HeapType.HighToLow); _heapHighToLow.Build(); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct1[i]); } _heapHighToLow.HeapIncreaseKey(8, 15); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct2[i]); } }
bool Test_5() { List <HeapTestItem> list = new List <HeapTestItem>(); Heap <HeapTestItem> queue = new Heap <HeapTestItem>(Heap <HeapTestItem> .CheckPriorityMethod.CPM_LESS); for (int i = 0; i < MAX_ITEM_NUM; ++i) { HeapTestItem item = CreateTestItem(random_1_to_10[i]); queue.Enqueue(item); list.Add(item); } for (int i = 0; i < MAX_ITEM_NUM; ++i) { list[i].value = i + 1; } queue.Build(); HeapTestItem must_less_priority_than_this = CreateTestItem(0); if (!queue.Check(must_less_priority_than_this)) { Print("Test_5, 1"); return(false); } for (int i = 0; i < MAX_ITEM_NUM; ++i) { HeapTestItem item = queue.Dequeue(); if (item.value != (i + 1)) { Print("Test_5, 2"); return(false); } } return(true); }
public void Should_Insert_Key_In_Correct_Position() { var A_correct1 = new List <int> { 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 }; var A_correct2 = new List <int> { 16, 15, 10, 8, 14, 9, 3, 2, 4, 1, 7 }; // var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapHighToLow = new Heap(A, HeapType.HighToLow); _heapHighToLow.Build(); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct1[i]); } _heapHighToLow.Insert(15); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct2[i]); } }
public void Should_Insert_Key_In_Correct_Position() { var A_correct1 = new List <int> { 1, 5, 3, 6, 7, 9, 10, 14, 8, 16 }; var A_correct2 = new List <int> { 1, 2, 3, 6, 5, 9, 10, 14, 8, 16, 7 }; // var A = new List <int> { 6, 1, 3, 5, 16, 9, 10, 14, 8, 7 }; _heapLowToHigh = new Heap(A, HeapType.LowToHigh); _heapLowToHigh.Build(); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct1[i]); } _heapLowToHigh.Insert(2); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct2[i]); } }
public void Should_Return_Correct_Parent_Index() { //(0 -->(1, 2) //(1 --> (3, 4) //(2 --> (5, 6) //(3 --> (7, 8)) //(4 --> (9, )) var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapHighToLow = new Heap(A, HeapType.HighToLow); _heapHighToLow.Build(); //index 0 _heapHighToLow.Parent(1).Should().Be(0); _heapHighToLow.Parent(2).Should().Be(0); //index 1 _heapHighToLow.Parent(3).Should().Be(1); _heapHighToLow.Parent(4).Should().Be(1); //index 2 _heapHighToLow.Parent(5).Should().Be(2); _heapHighToLow.Parent(6).Should().Be(2); //index 3 _heapHighToLow.Parent(7).Should().Be(3); _heapHighToLow.Parent(8).Should().Be(3); //index 4 _heapHighToLow.Parent(9).Should().Be(4); _heapHighToLow.Parent(10).Should().Be(4); }
public void Should_Descrese_Key() { var A_correct1 = new List <int> { 1, 5, 3, 6, 7, 9, 10, 14, 8, 16 }; var A_correct2 = new List <int> { 1, 4, 3, 5, 7, 9, 10, 14, 6, 16 }; // var A = new List <int> { 6, 1, 3, 5, 16, 9, 10, 14, 8, 7 }; _heapLowToHigh = new Heap(A, HeapType.LowToHigh); _heapLowToHigh.Build(); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct1[i]); } _heapLowToHigh.HeapDecreseKey(8, 4); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct2[i]); } }
public void Should_Return_ExtractMin() { var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapHighToLow = new Heap(A, HeapType.HighToLow); _heapHighToLow.Build(); _heapHighToLow.ExtractMin().Should().Be(1); }
public void Should_Return_Max() { var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapHighToLow = new Heap(A, HeapType.HighToLow); _heapHighToLow.Build(); _heapHighToLow.Max.Should().Be(16); }
public void Should_Return_ExtractMin() { var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapLowToHigh = new Heap(A, HeapType.LowToHigh); _heapLowToHigh.Build(); _heapLowToHigh.ExtractMin().Should().Be(1); }
public void Should_Return_Max() { var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapLowToHigh = new Heap(A, HeapType.LowToHigh); _heapLowToHigh.Build(); _heapLowToHigh.Max.Should().Be(16); }
public void Should_Build_Max_Heap_1() { var A_correct = new List <int> { 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 }; var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapHighToLow = new Heap(A, HeapType.HighToLow); _heapHighToLow.Build(); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct[i]); } }
public void Should_Build_Max_Heap_2() { var A_correct = new List <int> { 12, 10, 5, 8, 7, 4, 3, 1, 2, 6 }; var A = new List <int> { 2, 8, 4, 10, 6, 5, 3, 1, 12, 7 }; _heapHighToLow = new Heap(A, HeapType.HighToLow); _heapHighToLow.Build(); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct[i]); } }
public void Should_Build_Max_Heap_1() { var A_correct = new List <int> { 1, 2, 3, 4, 7, 9, 10, 14, 8, 16 }; var A = new List <int> { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; _heapLowToHigh = new Heap(A, HeapType.LowToHigh); _heapLowToHigh.Build(); for (var i = 0; i < A.Count; ++i) { A[i].Should().Be(A_correct[i]); } }