code_junkie

Fundamental Data Structures in C#

Nov 10th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. class BSTNode<T> where T : IComparable<T>
  2. {
  3. private BSTNode<T> _left = null;
  4. private BSTNode<T> _right = null;
  5. private T _value = default(T);
  6.  
  7. public T Value
  8. {
  9. get { return this._value; }
  10. set { this._value = value; }
  11. }
  12.  
  13. public BSTNode<T> Left
  14. {
  15. get { return _left; }
  16. set { this._left = value; }
  17. }
  18.  
  19. public BSTNode<T> Right
  20. {
  21. get { return _right; }
  22. set { this._right = value; }
  23. }
  24. }
  25.  
  26. class BinarySearchTree<T> where T : IComparable<T>
  27. {
  28. private BSTNode<T> _root = null;
  29. private int _count = 0;
  30.  
  31. public virtual void Clear()
  32. {
  33. _root = null;
  34. _count = 0;
  35. }
  36.  
  37. public virtual int Count
  38. {
  39. get { return _count; }
  40. }
  41.  
  42. public virtual void Add(T value)
  43. {
  44. BSTNode<T> newNode = new BSTNode<T>();
  45. int compareResult = 0;
  46.  
  47. newNode.Value = value;
  48.  
  49. if (_root == null)
  50. {
  51. this._count++;
  52. _root = newNode;
  53. }
  54. else
  55. {
  56. BSTNode<T> current = _root;
  57. BSTNode<T> parent = null;
  58.  
  59. while (current != null)
  60. {
  61. compareResult = current.Value.CompareTo(newNode.Value);
  62.  
  63. if (compareResult > 0)
  64. {
  65. parent = current;
  66. current = current.Left;
  67. }
  68. else if (compareResult < 0)
  69. {
  70. parent = current;
  71. current = current.Right;
  72. }
  73. else
  74. {
  75. // Node already exists
  76. throw new ArgumentException("Duplicate nodes are not allowed.");
  77. }
  78. }
  79.  
  80. this._count++;
  81.  
  82. compareResult = parent.Value.CompareTo(newNode.Value);
  83. if (compareResult > 0)
  84. {
  85. parent.Left = newNode;
  86. }
  87. else
  88. {
  89. parent.Right = newNode;
  90. }
  91. }
  92. }
  93.  
  94. public virtual BSTNode<T> FindByValue(T value)
  95. {
  96. BSTNode<T> current = this._root;
  97.  
  98. if (current == null)
  99. return null; // Tree is empty.
  100. else
  101. {
  102. while (current != null)
  103. {
  104. int result = current.Value.CompareTo(value);
  105. if (result == 0)
  106. {
  107. // Found the corrent Node.
  108. return current;
  109. }
  110. else if (result > 0)
  111. {
  112. current = current.Left;
  113. }
  114. else
  115. {
  116. current = current.Right;
  117. }
  118. }
  119.  
  120. return null;
  121. }
  122. }
  123.  
  124. public virtual void Delete(T value)
  125. {
  126.  
  127. BSTNode<T> current = this._root;
  128. BSTNode<T> parent = null;
  129.  
  130. int result = current.Value.CompareTo(value);
  131.  
  132. while (result != 0 && current != null)
  133. {
  134. if (result > 0)
  135. {
  136. parent = current;
  137. current = current.Left;
  138. }
  139. else if (result < 0)
  140. {
  141. parent = current;
  142. current = current.Right;
  143. }
  144.  
  145. result = current.Value.CompareTo(value);
  146. }
  147.  
  148. if (current == null)
  149. throw new ArgumentException("Cannot find item to delete.");
  150.  
  151.  
  152.  
  153. if (current.Right == null)
  154. {
  155. if (parent == null)
  156. this._root = current.Left;
  157. else
  158. {
  159. result = parent.Value.CompareTo(current.Value);
  160. if (result > 0)
  161. {
  162. parent.Left = current.Left;
  163. }
  164. else if (result < 0)
  165. {
  166. parent.Right = current.Left;
  167. }
  168. }
  169. }
  170. else if (current.Right.Left == null)
  171. {
  172. if (parent == null)
  173. this._root = current.Right;
  174. else
  175. {
  176. result = parent.Value.CompareTo(current.Value);
  177. if (result > 0)
  178. {
  179. parent.Left = current.Right;
  180. }
  181. else if (result < 0)
  182. {
  183. parent.Right = current.Right;
  184. }
  185. }
  186. }
  187. else
  188. {
  189.  
  190. BSTNode<T> furthestLeft = current.Right.Left;
  191. BSTNode<T> furthestLeftParent = current.Right;
  192.  
  193. while (furthestLeft.Left != null)
  194. {
  195. furthestLeftParent = furthestLeft;
  196. furthestLeft = furthestLeft.Left;
  197. }
  198.  
  199. furthestLeftParent.Left = furthestLeft.Right;
  200.  
  201. furthestLeft.Left = current.Left;
  202. furthestLeft.Right = current.Right;
  203.  
  204. if (parent != null)
  205. {
  206. result = parent.Value.CompareTo(current.Value);
  207. if (result > 0)
  208. {
  209. parent.Left = furthestLeft;
  210. }
  211. else if (result < 0)
  212. {
  213. parent.Right = furthestLeft;
  214. }
  215. }
  216. else
  217. {
  218. this._root = furthestLeft;
  219. }
  220. }
  221.  
  222. this._count--;
  223. }
  224. }
  225. }
Add Comment
Please, Sign In to add comment