-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add binary heap implementation with support for siftdown optimization
- Loading branch information
1 parent
fe7e10c
commit 1670553
Showing
9 changed files
with
790 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using Xunit; | ||
using FsCheck.Xunit; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace PriorityQueue.Tests | ||
{ | ||
public class PriorityQueue_BinaryTests | ||
{ | ||
[Fact] | ||
public static void Simple_Priority_Queue() | ||
{ | ||
var pq = new PriorityQueue_Binary<string, int>(); | ||
|
||
pq.Enqueue("John", 1940); | ||
pq.Enqueue("Paul", 1942); | ||
pq.Enqueue("George", 1943); | ||
pq.Enqueue("Ringo", 1941); | ||
|
||
Assert.Equal("John", pq.Dequeue()); | ||
Assert.Equal("Ringo", pq.Dequeue()); | ||
Assert.Equal("Paul", pq.Dequeue()); | ||
Assert.Equal("George", pq.Dequeue()); | ||
} | ||
|
||
[Fact] | ||
public static void Simple_Priority_Queue_Heapify() | ||
{ | ||
var pq = new PriorityQueue_Binary<string, int>(new (string, int)[] { ("John", 1940), ("Paul", 1942), ("George", 1943), ("Ringo", 1941) }); | ||
|
||
Assert.Equal("John", pq.Dequeue()); | ||
Assert.Equal("Ringo", pq.Dequeue()); | ||
Assert.Equal("Paul", pq.Dequeue()); | ||
Assert.Equal("George", pq.Dequeue()); | ||
} | ||
|
||
[Fact] | ||
public static void Simple_Priority_Queue_Enumeration() | ||
{ | ||
var pq = new PriorityQueue_Binary<string, int>(new (string, int)[] { ("John", 1940), ("Paul", 1942), ("George", 1943), ("Ringo", 1941) }); | ||
|
||
(string, int)[] expected = new[] { ("John", 1940), ("Ringo", 1941), ("George", 1943), ("Paul", 1942) }; | ||
Assert.Equal(expected, pq.UnorderedItems.ToArray()); | ||
} | ||
|
||
[Property(MaxTest = 10_000)] | ||
public static void HeapSort_Should_Work(string[] inputs) | ||
{ | ||
string[] expected = inputs.OrderBy(inp => inp).ToArray(); | ||
string[] actual = HeapSort(inputs).ToArray(); | ||
Assert.Equal(expected, actual); | ||
|
||
static IEnumerable<string> HeapSort(string[] inputs) | ||
{ | ||
var pq = new PriorityQueue_Binary<string, string>(); | ||
ValidateState(pq); | ||
|
||
foreach (string input in inputs) | ||
{ | ||
pq.Enqueue(input, input); | ||
ValidateState(pq); | ||
} | ||
|
||
Assert.Equal(inputs.Length, pq.Count); | ||
|
||
while (pq.Count > 0) | ||
{ | ||
yield return pq.Dequeue(); | ||
ValidateState(pq); | ||
} | ||
} | ||
} | ||
|
||
[Property(MaxTest = 10_000)] | ||
public static void HeapSort_Ctor_Should_Work(string[] inputs) | ||
{ | ||
string[] expected = inputs.OrderBy(inp => inp).ToArray(); | ||
string[] actual = HeapSort(inputs).ToArray(); | ||
Assert.Equal(expected, actual); | ||
|
||
static IEnumerable<string> HeapSort(string[] inputs) | ||
{ | ||
var pq = new PriorityQueue_Binary<string, string>(inputs.Select(x => (x, x))); | ||
|
||
ValidateState(pq); | ||
Assert.Equal(inputs.Length, pq.Count); | ||
|
||
while (pq.Count > 0) | ||
{ | ||
yield return pq.Dequeue(); | ||
ValidateState(pq); | ||
} | ||
} | ||
} | ||
|
||
private static void ValidateState<TElement, TPriority>(PriorityQueue_Binary<TElement, TPriority> pq) | ||
{ | ||
#if DEBUG | ||
pq.ValidateInternalState(); | ||
#endif | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.