public void MinHeapWithNumbers() { var heap = new BinaryMinHeap <int>(); foreach (var number in Enumerable.Range(1, 10).Reverse()) { heap.Push(number); } var minimumNumber = heap.Pop(); Assert.That(minimumNumber, Is.EqualTo(1)); }
public void MinHeapWithNodes() { var heap = new BinaryMinHeap <Node>(new NodeComparer()); foreach (var number in Enumerable.Range(1, 10).Reverse()) { heap.Push(new Node { PredictedTotalCost = number }); } var minimumNumber = heap.Pop().PredictedTotalCost; Assert.That(minimumNumber, Is.EqualTo(1)); }
private List <RiverNode> GenerateDownslopes(MapArray <int> bitmask) { var simplex = new Simplex2D(52562); MapArray <float> weights = new MapArray <float>(1000.0f); var rivernet = new List <RiverNode>(); var node_lookup = new Dictionary <GBTCorner, RiverNode>(); var queue = new BinaryMinHeap <QueueElement <RiverNode> >(); // Assign weights to each land vertex and add coast vertices to the queue. foreach (KeyValuePair <GBTCorner, int> kvp in bitmask.GetCornerEnumerator()) { if ((kvp.Value & 1) > 0) { weights[kvp.Key] = simplex.GetFractalNoise(4.0f * kvp.Key.position / Radius); if ((kvp.Value & 2) > 0) { RiverNode node = new RiverNode(kvp.Key); queue.Push(new QueueElement <RiverNode>(weights[kvp.Key], node)); rivernet.Add(node); node_lookup[kvp.Key] = node; } } } while (queue.Count > 0) { RiverNode node = queue.Pop().value; GBTCorner lowest = new GBTCorner(-1); float lowest_weight = 999.0f; // Find the neighboring land node with the lowest weight which has not already // been added to the network. foreach (GBTCorner adjacent in node.Vertex.GetAdjacent()) { if (CheckValidAdjacent(node, adjacent, bitmask, node_lookup) && weights[adjacent] < lowest_weight) { lowest_weight = weights[adjacent]; lowest = adjacent; } } // Add the lowest node to the network, and push it and the into the queue. if (lowest.isValid()) { var new_node = new RiverNode(lowest); new_node.Downslope = node; if (node.Left == null) { node.Left = new_node; // If the node hasn't been filled, add it to the queue again, but with a lower weight. weights[node.Vertex] += 0.05f; queue.Push(new QueueElement <RiverNode>(weights[node.Vertex], node)); } else if (node.Right == null) { node.Right = new_node; } node_lookup[lowest] = new_node; queue.Push(new QueueElement <RiverNode>(weights[lowest], new_node)); } } return(rivernet); }