2022-09-04 08:16:13 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace C_.Datastructures.Heap
|
|
|
|
{
|
|
|
|
internal class Heap<T> where T:IComparable
|
|
|
|
{
|
|
|
|
internal HeapNode<T>? Root { get; set; }
|
|
|
|
private int Count { get; set; }
|
|
|
|
|
|
|
|
public static Heap<T> Create(){
|
|
|
|
return new Heap<T>{
|
|
|
|
Root = null,
|
|
|
|
Count = 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Heap<T> Create(T value){
|
|
|
|
return new Heap<T>{
|
|
|
|
Root = HeapNode<T>.Create(value),
|
|
|
|
Count = 1
|
|
|
|
};
|
|
|
|
}
|
2022-11-04 11:28:52 +00:00
|
|
|
|
|
|
|
public void Add(T value){
|
|
|
|
Count++;
|
|
|
|
|
|
|
|
if (Root == default)
|
|
|
|
{//If the new node needs to be added to the top of the heap
|
|
|
|
Root = HeapNode<T>.Create(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//If the new node can be placed in a subchild
|
|
|
|
HeapNode<T> node = Root;
|
|
|
|
|
|
|
|
while(node.Left != default){
|
|
|
|
node = node.Left;
|
|
|
|
}
|
|
|
|
}
|
2022-09-04 08:16:13 +00:00
|
|
|
}
|
|
|
|
}
|