/// <summary> /// 根据指定数组初始化面向中位数的堆。 /// </summary> /// <param name="keys">初始数组。</param> public MedianPQ(Key[] keys) { minPQ = new MinPQ <Key>(); maxPQ = new MaxPQ <Key>(); if (keys.Length == 0) { n = 0; median = default(Key); return; } n = keys.Length; median = keys[0]; for (var i = 1; i < keys.Length; i++) { if (median.CompareTo(keys[i]) < 0) { minPQ.Insert(keys[i]); } else { maxPQ.Insert(keys[i]); } } UpdateMedian(); }
/// <summary> /// 默认构造函数,构造一个面向中位数的堆。 /// </summary> public MedianPQ() { maxPQ = new MaxPQ <Key>(); minPQ = new MinPQ <Key>(); median = default(Key); n = 0; }
/// <summary> /// 构造一个指定容量的面向中位数的堆。 /// </summary> /// <param name="capacity">初始容量。</param> public MedianPQ(int capacity) { maxPQ = new MaxPQ <Key>((capacity - 1) / 2); minPQ = new MinPQ <Key>((capacity - 1) / 2); n = 0; median = default(Key); }
/// <summary> /// 构造一个指定容量的面向中位数的堆。 /// </summary> /// <param name="capacity">初始容量。</param> public MedianPQ(int capacity) { this.maxPQ = new MaxPQ <Key>((capacity - 1) / 2); this.minPQ = new MinPQ <Key>((capacity - 1) / 2); this.n = 0; this.median = default(Key); }
/// <summary> /// 根据指定数组初始化面向中位数的堆。 /// </summary> /// <param name="keys">初始数组。</param> public MedianPQ(Key[] keys) { this.minPQ = new MinPQ <Key>(); this.maxPQ = new MaxPQ <Key>(); if (keys.Length == 0) { this.n = 0; this.median = default(Key); return; } this.n = keys.Length; this.median = keys[0]; for (int i = 1; i < keys.Length; i++) { if (this.median.CompareTo(keys[i]) < 0) { this.minPQ.Insert(keys[i]); } else { this.maxPQ.Insert(keys[i]); } } UpdateMedian(); }
/// <summary> /// 默认构造函数,构造一个面向中位数的堆。 /// </summary> public MedianPQ() { this.maxPQ = new MaxPQ <Key>(); this.minPQ = new MinPQ <Key>(); this.median = default(Key); this.n = 0; }
/// <summary> /// 根据已有元素建立一个最大-最小堆。(O(n)) /// </summary> /// <param name="keys">需要建堆的元素。</param> public MinMaxPQ(Key[] keys) { this.n = keys.Length; MinMaxNode[] minNodes = new MinMaxNode[keys.Length]; MinMaxNode[] maxNodes = new MinMaxNode[keys.Length]; for (int i = 0; i < this.n; i++) { MinMaxNode.GetNodes(keys[i], i + 1, out minNodes[i], out maxNodes[i]); } this.minPQ = new MinPQ(minNodes); this.maxPQ = new MaxPQ(maxNodes); }
/// <summary> /// 根据已有元素建立一个最大-最小堆。(O(n)) /// </summary> /// <param name="keys">需要建堆的元素。</param> public MinMaxPQ(Key[] keys) { n = keys.Length; var minNodes = new MinMaxNode[keys.Length]; var maxNodes = new MinMaxNode[keys.Length]; for (var i = 0; i < n; i++) { MinMaxNode.GetNodes(keys[i], i + 1, out minNodes[i], out maxNodes[i]); } minPQ = new MinPQ(minNodes); maxPQ = new MaxPQ(maxNodes); }
/// <summary> /// 获取堆的迭代器,元素以升序排列。 /// </summary> /// <returns></returns> public IEnumerator <Key> GetEnumerator() { MinPQ <Key> copy = new MinPQ <Key>(this.n); for (int i = 1; i <= this.n; i++) { copy.Insert(this.pq[i]); } while (!copy.IsEmpty()) { yield return(copy.DelMin()); // 下次迭代的时候从这里继续执行。 } }
/// <summary> /// 获取堆的迭代器,元素以升序排列。 /// </summary> /// <returns></returns> public IEnumerator <Key> GetEnumerator() { var copy = new MinPQ <Key>(n); for (var i = 1; i <= n; i++) { copy.Insert(pq[i]); } while (!copy.IsEmpty()) { yield return(copy.DelMin()); // 下次迭代的时候从这里继续执行。 } }
/// <summary> /// 构造一个指定容量的最大-最小堆。 /// </summary> /// <param name="capacity">堆的大小。</param> public MinMaxPQ(int capacity) { minPQ = new MinPQ(capacity); maxPQ = new MaxPQ(capacity); n = 0; }
/// <summary> /// 构造一个指定容量的最大-最小堆。 /// </summary> /// <param name="capacity">堆的大小。</param> public MinMaxPQ(int capacity) { this.minPQ = new MinPQ(capacity); this.maxPQ = new MaxPQ(capacity); this.n = 0; }