本文整理汇总了C#中Heap.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# Heap.Insert方法的具体用法?C# Heap.Insert怎么用?C# Heap.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Heap
的用法示例。
在下文中一共展示了Heap.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestHeapSort
public void TestHeapSort()
{
Heap<int> h = new Heap<int>();
h.Insert(500);
h.Insert(100);
h.Insert(200);
h.Insert(50);
h.Insert(1);
h.Insert(420);
h.Insert(3);
h.Insert(250);
h.Insert(5);
h.Insert(499);
int[] sortedItems = h.HeapSort();
Assert.AreEqual(1, sortedItems[0]);
Assert.AreEqual(3, sortedItems[1]);
Assert.AreEqual(5, sortedItems[2]);
Assert.AreEqual(50, sortedItems[3]);
Assert.AreEqual(100, sortedItems[4]);
Assert.AreEqual(200, sortedItems[5]);
Assert.AreEqual(250, sortedItems[6]);
Assert.AreEqual(420, sortedItems[7]);
Assert.AreEqual(499, sortedItems[8]);
Assert.AreEqual(500, sortedItems[9]);
}
示例2: TestMethod1
public void TestMethod1()
{
int[] i = new int[] {0, 1, 2, 3, 4, 5};
Heap h = new Heap(6);
h.Insert(23);
h.Insert(13);
h.Insert(12);
h.Insert(10);
}
示例3: TestExtractMinimum
public void TestExtractMinimum()
{
Heap<int> h = new Heap<int>();
h.Insert(500);
h.Insert(100);
h.Insert(200);
h.Insert(50);
Assert.AreEqual(50, h.ExtractMinimum());
Assert.AreEqual(100, h.ExtractMinimum());
Assert.AreEqual(200, h.ExtractMinimum());
Assert.AreEqual(500, h.ExtractMinimum());
}
示例4: Insert_MultipleBubbleToRoot
public void Insert_MultipleBubbleToRoot()
{
Heap<int> heap = new Heap<int>(HeapType.Max)
{
List = new List<int>()
{
150, // root
50, // left child
100, // right child
45, // left child of 50
40, // right child of 50
95, // left child of 100
90, // right child of 100
}
};
heap.Insert(200);
Assert.AreEqual<int>(200, heap.List[0]);
Assert.AreEqual<int>(150, heap.List[1]);
Assert.AreEqual<int>(100, heap.List[2]);
Assert.AreEqual<int>(50, heap.List[3]);
Assert.AreEqual<int>(40, heap.List[4]);
Assert.AreEqual<int>(95, heap.List[5]);
Assert.AreEqual<int>(90, heap.List[6]);
Assert.AreEqual<int>(45, heap.List[7]);
}
示例5: Main
static void Main(string[] args)
{
StreamReader reader = new StreamReader(@"C:\Users\Darsh\Documents\Visual Studio 2013\Projects\ProjectThree\ProjectThree\input.txt");
Student student;
Heap<Student> theHeap = new Heap<Student>();
string sr = reader.ReadLine();//raid
Student[] records = new Student[sr.Length];
while (sr != null)// while there is still text
{
string[] delimiter = { ",", " " };
string[] info = sr.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
student = new Student(Convert.ToInt32(info[0]), Convert.ToDouble(info[1]));
theHeap.Insert(student);//insert all data into the Heap
sr = reader.ReadLine();
}
Console.WriteLine("Empty? {0}",theHeap.IsEmpty()); //false
Console.WriteLine("Root: {0}",theHeap.GetRoot());
theHeap.RemoveRoot();
theHeap.Print(); //Prints out student id and gpa as min heap
Console.WriteLine();
Console.WriteLine("HEAPSORT!!");
theHeap.HeapSort();//prints out the heap sort going from high to low
Console.ReadKey();
}
示例6: InitHeap
public void InitHeap()
{
_heap = new MinHeap<int>();
for (int i = 100; i >0; i--)
{
_heap.Insert(i);
}
}
示例7: Insert_SingleBubbleToRoot
public void Insert_SingleBubbleToRoot()
{
Heap<int> heap = new Heap<int>(HeapType.Max)
{
List = new List<int>()
{
50
}
};
heap.Insert(100);
Assert.AreEqual<int>(100, heap.List[0]);
Assert.AreEqual<int>(50, heap.List[1]);
}
示例8: DoHeap
static void DoHeap()
{
Heap<Person> heap = new Heap<Person>();
//insert some people with dependents in no particular order
heap.Insert(new Person(4));
heap.Insert(new Person(1));
heap.Insert(new Person(8));
heap.Insert(new Person(0));
heap.Insert(new Person(13)); //this will be removed first
heap.Insert(new Person(2));
heap.Remove(); //person with 13 dependents is no longer in the heap
Console.WriteLine(heap.ToString());
}
示例9: Insert_SingleBubbleToInnerNode
public void Insert_SingleBubbleToInnerNode()
{
Heap<int> heap = new Heap<int>(HeapType.Max)
{
List = new List<int>()
{
150,
50,
100
}
};
heap.Insert(75);
Assert.AreEqual<int>(150, heap.List[0]);
Assert.AreEqual<int>(75, heap.List[1]);
Assert.AreEqual<int>(100, heap.List[2]);
Assert.AreEqual<int>(50, heap.List[3]);
}
示例10: NthUglyNumber
// Q264
public int NthUglyNumber(int n)
{
int res = 0;
Heap minHeap = new Heap(n * 3);
minHeap.Insert(1);
for (int i = 0; i < n;i++ )
{
res = minHeap.DeleteMin();
while (res == minHeap.PeekMin())
{
minHeap.DeleteMin();
}
int tmp;
tmp = res * 2;
if (tmp > 0)
{
minHeap.Insert(tmp);
tmp = res * 3;
if (tmp > 0)
{
minHeap.Insert(tmp);
tmp = res * 5;
if (tmp > 0)
{
minHeap.Insert(tmp);
}
}
}
}
return res;
}
示例11: AStar
// Range: -1 Access: 0 Flags: ( 0, 4, 255 )
public static dynamic AStar( dynamic start = null, dynamic end = null, Mob_Living_SimpleAnimal atom = null, System.Reflection.MethodInfo dist = null, dynamic maxnodes = null, dynamic maxnodedepth = null, dynamic mintargetdist = null, System.Reflection.MethodInfo adjacent = null, Ent_Item_Weapon_Card_Id id = null, dynamic exclude = null, bool? simulated_only = null ) {
Heap open = null;
ByTable closed = null;
ByTable path = null;
dynamic cur = null;
bool? closeenough = null;
dynamic L = null;
dynamic T = null;
dynamic newg = null;
PathNode PN = null;
dynamic T2 = null;
double? i = null;
if ( maxnodedepth == null ) {
maxnodedepth = 30;
}
if ( adjacent == null ) {
adjacent = typeof(Tile).GetMethod( "reachableAdjacentTurfs" );
}
if ( id == null ) {
id = null;
}
if ( exclude == null ) {
exclude = null;
}
if ( simulated_only == null ) {
simulated_only = true;
}
if ( Lang13.Bool( maxnodes ) ) {
if ( Lang13.Double( Lang13.call( Lang13.bindf( start, dist ), end ) ) > Lang13.Double( maxnodes ) ) {
return 0;
}
maxnodedepth = maxnodes;
}
open = new Heap( typeof(GlobalFuncs).GetMethod( "HeapPathWeightCompare" ) );
closed = new ByTable();
path = null;
start = GlobalFuncs.get_turf( start );
if ( !Lang13.Bool( start ) ) {
return 0;
}
open.Insert( new PathNode( start, null, false, Lang13.call( Lang13.bindf( start, dist ), end ), false ) );
while (!open.IsEmpty() && !( path != null )) {
cur = open.Pop();
closed.Add( cur.source );
closeenough = null;
if ( Lang13.Bool( mintargetdist ) ) {
closeenough = Lang13.Double( Lang13.call( Lang13.bindf( cur.source, dist ), end ) ) <= Lang13.Double( mintargetdist );
}
if ( Lang13.Bool( maxnodedepth ) && Lang13.Double( cur.nt ) > Lang13.Double( maxnodedepth ) ) {
continue;
}
if ( cur.source == end || closeenough == true ) {
path = new ByTable();
path.Add( cur.source );
while (Lang13.Bool( cur.prevNode )) {
cur = cur.prevNode;
path.Add( cur.source );
}
break;
}
L = Lang13.call( Lang13.bindf( cur.source, adjacent ), atom, id, simulated_only );
T = null;
foreach (dynamic _a in L ) {
T = _a;
if ( T == exclude || closed.contains( T ) ) {
continue;
}
newg = cur.g + Lang13.call( Lang13.bindf( cur.source, dist ), T );
if ( !Lang13.Bool( T.PNode ) ) {
open.Insert( new PathNode( T, cur, Lang13.Bool( newg ), Lang13.call( Lang13.bindf( T, dist ), end ), Lang13.Bool( cur.nt + 1 ) ) );
} else if ( Lang13.Double( newg ) < Lang13.Double( T.PNode.g ) ) {
T.PNode.prevNode = cur;
T.PNode.g = newg;
((dynamic)T.PNode).calc_f();
T.PNode.nt = cur.nt + 1;
open.ReSort( T.PNode );
}
};
}
PN = null;
foreach (dynamic _b in open.L ) {
if ( !( _b is PathNode ) ) {
continue;
}
PN = _b;
PN.source.PNode = null;
};
T2 = null;
foreach (dynamic _c in closed ) {
T2 = _c;
T2.PNode = null;
};
if ( path != null ) {
i = null;
i = 1;
while (( i ??0) <= path.len / 2) {
path.Swap( ((int)( i )), ((int)( path.len - ( i ??0) + 1 )) );
i++;
}
//.........这里部分代码省略.........
示例12: TestInsertHeap
public void TestInsertHeap()
{
Heap<int> h = new Heap<int>();
h.Insert(500);
Assert.AreEqual(500, h.PeekMinimum());
h.Insert(100);
Assert.AreEqual(100, h.PeekMinimum());
h.Insert(200);
Assert.AreEqual(100, h.PeekMinimum());
h.Insert(50);
Assert.AreEqual(50, h.PeekMinimum());
}
示例13: Insert_ThirdNode
public void Insert_ThirdNode()
{
Heap<int> heap = new Heap<int>(HeapType.Max)
{
List = new List<int>()
{
50,
75
}
};
using (ShimsContext.Create())
{
int insertedAt = -1;
ShimHeap<int>.AllInstances.HeapifyUpInt32 = (h, ix) => { insertedAt = ix; };
heap.Insert(25);
Assert.AreEqual<int>(2, insertedAt);
Assert.AreEqual<int>(50, heap.List[0]);
Assert.AreEqual<int>(75, heap.List[1]);
Assert.AreEqual<int>(25, heap.List[2]);
Assert.AreEqual<int>(3, heap.Count);
}
}
示例14: Insert_IntoEmpty
public void Insert_IntoEmpty()
{
using (ShimsContext.Create())
{
int insertedAt = -1;
ShimHeap<int>.AllInstances.HeapifyUpInt32 = (h, ix) => { insertedAt = ix; };
Heap<int> heap = new Heap<int>(HeapType.Max);
heap.Insert(50);
Assert.AreEqual<int>(0, insertedAt);
Assert.AreEqual<int>(50, heap.List[0]);
Assert.AreEqual<int>(1, heap.Count);
}
}
示例15: Insert
public void Insert()
{
int[] array = {16, 14, 10, 8, 7, 9, 3};
Heap heap = new Heap(array);
int insert = 11;
heap.Insert(insert);
Assert.AreEqual(array.Length + 1, heap.Queue.Length);
Assert.IsTrue(heap.Queue.Contains(insert));
Assert.IsTrue(IsMaxHeap(heap.Queue, 0));
}