Guest User

Untitled

a guest
Oct 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace project
  6. {
  7. class Program
  8. {
  9. public static void Main(string[] args)
  10. {
  11. BinarySearchTree<int> bst = new BinarySearchTree<int>();
  12. bst.Insert(1);
  13. bst.Insert(3);
  14. bst.Insert(5);
  15. bst.Insert(2);
  16. bst.Insert(21);
  17. bst.Insert(4, bst.root);
  18. System.Console.WriteLine(bst.FindByValue(4,bst.root).data);
  19. }
  20. }
  21.  
  22. public class BinNode<T> where T:IComparable
  23. {
  24. //constructors
  25. public BinNode(T data)
  26. {
  27. this.data = data;
  28. }
  29. public BinNode() { }
  30. //properties
  31. public BinNode<T> right {get; set;} = null;
  32. public BinNode<T> left {get; set;} = null;
  33. public T data {get;set;}
  34. }
  35.  
  36.  
  37. public class BinarySearchTree<T> where T:IComparable
  38. {
  39. public BinNode<T> root {get; set;} = null;
  40.  
  41. //recursively insert new node into the bst instance with T data
  42. public void Insert(T data, BinNode<T> node)
  43. {
  44.  
  45. if (data.CompareTo(node.data) > 0)
  46. {
  47. if (node.right == null)
  48. {
  49. node.right = new BinNode<T>(data);
  50. return;
  51. }
  52. Insert(data, node.right);
  53. System.Console.WriteLine("right");
  54. }
  55.  
  56. else if (data.CompareTo(node.data) < 0)
  57. {
  58. if (node.left == null)
  59. {
  60. node.left = new BinNode<T>(data);
  61. return;
  62. }
  63. Insert(data, node.left);
  64. System.Console.WriteLine("left");
  65. }
  66. }
  67.  
  68. //normal insertion method (inserts T data into a bst instance)
  69. public void Insert(T data)
  70. {
  71. if (root == null)
  72. {
  73. root = new BinNode<T>(data);
  74. }
  75.  
  76. BinNode<T> current = root;
  77.  
  78. while (current != null)
  79. {
  80. if (data.CompareTo(current.data) > 0)
  81. {
  82. if (current.right != null)
  83. {
  84. current = current.right;
  85. continue;
  86. }
  87. current.right = new BinNode<T>(data);
  88. }
  89.  
  90. else if (data.CompareTo(current.data) < 0)
  91. {
  92. if (current.left != null)
  93. {
  94. current = current.left;
  95. continue;
  96. }
  97. current.left = new BinNode<T>(data);
  98. }
  99.  
  100. else
  101. {
  102. return;
  103. }
  104.  
  105. }
  106. }
  107.  
  108. //insert all elements from an array
  109. public void InsertFromArray(T[] array)
  110. {
  111. foreach(T t in array)
  112. {
  113. this.Insert(t);
  114. }
  115. }
  116. //get the last level in the bst instance
  117. public int GetLevel(BinNode<T> node, int current=1)
  118. {
  119. int right = 0 ;
  120. int left = 0 ;
  121.  
  122. if (node.right != null)
  123. {
  124. right = GetLevel(node.right, current+1);
  125. }
  126.  
  127. if (node.left != null)
  128. {
  129. left = GetLevel(node.right, current+1);
  130. }
  131.  
  132. if (right == 0 && left == 0) return current; //this is readable in my opinion but according to best practices (microsoft), only one operation per line is good, so should this be changed or is it okay
  133.  
  134. else
  135. {
  136. return right > left ? right : left; //is this acceptable?
  137. }
  138.  
  139. }
  140. //get a queue of all the values in a given level
  141. public void GetOnLevel(BinNode<T> node, int curLevel, int trgLevel, Queue<T> result)
  142. {
  143. if (curLevel == trgLevel)
  144. {
  145. result.Enqueue(node.data);
  146. }
  147. else
  148. {
  149. if (node.left != null)
  150. {
  151. GetOnLevel(node.left, curLevel+1, trgLevel, result);
  152. }
  153.  
  154. if(node.right != null)
  155. {
  156. GetOnLevel(node.right, curLevel+1, trgLevel, result);
  157. }
  158. }
  159. }
  160.  
  161. //returns the node that holds data equivalent to T data
  162. public BinNode<T> FindByValue(T data, BinNode<T> node)
  163. {
  164. if (node == null) return null;
  165.  
  166. if (data.Equals(node.data))
  167. {
  168. return node;
  169. }
  170.  
  171. if (data.CompareTo(node.data) > 0)
  172. {
  173. return FindByValue(data, node.right);
  174. }
  175. else if (data.CompareTo(node.data) < 0)
  176. {
  177. return FindByValue(data, node.left);
  178. }
  179.  
  180. else return null;
  181. }
  182.  
  183. }
  184. }
  185.  
  186. if (right == 0 && left == 0) return current;
  187.  
  188. return right > left ? right : left;
Add Comment
Please, Sign In to add comment