/// <summary> /// constructor heap /// </summary> /// <param name="arr"></param> /// <param name="node"></param> /// <param name="i">the index of node in arr</param> /// <param name="heapSize"></param> private void BuildHeap(T[] arr, BinaryHeapNode <T> node, int i, int heapSize) { int left = 2 * i + 1; int right = 2 * i + 2; if (left >= heapSize) { return; } BinaryHeapNode <T> leftNode = new BinaryHeapNode <T>(arr[left], node); node.Left = leftNode; BuildHeap(arr, leftNode, left, heapSize); if (right >= heapSize) { return; } BinaryHeapNode <T> rightNode = new BinaryHeapNode <T>(arr[right], node); node.Right = rightNode; BuildHeap(arr, rightNode, right, heapSize); }
/// <summary> /// constructor /// </summary> /// <param name="arr"></param> /// <param name="heapSize">heapsize must not bigger than arr.Length</param> /// <param name="type"></param> public BinaryHeap(T[] arr, int heapSize, HeapType type) { Debug.Assert(heapSize <= arr.Length); HeapType = type; Root = new BinaryHeapNode <T>(arr[0]); BuildHeap(arr, Root, 0, heapSize); }
public BinaryHeapNode(T key, BinaryHeapNode <T> parent) { this.Key = key; this.Parent = parent; }