HTsekin

Tree

Mar 15th, 2021 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace BST
  6. {
  7. public class BinarySearchTree<T> : IBinarySearchTree<T> where T : IComparable<T>
  8. {
  9. private T value;
  10. private IBinarySearchTree<T> left;
  11. private IBinarySearchTree<T> right;
  12.  
  13. public BinarySearchTree(T value)
  14. {
  15. this.value = value;
  16. this.left = null;
  17. this.right = null;
  18. }
  19.  
  20. public int Height { get => GetHeight(); }
  21. public IList<T> GetBFS()
  22. {
  23. IList<T> list = new List<T>();
  24.  
  25. var queue = new Queue<IBinarySearchTree<T>>();
  26.  
  27. queue.Enqueue(this);
  28.  
  29. while (queue.Count > 0)
  30. {
  31. var node = queue.Dequeue();
  32.  
  33. list.Add(node.GetRootValue());
  34.  
  35. if (node.GetLeftTree() != null)
  36. queue.Enqueue(node.GetLeftTree());
  37.  
  38. if (node.GetRightTree() != null)
  39. queue.Enqueue(node.GetRightTree());
  40. }
  41.  
  42. return list;
  43. }
  44.  
  45. public IList<T> GetInOrder()
  46. {
  47. IList<T> list = new List<T>();
  48. GetInOrder(this, list);
  49. return list;
  50. }
  51.  
  52. public void GetInOrder(IBinarySearchTree<T> node, IList<T> list)
  53. {
  54. if (node.GetLeftTree() != null)
  55. {
  56. this.GetInOrder(node.GetLeftTree(), list);
  57. }
  58.  
  59. list.Add(node.GetRootValue());
  60.  
  61. if (node.GetRightTree() != null)
  62. {
  63. this.GetInOrder(node.GetRightTree(), list);
  64. }
  65. }
  66. public IBinarySearchTree<T> GetLeftTree()
  67. {
  68. return this.left;
  69. }
  70.  
  71. public IList<T> GetPostOrder()
  72. {
  73. IList<T> list = new List<T>();
  74. GetPostOrder(this, list);
  75. return list;
  76. }
  77.  
  78. public void GetPostOrder(IBinarySearchTree<T> node, IList<T> list)
  79. {
  80. if (node.GetLeftTree() != null)
  81. {
  82. this.GetPostOrder(node.GetLeftTree(), list);
  83. }
  84.  
  85. if (node.GetRightTree() != null)
  86. {
  87. this.GetPostOrder(node.GetRightTree(), list);
  88. }
  89.  
  90. list.Add(node.GetRootValue());
  91. }
  92. public IList<T> GetPreOrder()
  93. {
  94. IList<T> list = new List<T>();
  95. GetPreOrder(this, list);
  96. return list;
  97. }
  98.  
  99. public void GetPreOrder(IBinarySearchTree<T> node, IList<T> list)
  100. {
  101. list.Add(node.GetRootValue());
  102.  
  103. if (node.GetLeftTree() != null)
  104. {
  105. this.GetPreOrder(node.GetLeftTree(), list);
  106. }
  107.  
  108. if (node.GetRightTree() != null)
  109. {
  110. this.GetPreOrder(node.GetRightTree(), list);
  111. }
  112. }
  113.  
  114.  
  115. public IBinarySearchTree<T> GetRightTree()
  116. {
  117. return this.right;
  118. }
  119. public T GetRootValue()
  120. {
  121. return this.value;
  122. }
  123. public void Insert(T element)
  124. {
  125. if(this.value != null)
  126. {
  127. if (this.value.CompareTo(element) > 0)
  128. {
  129. if (this.left != null)
  130. {
  131. this.left.Insert(element);
  132. }
  133. else
  134. {
  135. this.left = new BinarySearchTree<T>(element);
  136. }
  137. }
  138. else if(this.value.CompareTo(element) < 0)
  139. {
  140. if(this.right != null)
  141. {
  142. this.right.Insert(element);
  143. }
  144. else
  145. {
  146. this.right = new BinarySearchTree<T>(element);
  147. }
  148. }
  149. else
  150. {
  151. throw new ArgumentException("Eelement is already added!");
  152. }
  153. }
  154. else
  155. {
  156. this.value = element;
  157. }
  158.  
  159. }
  160.  
  161.  
  162. public bool Search(T element)
  163. {
  164. return Search(element, this);
  165. }
  166.  
  167. public bool Search(T element, IBinarySearchTree<T> node)
  168. {
  169. int result = element.CompareTo(node.GetRootValue());
  170.  
  171. if (result == 0)
  172. {
  173. return true;
  174. }
  175. else if(result < 0)
  176. {
  177. if (node.GetLeftTree() != null)
  178. return Search(element, node.GetLeftTree());
  179.  
  180. return false;
  181. }
  182. else
  183. {
  184. if (node.GetRightTree() != null)
  185. return Search(element, node.GetRightTree());
  186.  
  187. return false;
  188. }
  189. }
  190.  
  191. public int GetHeight()
  192. {
  193. return GetHeight(this);
  194. }
  195.  
  196. public int GetHeight(IBinarySearchTree<T> node)
  197. {
  198. if (node == null)
  199. {
  200. return -1;
  201. }
  202. else
  203. {
  204. int heightL = GetHeight(node.GetLeftTree());
  205. int heightR = GetHeight(node.GetRightTree());
  206.  
  207. if (heightL > heightR)
  208. {
  209. return heightL + 1;
  210. }
  211. else
  212. {
  213. return heightR + 1;
  214. }
  215. }
  216.  
  217. }
  218.  
  219. // Advanced task!
  220. public bool Remove(T value)
  221. {
  222. return this.Remove(value, this);
  223. }
  224.  
  225. public bool Remove(T value, IBinarySearchTree<T> node)
  226. {
  227. if (node == null)
  228. return false;
  229.  
  230. if(node.GetLeftTree() == null && node.GetRightTree() == null)
  231. {
  232. node = null;
  233. }
  234.  
  235. return true;
  236. }
  237. }
  238. }
  239.  
Add Comment
Please, Sign In to add comment