本文整理汇总了C#中Heap.insert方法的典型用法代码示例。如果您正苦于以下问题:C# Heap.insert方法的具体用法?C# Heap.insert怎么用?C# Heap.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Heap
的用法示例。
在下文中一共展示了Heap.insert方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: testHeap
private String testHeap(int lengthA, int lenghtB, int rangeA, int rangeB, int size)
{
int insertArrayLength = lengthA;
int removeAndInsertArrayLength = lenghtB;
int insertRange = rangeA;
int removeAndInsertRange = rangeB;
Element[] elementM = new Element[insertArrayLength];
Element[] elementN = new Element[removeAndInsertArrayLength];
Random rnd = new Random();
int tmp = 0;
while (tmp < insertArrayLength)
{
elementM[tmp] = new Element(rnd.Next(1, insertRange + 1));
tmp++;
}
tmp = 0;
while (tmp < removeAndInsertArrayLength)
{
elementN[tmp] = new Element(rnd.Next(1, removeAndInsertRange + 1));
tmp++;
}
Stopwatch watch = new Stopwatch();
watch.Start(); //czy tutaj włączyć czy przy inicjalizacji randomów?
Heap heap = new Heap(size);
for (tmp = 0; tmp < insertArrayLength; tmp++)
{
heap.insert(elementM[tmp]);
}
tmp = 0;
while (tmp < removeAndInsertArrayLength)
{
heap.deleteMin();
heap.insert(elementN[tmp]);
tmp++;
}
watch.Stop();
return watch.Elapsed.ToString(); //watch.ElapsedMilliseconds.ToString();
}
示例2: findPath
//uses A* to find path
private void findPath(GridNode start, GridNode end)
{
Stopwatch watch = new Stopwatch();
watch.Start();
Heap<GridNode> heap = new Heap<GridNode>(gameGrid.gridHeight*gameGrid.gridLength);
Dictionary<GridNode,int> costSoFar = new Dictionary<GridNode,int>();
Dictionary<GridNode,GridNode> cameFrom = new Dictionary<GridNode,GridNode>();
HashSet<GridNode> closed = new HashSet<GridNode>();
heap.insert (start);
costSoFar.Add(start, 0);
while(heap.Count > 0){
GridNode current = heap.extract();
closed.Add (current);
if(current.Equals(end)){
retraceRoute(start,current,cameFrom);
heap.Clear ();
watch.Stop();
print (watch.ElapsedMilliseconds);
}else{
List<GridNode> list = gameGrid.getNeighbours(current);
foreach(GridNode node in list){
if(!node.isBlock && !closed.Contains(node)){
int cost = costSoFar[current] + gameGrid.getDistanceBetween(current,node);
if(!costSoFar.ContainsKey (node) || cost < costSoFar[node]){
costSoFar.Remove (node);
costSoFar.Add (node,cost);
node.score = cost + gameGrid.getHeuristic(node, end);
heap.insert (node);
cameFrom.Remove (node);
cameFrom.Add (node, current);
}
}
}
}
}
explored = new HashSet<GridNode>(closed);
}
示例3: MST_Prim
// Prim's algorithm to compute MST
// Code most of this yourself
int[] MST_Prim( int s)
{
int v;
int wgt = 0;
int wgt_sum = 0;
int[] dist, parent, hPos;
dist = new int[V + 1];
parent = new int[V + 1];
hPos = new int[V + 1];
Node t;
for (v = 0; v <= V; v++)
{
dist[v] = int.MaxValue;
parent[v] = 0;
hPos[v] = 0;
}
Heap pq = new Heap(V, dist, hPos);
pq.insert(s);
dist[s] = 0;
while ( ! pq.isEmpty())
{
v = pq.remove();
Console.WriteLine("Adding to MST edge {0}--({1})--{2}", toChar(parent[v]), dist[v], toChar(v));
if (v != s)
{
wgt_sum += dist[v];
}
dist[v] = 0;
for (t= adj[v]; t.data != 0; t = t.next)
{
wgt = t.wgt;
if (t.wgt < dist[t.data])
{
dist[t.data] = t.wgt;
parent[t.data] = v;
if (hPos[t.data] == 0)
{
pq.insert(t.data);
}
else
{
pq.siftUp(hPos[t.data]);
}
}
}
}
Console.Write("\n\nWeight of MST = {0}\n",wgt_sum);
return parent;
}
示例4: Main
static void Main(string[] args)
{
Heap h = new Heap();
Random r = new Random();
int i, x;
for (i = 0; i < 10; ++i)
{
x = r.Next(99);
Console.WriteLine("\nInserting {0} ", x);
h.insert(x);
h.display();
}
x = h.remove();
Console.WriteLine("\nRemoving {0} ", x);
h.display();
Console.ReadKey();
}
示例5: Main
static void Main(string[] args)
{
Heap h = new Heap();
Random r = new Random();
int i, x;
for (i = 0; i < 10; ++i)
{
x = r.Next(99);
Console.WriteLine("\nInserting {0} ", x);
h.insert(x);
h.display();
}
Console.WriteLine("\nConverted");
h.convert();
h.display();
Console.WriteLine("\nSorting");
h.heapSort();
h.display();
Console.WriteLine("\nSorting");
h.heapSort();
h.display();
Console.WriteLine("\nSorting");
h.heapSort();
h.display();
Console.ReadKey();
}
示例6: MST_Prim
// Prim's algorithm to compute MST
// Code most of this yourself
int[] MST_Prim(int s)
{
int v;
int wgt_sum = 0;
int[] dist, parent, hPos;
Node t;
//the distance from node to node
dist = new int[V + 1];
//the parent node
parent = new int[V + 1];
//current heap position
hPos = new int[V + 1];
// initialising parent and position to zero, and dist to the max value
for (v = 1; v <= V; v++)
{
dist[v] = int.MaxValue;
parent[v] = 0;
hPos[v] = 0;
}
Heap heap = new Heap(V + 1, dist, hPos);
heap.insert(s);
dist[s] = 0;
while (!heap.isEmpty())
{
v = heap.remove();
//calculates the sum of the weights
wgt_sum += dist[v];
dist[v] = -dist[v];
Console.Write("\nAdding edge {0}--({1})--{2}", toChar(parent[v]), dist[v], toChar(v));
for (t = adj[v]; t != z; t = t.next)
{
if (t.wgt < dist[t.vert])
{
dist[t.vert] = t.wgt;
parent[t.vert] = v;
//If the vertex is empty, insert next vertex
if (hPos[t.vert] == 0)
{
heap.insert(t.vert);
}
else //Else call sift up
{
heap.siftUp(hPos[t.vert]);
}
}
}
}
Console.Write("\n\nWeight = {0}\n", wgt_sum);
return parent;
}