C# 二叉堆

二叉堆数据结构讲解: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766751.html

 

C#代码实现

using System;
using System.Collections.Generic;

namespace 二叉堆
{
    //从小到大
    public class BinaryHeap
    {
        private int[] heap;
        private int index = 1;

        public void Init(int count)
        {
            count++;
            heap = new int[count];

        }

        
        public void Enqueue(int value) 
        {
            heap[index] = value;

            if (index == 1) {
                index++;
                return;
            }

            //开始从最下层跟父节点对比,往上升级
            int temp = 0;
            int tempIndex = index;
            int parentIndex = 0;
            int tempI = tempIndex % 2 == 0 ? 0 : 1;

            //动态改变tempI=0?1,进入左右与父节点的值比较和交换
            while (heap[tempIndex] < heap[(parentIndex = (tempIndex - tempI) / 2)])
            {
                temp = heap[tempIndex];
                heap[tempIndex] = heap[parentIndex];
                heap[parentIndex] = temp;
                tempIndex = parentIndex;

                tempI = tempIndex % 2 == 0 ? 0 : 1;     //判断我现在是左节点(i * 2),还是右节点(i - 1 * 2)
            }

            index++;
        }


        public int Dequeue() 
        {
            if (index == 1)
                return heap[index];

            int result = heap[1];
            heap[1] = 0;

            int temp = 0;
            int tempIndex = 1;
            int indexLeft = 0;
            int indexRight  = 0;

            //当堆里面有两个元素的时候,也就是index>=2的时候
            while(tempIndex * 2 < index) 
            {
                indexLeft = (tempIndex * 2 < index) ? tempIndex * 2 : 0;
                indexRight = ((tempIndex) * 2 + 1 < index) ? (tempIndex) * 2 + 1 : 0;

                //两个节点都存在情况下
                if(Convert.ToBoolean(indexLeft) && Convert.ToBoolean(indexRight))
                {
                    if (heap[indexLeft] < heap[indexRight])
                    {
                        temp = heap[tempIndex];
                        heap[tempIndex] = heap[indexLeft];
                        heap[indexLeft] = temp;

                        tempIndex = indexLeft;
                    }
                    else 
                    {
                        temp = heap[tempIndex];
                        heap[tempIndex] = heap[indexRight];
                        heap[indexRight] = temp;

                        tempIndex = indexRight;
                    }
                }
                else if (Convert.ToBoolean(indexLeft))
                {

                    temp = heap[tempIndex];
                    heap[tempIndex] = heap[indexLeft];
                    heap[indexLeft] = temp;

                    tempIndex = indexLeft;
                }
                else 
                {
                    break;
                }
            }


            if(tempIndex != index - 1)
            {
                heap[tempIndex] = heap[index - 1];
                heap[index - 1] = 0;

                int tempI = tempIndex % 2 == 0 ? 0 : 1;
                int parentIndex = 0;

                //动态改变tempI=0?1,进入左右与父节点的值比较和交换
                while (heap[tempIndex] < heap[(parentIndex = (tempIndex - tempI) / 2)])
                {
                    temp = heap[tempIndex];
                    heap[tempIndex] = heap[parentIndex];
                    heap[parentIndex] = temp;
                    tempIndex = parentIndex;

                    tempI = tempIndex % 2 == 0 ? 0 : 1;     //判断我现在是左节点(i * 2),还是右节点(i - 1 * 2)
                }
            }

            index--;
            return result;
        }

        public int GetMin() 
        {
            if (index == 1)
                return 0;

            return heap[1];
        }

        public override string ToString()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb = sb.Append("0-");
            for (int i = 1; i < heap.Length; i++)
            {
                if (heap[i] == 0)
                    break;
                sb = sb.Append(heap[i] + "-");
            }

            sb = sb.Remove(sb.Length - 1, 1);


            return sb.ToString();
        }
    }
}

你可能感兴趣的:(C# 二叉堆)