public void MaxHeapInitialises() { int testOrder = 2; var maxDHeap = new MaxDHeap <int>(testOrder); Assert.IsNotNull(maxDHeap); }
internal static IEnumerable <T> MergeBy <T, TKey>(IEnumerable <IEnumerable <T> > lists, Func <T, TKey> keySelector, IComparer <TKey> comparer, bool descending) { var initialItems = lists .Select(l => l.GetEnumerator()) .Where(i => i.MoveNext()); IComparer <T> keyComparer = comparer != null ? new KeySelectorComparer <T, TKey>(keySelector, comparer) : new KeySelectorComparer <T, TKey>(keySelector); var enumerableComparer = new EnumeratorComparer <T>(keyComparer); DHeap <IEnumerator <T> > heap; if (descending) { heap = new MaxDHeap <IEnumerator <T> >(2, initialItems, enumerableComparer); } else { heap = new MinDHeap <IEnumerator <T> >(2, initialItems, enumerableComparer); } while (!heap.IsEmpty) { var i = heap.RemoveRoot(); yield return(i.Current); if (i.MoveNext()) { heap.Insert(i); } } }
public void MaxHeapInitialisesAndInsertsSpecifiedCollection() { int testOrder = 2; IList <int> testCollection = new List <int> { 50, 51, 38, 37, 23, 11, 5, 3 }; var maxDHeap = new MaxDHeap <int>(testOrder, testCollection.Randomize()); Assert.AreEqual(testCollection.Count, maxDHeap.Count); }
public void MaxHeapRemoveRootReturnsLargestValue() { int testOrder = 2; IList <int> testCollection = new List <int> { 50, 51, 38, 37, 23, 11, 5, 3 }; var maxDHeap = new MaxDHeap <int>(testOrder, testCollection.Randomize()); int root = maxDHeap.RemoveRoot(); Assert.AreEqual(51, root); }
public void MaxHeapMovesNextLargestValueToRootAfterRemoveRoot() { int testOrder = 2; IList <int> testCollection = new List <int> { 50, 51, 38, 37, 23, 11, 5, 3 }; var maxDHeap = new MaxDHeap <int>(testOrder, testCollection.Randomize()); maxDHeap.RemoveRoot(); int newRoot = maxDHeap.Peek(); Assert.AreEqual(50, newRoot); }
public void MaxHeapDoesNotInsertItemAsRootIfItemIsSmallerThanOriginalRoot() { int testOrder = 2; IList <int> testCollection = new List <int> { 50, 51, 38, 37, 23, 11, 5, 3 }; var maxDHeap = new MaxDHeap <int>(testOrder, testCollection.Randomize()); int originalRoot = maxDHeap.Peek(); maxDHeap.Insert(3); int newRoot = maxDHeap.Peek(); Assert.AreEqual(51, originalRoot); Assert.AreEqual(51, newRoot); }
public void MaxHeapSupportsConsumingEnumerable() { int testOrder = 2; IList <int> testCollection = new List <int> { 50, 51, 38, 37, 23, 11, 5, 3 }; var maxDHeap = new MaxDHeap <int>(testOrder, testCollection.Randomize()); var consumingEnumerator = maxDHeap.GetConsumingEnumerable().GetEnumerator(); Assert.DoesNotThrow(() => { while (consumingEnumerator.MoveNext()) { //ignore empty block } }); }
public void MaxHeapConsumingEnumerableRemovesAllElements() { const int expectedCount = 0; int testOrder = 2; IList <int> testCollection = new List <int> { 50, 51, 38, 37, 23, 11, 5, 3 }; var maxDHeap = new MaxDHeap <int>(testOrder, testCollection.Randomize()); var consumingEnumerator = maxDHeap.GetConsumingEnumerable().GetEnumerator(); while (consumingEnumerator.MoveNext()) { //ignore empty block } Assert.AreEqual(expectedCount, maxDHeap.Count); }
public void MaxHeapInsertsItemInCollectionAsRootIfItemIsLargerThanOriginalRoot() { int testOrder = 2; IList <int> testCollection = new List <int> { 50, 51, 38, 37, 23, 11, 5, 3 }; IList <int> newCollection = new List <int> { 34, 36, 38, 40, 42, 44, 46, 48, 52 }; var maxDHeap = new MaxDHeap <int>(testOrder, testCollection.Randomize()); int originalRoot = maxDHeap.Peek(); maxDHeap.InsertRange(newCollection.Randomize()); int newRoot = maxDHeap.Peek(); Assert.AreEqual(51, originalRoot); Assert.AreEqual(52, newRoot); }