C# min heap implentation

   public class Heap< T> where T : IComparable
    {
        private List< T> elements = new List<T>(); 

        public int GetSize()
        {
            return elements.Count;
        }

        public T GetMin()
        {
            return this.elements.Count > 0 ? this.elements[0] : default(T);
        }

        public void Add(T item)
        {
            elements.Add(item);
            this.HeapifyUp(elements.Count - 1);
        }

        public T PopMin()
        {
            if (elements.Count > 0)
            {
                T item = elements[0];
                elements[0] = elements[elements.Count - 1];
                elements.RemoveAt(elements.Count - 1);

                this.HeapifyDown(0);
                return item;
            }

            throw new InvalidOperationException("no element in heap");
        }

        private void HeapifyUp(int index)
        {
            var parent = this.GetParent(index);
            if (parent >= 0 && elements[index].CompareTo(elements[parent]) < 0)
            {
                var temp = elements[index];
                elements[index] = elements[parent];
                elements[parent] = temp;

                this.HeapifyUp(parent);
            }
        }

        private void HeapifyDown(int index)
        {
            var smallest = index;

            var left = this.GetLeft(index);
            var right = this.GetRight(index);

            if (left < this.GetSize() && elements[left].CompareTo(elements[index]) < 0)
            {
                smallest = left;
            }

            if (right < this.GetSize() && elements[right].CompareTo(elements[smallest]) < 0)
            {
                smallest = right;
            }

            if (smallest != index)
            {
                var temp = elements[index];
                elements[index] = elements[smallest];
                elements[smallest] = temp;

                this.HeapifyDown(smallest);
            }

        }

        private int GetParent(int index)
        {
            if (index <= 0)
            {
                return -1;
            }

            return (index - 1) / 2;
        }

        private int GetLeft(int index)
        {
            return 2 * index + 1;
        }

        private int GetRight(int index)
        {
            return 2 * index + 2;
        }
    }
时间: 2024-08-05 21:54:55

C# min heap implentation的相关文章

LF.391.Determine If Array Is Min Heap

Determine if the given integer array is min heap. 1 public class Solution { 2 public boolean isMinHeap(int[] array) { 3 // Write your solution here 4 if (array == null || array.length == 0) { 5 return true ; 6 } 7 int pre = array[0] ; 8 for (int i =

Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java

Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个heap,一个是最小堆,一个是最大堆. 2.一直使maxHeap的size大于minHeap. 3. 当两边size相同时,比较新插入的value,如果它大于minHeap的最大值,把它插入到minHeap.并且把minHeap的最小值移动到maxHeap. ...具体看代码 1 /*********

Min Heap in Kotlin

class MinHeap constructor(maxSize_: Int) { var size = 0 var maxSize = maxSize_ var heapArray: Array<Int>? = null companion object { const val FRONT = 1 } init { size = 0 heapArray = Array(maxSize,{0}) heapArray!![0] = Int.MIN_VALUE } private fun par

[email&#160;protected] Implementation of minimal heap

A binary heap is a heap data struc­ture cre­ated using a binary tree. binary tree has two rules - Binary Heap has to be com­plete binary tree at all lev­els except the last level. This is called shape prop­erty. All nodes are either greater than equa

Lintcode: Heapify &amp;&amp; Summary: Heap

Given an integer array, heapify it into a min-heap array. For a heap array A, A[0] is the root of heap, and for each A[i], A[i * 2 + 1] is the left child of A[i] and A[i * 2 + 2] is the right child of A[i]. Example Given [3,2,1,4,5], return [1,2,3,4,

c++中STL之heap, priority_queue使用

一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的max-heap实际上是以一个vector表现的完全二叉树(complete binary tree).STL在<algorithm.h>中实现了对存储在vector/deque 中的元素进行堆操作的函数,包括make_heap, pop_heap, push_heap, sort_heap,对不愿

数据结构之Heap (Java)

Heap简介 Heap译为“堆”,是一种特殊的树形数据结构,它满足所有堆的特性:父节点的值大于等于子节点的值(max heap),或者小于等于子节点的值(min heap).对于max heap 根节点的值为整个树最大值,反之亦然,min heap 根节点的值为整个树最小值.本文采用Java编程语言简单实现min heap. Java Heap 对于大多数应用来说,Java堆 (Java Heap) 是Java虚拟机所管理的内存中最大的一块.Java堆是被所有线程共享的一块内存区域,在虚拟机启动

c++ heap学习

heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制. 而这个实现机制中的max-heap实际上是以一个vector表现的完全二叉树(complete binary tree).二叉堆(binary heap)就是i一种完全二叉树.也即是.整棵二叉树除了最底层的叶节点以外,都是填满的,而最低层的叶子结点必须是从左到右不留空隙.至于max-heap和min-heap,前者的任何一个父亲结

ADT - Binary Heap(二叉堆) &amp;&amp;

今天早上起来完成了一个完整的基于二叉堆实现的优先队列,其中包含最小优先和最大优先队列. 实际上上篇也说了优先队列的特性,通过建堆和堆排序操作,我们就已经看到了这种数据结构中的数据具有某种优先级别,要么非根节点大于他的子节点,要么就相反,在最大优先队列中最大优先级别就是指节点值最大的数据为根节点,每次出队时肯定是最大的先出去,反之则是最小优先队列,但要注意插入时的数据不一定是最大或最小的,优先队列会通过一点小技巧找到所有节点之间的关系并对应起来,重新使得你随意插入的数据满足优先队列的特性,因而这种