Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. class MaxHeap<T> : IEnumerable<T> where T : IComparable
  6. {
  7. private T[] UnderlyingArray;
  8. private int ElementCount = 0;
  9. private int OldCount = 0;
  10. private bool IsHeap = true;
  11.  
  12. public MaxHeap(int starting_size)
  13. {
  14. UnderlyingArray = new T[starting_size];
  15. }
  16.  
  17. public MaxHeap()
  18. {
  19. UnderlyingArray = new T[5];
  20. }
  21.  
  22. public int Count
  23. {
  24. get
  25. {
  26. return ElementCount;
  27. }
  28. }
  29.  
  30. public void Clear()
  31. {
  32. UnderlyingArray = new T[UnderlyingArray.Length];
  33. }
  34.  
  35. public T this[int index]
  36. {
  37. get
  38. {
  39. return UnderlyingArray[index];
  40. }
  41. }
  42.  
  43. public void Add(T new_value)
  44. {
  45. if (!IsHeap)
  46. BuildHeap();
  47.  
  48. if (ElementCount == UnderlyingArray.Length)
  49. Resize();
  50.  
  51. UnderlyingArray[ElementCount] = new_value;
  52. Swim();
  53. ElementCount++;
  54. }
  55.  
  56. public T PopTop()
  57. {
  58. if (!IsHeap)
  59. BuildHeap();
  60.  
  61. T max = UnderlyingArray[0];
  62.  
  63. Swap(0, ElementCount - 1);
  64. UnderlyingArray[ElementCount - 1] = default(T);
  65. ElementCount--;
  66. Sink();
  67.  
  68. return max;
  69. }
  70.  
  71. public void Sort()
  72. {
  73. if (!IsHeap)
  74. BuildHeap();
  75.  
  76. OldCount = ElementCount;
  77.  
  78. for (int i = 0; i < OldCount; i++)
  79. {
  80. Swap(0, ElementCount - 1);
  81. ElementCount--;
  82. Sink();
  83. }
  84.  
  85. IsHeap = false;
  86. ElementCount = OldCount;
  87. }
  88.  
  89. public void BuildHeap()
  90. {
  91. if (IsHeap)
  92. return;
  93.  
  94. int last_item = OldCount - 1;
  95.  
  96. for (int index = 0; index < OldCount / 2; index++)
  97. {
  98. Swap(index, last_item);
  99. last_item--;
  100. }
  101.  
  102. ElementCount = OldCount;
  103. IsHeap = true;
  104. }
  105.  
  106. private void Swim()
  107. {
  108. int current_index = ElementCount;
  109.  
  110. while (current_index != 0)
  111. {
  112. int parent_index = FindParent(current_index);
  113.  
  114. if (UnderlyingArray[current_index].CompareTo(UnderlyingArray[parent_index]) == 0)
  115. break;
  116.  
  117. else if (UnderlyingArray[current_index].CompareTo(UnderlyingArray[parent_index]) > 0)
  118. Swap(current_index, parent_index);
  119.  
  120. current_index = parent_index;
  121. }
  122. }
  123.  
  124. private void Swap(int first, int second)
  125. {
  126. T temp = UnderlyingArray[first];
  127. UnderlyingArray[first] = UnderlyingArray[second];
  128. UnderlyingArray[second] = temp;
  129. }
  130.  
  131. private void Sink()
  132. {
  133. int current_index = 0;
  134.  
  135. while (current_index < ElementCount)
  136. {
  137. int max_child_index = current_index;
  138. int left_child_index = FindLeft(current_index);
  139. int right_child_index = FindRight(current_index);
  140.  
  141. if (left_child_index >= ElementCount)
  142. break;
  143.  
  144. if (right_child_index >= ElementCount)
  145. max_child_index = left_child_index;
  146.  
  147. if (right_child_index < ElementCount &&
  148. UnderlyingArray[left_child_index].CompareTo(UnderlyingArray[right_child_index]) == 0)
  149. max_child_index = left_child_index;
  150.  
  151. if (right_child_index < ElementCount &&
  152. UnderlyingArray[left_child_index].CompareTo(UnderlyingArray[right_child_index]) > 0)
  153. max_child_index = left_child_index;
  154.  
  155. if (right_child_index < ElementCount &&
  156. UnderlyingArray[left_child_index].CompareTo(UnderlyingArray[right_child_index]) < 0)
  157. max_child_index = right_child_index;
  158.  
  159. if (UnderlyingArray[max_child_index].CompareTo(UnderlyingArray[current_index]) == 0)
  160. break;
  161.  
  162. if (UnderlyingArray[max_child_index].CompareTo(UnderlyingArray[current_index]) > 0)
  163. Swap(current_index, max_child_index);
  164.  
  165. current_index = max_child_index;
  166. }
  167. }
  168.  
  169. private int FindParent(int current_index)
  170. {
  171. return (current_index - 1) / 2;
  172. }
  173.  
  174. private int FindLeft(int current_index)
  175. {
  176. return (current_index * 2) + 1;
  177. }
  178.  
  179. private int FindRight(int current_index)
  180. {
  181. return (current_index * 2) + 2;
  182. }
  183.  
  184. private void Resize()
  185. {
  186. T[] new_array = new T[UnderlyingArray.Length * 2];
  187.  
  188. for (int index = 0; index < UnderlyingArray.Length; index++)
  189. new_array[index] = UnderlyingArray[index];
  190.  
  191. UnderlyingArray = new_array;
  192. }
  193.  
  194. public IEnumerator<T> GetEnumerator()
  195. {
  196. for (int index = 0; index < ElementCount; index++)
  197. {
  198. T temp = UnderlyingArray[index];
  199. //Console.WriteLine("Current Value: {0}", temp);
  200. yield return temp;
  201. }
  202. }
  203.  
  204. IEnumerator IEnumerable.GetEnumerator()
  205. {
  206. return GetEnumerator();
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement