- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace .DSObj
- {
- /*****************************************************************************
- *
- * 数组表示树 [0]=8 parent = (index-1)/2;
- * 8 [1]=3 leftchild = index*2+1;
- * / / [2]=6 rightchild = leftchild+1;
- * 3 6 [3]=0 最右边非叶节点(6)=maxIndex/2-1;
- * / / / / [4]=3
- * 0 3 4 5 [5]=4
- * [6]=5
- *
- * **************************************************************************/
- /// <summary>
- /// //
- /// </summary>
- public class Heap<T> where T : System.IComparable
- {
- T[] heapArr;
- int m_maxSize;
- int m_curIndex;
- public Heap(int maxSize)
- {
- heapArr = new T[maxSize];
- m_maxSize = maxSize;
- m_curIndex = 0;
- }
- public Heap(int maxSize,T[] items):this(maxSize)
- {
- Insert(items);
- }
- public bool Insert(T item)
- {
- if (m_curIndex == m_maxSize)
- return false;
- heapArr[m_curIndex] = item;
- TrickleUp(m_curIndex++);
- return true;
- }
- public void Insert(T[] items)
- {
- foreach (T temp in items)
- Insert(temp);
- }
- public bool IsEmpty()
- {
- return m_curIndex == 0;
- }
- public T Remove()
- {
- T root = heapArr[0];
- //把最后一个元素放到顶部
- heapArr[0] = heapArr[--m_curIndex];
- TrickleDown(0);
- return root;
- }
- /// <summary>
- /// m_curIndex / 2 -1 = 堆中最右边非叶节点的节点(6)
- /// </summary>
- /// <param name="itemIndex"></param>
- private void TrickleDown(int itemIndex)
- {
- int largerIndex;//左右孩子大的索引
- T item = heapArr[itemIndex];
- //索引找到小于树的高度的节点,最大找到叶节点父节点。
- //叶节点没有子孩子
- while (itemIndex < m_curIndex / 2)
- {
- int lChildIndex = itemIndex * 2 + 1;
- int rChildIndex = lChildIndex + 1;
- if (rChildIndex < m_curIndex &
- heapArr[rChildIndex].CompareTo(heapArr[lChildIndex]) > 0)
- largerIndex = rChildIndex;
- else
- largerIndex = lChildIndex;
- if (item.CompareTo(heapArr[largerIndex]) > 0)
- break;
- heapArr[itemIndex] = heapArr[largerIndex];
- itemIndex = largerIndex;
- }
- heapArr[itemIndex] = item;
- }
- private void TrickleUp(int itemIndex)
- {
- int parentIndex = (itemIndex - 1) / 2;
- T item = heapArr[itemIndex];
- while (itemIndex>0 && parentIndex >= 0)
- {
- if (item.CompareTo(heapArr[parentIndex]) > 0)
- {
- heapArr[itemIndex] = heapArr[parentIndex];
- itemIndex = parentIndex;
- //如果parentIndex=0,则已到根节点,则不用再往上找了
- if(parentIndex>0)
- parentIndex = (parentIndex - 1) / 2;
- }
- else
- break;
- }
- heapArr[itemIndex] = item;
- }
- private int FindIndex(T item)
- {
- int index=0;
- while (index < m_curIndex / 2)
- {
- int lChildIndex = index * 2 + 1;
- int rChildIndex = lChildIndex + 1;
- if (item.Equals(heapArr[lChildIndex]))
- return lChildIndex;
- if (item.Equals(heapArr[rChildIndex]))
- return rChildIndex;
- if (rChildIndex < m_curIndex &
- item.CompareTo(heapArr[lChildIndex]) < 0)
- index = rChildIndex;
- else
- index = lChildIndex;
- }
- return -1;
- }
- public bool ChangeValue(T item,T newValue)
- {
- int index = FindIndex(item);
- if (index > 0)
- {
- heapArr[index] = newValue;
- if (item.CompareTo(newValue)<0)
- TrickleUp(index);
- else
- TrickleDown(index);
- return true;
- }
- return false;
- }
- public override string ToString()
- {
- string str = string.Empty;
- for (int i = 0; i < m_curIndex; i++)
- {
- str += heapArr[i].ToString();
- if (!string.IsNullOrEmpty(str))
- str += "|";
- }
- return str;
- }
- 堆排序部份//
- public static int[] HeapSort(int[] arr)
- {
- Heap<int> heap = new Heap<int>(arr.Length);
- for (int i = 0; i < arr.Length; i++)
- {
- heap.heapArr[i] = arr[i];
- heap.m_curIndex++;
- }
- for (int i = heap.m_curIndex / 2 - 1; i >= 0; i--)
- {
- heap.TrickleDown(i);
- }
- for (int j = heap.m_curIndex - 1; j >= 0; j--)
- {
- int temp = heap.Remove();
- heap.heapArr[j] = temp;
- }
- return heap.heapArr;
- }
- }
- }
Heap算法实现
最新推荐文章于 2021-10-29 18:08:19 发布