Heap算法实现

  1.  using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace .DSObj
  5. {
  6.     /*****************************************************************************
  7.      * 
  8.      * 数组表示树                   [0]=8          parent = (index-1)/2;
  9.      *                8             [1]=3          leftchild = index*2+1;
  10.      *             /    /           [2]=6          rightchild = leftchild+1;
  11.      *            3      6          [3]=0          最右边非叶节点(6)=maxIndex/2-1;
  12.      *           / /    / /         [4]=3
  13.      *          0   3  4   5        [5]=4
  14.      *                              [6]=5
  15.      * 
  16.      * **************************************************************************/
  17.     /// <summary>
  18.     /// //
  19.     /// </summary>
  20.     public class Heap<T> where T : System.IComparable
  21.     {
  22.         T[] heapArr;
  23.         int m_maxSize;
  24.         int m_curIndex;
  25.         public Heap(int maxSize)
  26.         {
  27.             heapArr = new T[maxSize];
  28.             m_maxSize = maxSize;
  29.             m_curIndex = 0;
  30.         }
  31.         public Heap(int maxSize,T[] items):this(maxSize)
  32.         {
  33.             Insert(items);
  34.         }
  35.         public bool Insert(T item)
  36.         {
  37.             if (m_curIndex == m_maxSize)
  38.                 return false;
  39.             heapArr[m_curIndex] = item;          
  40.             TrickleUp(m_curIndex++);
  41.             return true;
  42.         }
  43.         public void Insert(T[] items)
  44.         {
  45.             foreach (T temp in items)
  46.                 Insert(temp);
  47.         }
  48.         public bool IsEmpty()
  49.         {
  50.             return m_curIndex == 0;
  51.         }
  52.         public T Remove()
  53.         {
  54.             T root = heapArr[0];
  55.             //把最后一个元素放到顶部
  56.             heapArr[0] = heapArr[--m_curIndex];
  57.             TrickleDown(0);
  58.             return root;
  59.         }
  60.         /// <summary>
  61.         /// m_curIndex / 2 -1 = 堆中最右边非叶节点的节点(6)
  62.         /// </summary>
  63.         /// <param name="itemIndex"></param>
  64.         private void TrickleDown(int itemIndex)
  65.         {
  66.             int largerIndex;//左右孩子大的索引
  67.             T item = heapArr[itemIndex];
  68.             //索引找到小于树的高度的节点,最大找到叶节点父节点。
  69.             //叶节点没有子孩子 
  70.             while (itemIndex < m_curIndex / 2)
  71.             {
  72.                 int lChildIndex = itemIndex * 2 + 1;
  73.                 int rChildIndex = lChildIndex + 1;
  74.                 if (rChildIndex < m_curIndex &
  75.                     heapArr[rChildIndex].CompareTo(heapArr[lChildIndex]) > 0)
  76.                     largerIndex = rChildIndex;
  77.                 else
  78.                     largerIndex = lChildIndex;
  79.                 if (item.CompareTo(heapArr[largerIndex]) > 0)
  80.                     break;
  81.                 heapArr[itemIndex] = heapArr[largerIndex];
  82.                 itemIndex = largerIndex;
  83.             }
  84.             heapArr[itemIndex] = item;
  85.         }
  86.         private void TrickleUp(int itemIndex)
  87.         {
  88.             int parentIndex = (itemIndex - 1) / 2;
  89.             T item = heapArr[itemIndex];
  90.             while (itemIndex>0 && parentIndex >= 0)
  91.             {
  92.                 if (item.CompareTo(heapArr[parentIndex]) > 0)
  93.                 {
  94.                     heapArr[itemIndex] = heapArr[parentIndex];
  95.                     itemIndex = parentIndex;
  96.                     //如果parentIndex=0,则已到根节点,则不用再往上找了
  97.                     if(parentIndex>0)
  98.                         parentIndex = (parentIndex - 1) / 2;
  99.                 }
  100.                 else
  101.                     break;
  102.             }
  103.             heapArr[itemIndex] = item;
  104.         }
  105.         private int FindIndex(T item)
  106.         {
  107.             int index=0;
  108.             while (index < m_curIndex / 2)
  109.             {
  110.                 int lChildIndex = index * 2 + 1;
  111.                 int rChildIndex = lChildIndex + 1;
  112.                 if (item.Equals(heapArr[lChildIndex]))
  113.                     return lChildIndex;
  114.                 if (item.Equals(heapArr[rChildIndex]))
  115.                     return rChildIndex;
  116.                 if (rChildIndex < m_curIndex &
  117.                     item.CompareTo(heapArr[lChildIndex]) < 0)
  118.                     index = rChildIndex;
  119.                 else
  120.                     index = lChildIndex;               
  121.             }
  122.             return -1;
  123.         }
  124.         public bool ChangeValue(T item,T newValue)
  125.         {
  126.            int index = FindIndex(item);
  127.            if (index > 0)
  128.            {
  129.                heapArr[index] = newValue;
  130.                if (item.CompareTo(newValue)<0)
  131.                    TrickleUp(index);
  132.                else
  133.                    TrickleDown(index);
  134.                return true;
  135.            }
  136.            return false;
  137.         }
  138.         public override string ToString()
  139.         {
  140.             string str = string.Empty;
  141.             for (int i = 0; i < m_curIndex; i++)
  142.             {
  143.                 str += heapArr[i].ToString();
  144.                 if (!string.IsNullOrEmpty(str))
  145.                     str += "|";
  146.             }
  147.             return str;
  148.         }
  149.         堆排序部份//
  150.         public static int[] HeapSort(int[] arr)
  151.         {
  152.             Heap<int> heap = new Heap<int>(arr.Length);
  153.             for (int i = 0; i < arr.Length; i++)
  154.             {
  155.                 heap.heapArr[i] = arr[i];
  156.                 heap.m_curIndex++;
  157.             }
  158.             for (int i = heap.m_curIndex / 2 - 1; i >= 0; i--)
  159.             {
  160.                 heap.TrickleDown(i);
  161.             }
  162.             for (int j = heap.m_curIndex - 1; j >= 0; j--)  
  163.             {                        
  164.                 int temp = heap.Remove();
  165.                 heap.heapArr[j] = temp;
  166.             }
  167.             return heap.heapArr;
  168.         } 
  169.     }
  170. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值