Пример #1
0
 /// <summary>
 /// Constructing an empty heap
 /// </summary>
 /// <param name="capacity">Capacity of the Heap</param>
 /// <param name="heapProperty">Indicates whether it is a max-heap or min-heap.</param>
 /// <param name="comparer">An ICompare</param>
 public BinaryHeap(int capacity, HeapProperty heapProperty, Func <TSource, TKey> keySelector, IComparer <TKey> comparer)
 {
     _a           = new TSource[capacity];
     _size        = 0;
     _keySelector = keySelector;
     _comparer    = comparer;
     SetHeapProperty(heapProperty);
 }
Пример #2
0
        /// <summary>
        /// Constructing a binary heap by heapify an array.
        /// </summary>
        /// <param name="a">The array to heapify.</param>
        /// <param name="heapProperty">Indicate whether it is a max-heap or min-heap.</param>
        /// <param name="comparer">An ICompare</param>
        public BinaryHeap(TSource[] a, HeapProperty heapProperty, Func <TSource, TKey> keySelector, IComparer <TKey> comparer)
        {
            _a           = a;
            _size        = a.Length;
            _keySelector = keySelector;
            _comparer    = comparer;
            SetHeapProperty(heapProperty);

            Heapify(a, _size, keySelector, _comparer, _heapPropertyPredicate);
        }
Пример #3
0
 private void SetHeapProperty(HeapProperty heapProperty)
 {
     if (heapProperty == HeapProperty.MaxHeap)
     {
         _heapPropertyPredicate = (b) => !b;
     }
     else
     {
         _heapPropertyPredicate = (b) => b;
     }
 }
Пример #4
0
        /// <summary>
        /// Verifies the Heap property
        /// </summary>
        /// <returns>Whether it is a max heap or a min heap</returns>
        public bool VerifyHeapProperty(HeapProperty hp)
        {
            bool satisfyHeapProperty = true;

            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[Parent(i)].CompareTo(nodes[i]) * ((hp == HeapProperty.MaxHeap) ? 1 : -1) < 0)
                {
                    satisfyHeapProperty = false;
                }
            }

            return(satisfyHeapProperty);
        }
Пример #5
0
 public Heap(IEnumerable <HeapNode> nodes, HeapProperty heapProperty)
 {
     this.nodes        = new List <HeapNode>(nodes);
     this.heapProperty = (heapProperty == HeapProperty.MaxHeap) ? 1 : -1;
 }
Пример #6
0
 /// <summary>
 /// Constructing an empty heap
 /// </summary>
 /// <param name="capacity">Capacity of the Heap</param>
 /// <param name="heapProperty">Indicates whether it is a max-heap or min-heap.</param>
 public BinaryHeap(int capacity, HeapProperty heapProperty, Func <TSource, TKey> keySelector)
     : this(capacity, heapProperty, keySelector, Comparer <TKey> .Default)
 {
 }
Пример #7
0
 /// <summary>
 /// Constructing a binary heap by heapify an array.
 /// </summary>
 /// <param name="a">The array to heapify.</param>
 /// <param name="heapProperty">Indicate whether it is a max-heap or min-heap.</param>
 public BinaryHeap(TSource[] a, HeapProperty heapProperty, Func <TSource, TKey> keySelector)
     : this(a, heapProperty, keySelector, Comparer <TKey> .Default)
 {
 }