数据结构---堆的实现-C#

来源:互联网 发布:淘宝网店没上货被收回 编辑:程序博客网 时间:2024/06/09 17:38

堆是一种数据结构,其实是一种用数组储存的数,并且满足一些特性:如果是最小化堆,那么儿子节点的值一定要比父亲节点大。

看代码:

 //最小化堆,根节点最小    class Heap    {        private int[] m_heapArr;        private int m_count;        public Heap(int[] numbers,int arrSize)        {            m_heapArr = new List<int>(numbers).ToArray(); ;            m_count = arrSize;            makeHeap();        }        public int getRootValue()        {            return m_heapArr[0];        }        public void DeleteRoot()        {            Swap(0, m_count - 1);            m_count--;            shift_down(0);        }        private void makeHeap()        {            for (int i = getParentIdx(m_count-1); i >= 0; i--)            {                shift_down(i);            }        }        private void shift_down(int pos)        {            if ( getSonLeftIdx(pos)==-1)                return;            bool done = false;            while (!done)            {                int sonToExchange = getSonLeftIdx(pos) ;                int sonRight = getSonRightIdx(pos);                                if (sonRight != -1)                {                    if (m_heapArr[sonToExchange] > m_heapArr[sonRight])                    {                        sonToExchange = sonRight;                    }                }                if (m_heapArr[pos] > m_heapArr[sonToExchange])                {                    Swap(pos, sonToExchange);                    pos = sonToExchange;                }                else                {                    done = true;                }                //已经没有儿子了                if (getSonLeftIdx(pos)==-1)                    return;            }        }        private int getSonLeftIdx(int parent)        {            int son=2*parent+1;            if(son>=m_count)                return -1;            return son;        }        private int getSonRightIdx(int parent)        {            int sonr=2*parent+2;            if(sonr>=m_count)                return -1;            return sonr;        }        private int getParentIdx(int son)        {            if (son < 0)                return -1;            if (son == 0)                return 0;            return (son + 1) / 2 - 1;                    }        private void Swap( int idx1, int idx2)        {            int temp = m_heapArr[idx1];            m_heapArr[idx1] = m_heapArr[idx2];            m_heapArr[idx2] = temp;        }            }

原创粉丝点击