Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. class BinaryTreeNode<TNode> : IComparable<TNode> where TNode : IComparable<TNode>
  2. {
  3. public BinaryTreeNode(TNode value)
  4. {
  5. Value = value;
  6. }
  7.  
  8. public BinaryTreeNode<TNode> Right { get; set; }
  9.  
  10. public BinaryTreeNode<TNode> Left { get; set; }
  11.  
  12. public TNode Value { get; private set; }
  13.  
  14. public int CompareTo(TNode other) => Value.CompareTo(other);
  15. }
  16.  
  17.  
  18. // Сreating a binary tree
  19.  
  20. public class BinaryTree<T> : IEnumerable<T> where T : IComparable<T>
  21. {
  22. public delegate void BinaryTreeAdd(T value);
  23. public event BinaryTreeAdd OnTreeItemAdded;
  24.  
  25. private BinaryTreeNode<T> Head; // Set the pointer to the root of the tree
  26.  
  27. #region Додавання нового вузла дерева
  28.  
  29. public void Add(T value)
  30. {
  31. // First case: the tree is empty
  32.  
  33. if (Head == null)
  34. Head = new BinaryTreeNode<T>(value);
  35.  
  36. // Second case: the tree isn't empty, so use recursive algorithms
  37. // to find where to add a node
  38.  
  39. else
  40. AddTo(Head, value);
  41.  
  42. OnTreeItemAdded?.Invoke(value);
  43. }
  44.  
  45. // Recursive algorithms
  46.  
  47. private void AddTo(BinaryTreeNode<T> node, T value)
  48. {
  49. // First case: the value of the added node is less than the value of the current node
  50.  
  51. if (value.CompareTo(node.Value) < 0)
  52. {
  53. // If the left child is missing-add it
  54.  
  55. if (node.Left == null)
  56. node.Left = new BinaryTreeNode<T>(value);
  57. else
  58. // Re-iteration
  59. AddTo(node.Left, value);
  60. }
  61. // Second case: the value of the added node is the same or greader
  62. else
  63. {
  64. // If the rigth child is missing-add it
  65.  
  66. if (node.Right == null)
  67. node.Right = new BinaryTreeNode<T>(value);
  68. else
  69. AddTo(node.Right, value);
  70. }
  71. }
  72. #endregion
  73.  
  74. #region Нумератор
  75.  
  76. public IEnumerator<T> GetEnumerator() => InOrderTraversal();
  77.  
  78. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
  79.  
  80. #endregion
  81.  
  82. #region Семметричный порядок
  83.  
  84. public IEnumerator<T> InOrderTraversal()
  85. {
  86.  
  87.  
  88. if (Head != null)
  89. {
  90. // Save the node to the stack
  91.  
  92. Stack<BinaryTreeNode<T>> stack = new Stack<BinaryTreeNode<T>>();
  93. BinaryTreeNode<T> current = Head;
  94.  
  95.  
  96. bool goLeftNext = true;
  97.  
  98. // Beginning. Placing the root of the tree on the stack.
  99.  
  100. stack.Push(current);
  101.  
  102. while (stack.Count > 0)
  103. {
  104. // If we move to the left ...
  105. if (goLeftNext)
  106. {
  107. // A record of all left descendants in the stack..
  108.  
  109. while (current.Left != null)
  110. {
  111. stack.Push(current);
  112. current = current.Left;
  113. }
  114. }
  115.  
  116. // Return of the leftmost descendant
  117.  
  118. yield return current.Value;
  119.  
  120. // If he has a right descendant
  121.  
  122. if (current.Right != null)
  123. {
  124. current = current.Right;
  125.  
  126. // If we once go to the right, then we must again make a pass on the left side
  127.  
  128. goLeftNext = true;
  129. }
  130. else
  131. {
  132. // If there is no right child, we must extract the parent node from the stack
  133.  
  134. current = stack.Pop();
  135. goLeftNext = false;
  136. }
  137. }
  138. }
  139. }
  140.  
  141. #endregion
  142. }
  143. public class StudentInfo : IComparable<StudentInfo>
  144. {
  145. public List<StudentInfo> StudenList;
  146.  
  147. public StudentInfo(string Name, string Test, string Date, int Resault)
  148. {
  149.  
  150. this.Name = Name;
  151. this.Test = Test;
  152. this.Date = Date;
  153. this.Result = Resault;
  154. }
  155. public string Name { get; set; }
  156.  
  157. public string Test { get; set; }
  158.  
  159. public string Date { get; set; }
  160.  
  161. public int Result { get; set; }
  162.  
  163.  
  164. public int CompareTo(StudentInfo other)
  165. {
  166. return this.Result - other.Result;
  167. }
  168. }
  169. class Program
  170. {
  171. static void Main(string[] args)
  172. {
  173. Program program = new Program();
  174. BinaryTree<int> binaryTree = new BinaryTree<int>();
  175. StudentInfo student1 = new StudentInfo("Leonid Gricenko", "OM", "14.10.2019", 16);
  176. StudentInfo student2 = new StudentInfo("Olhovoi Roman", "OT", "16.10.2019", 3);
  177. StudentInfo student3 = new StudentInfo("Petrov Alex", "TVE", "11.10.2019", 10);
  178. StudentInfo student4 = new StudentInfo("Rudenko Vladislav", "TVN", "01.11.2019", 13);
  179. StudentInfo student5 = new StudentInfo("Povoroznuk Kostya", "TOE", "05.10.2019", 11);
  180.  
  181. binaryTree.Add(student1.Result);
  182. binaryTree.Add(student2.Result);
  183. binaryTree.Add(student3.Result);
  184. binaryTree.Add(student4.Result);
  185. binaryTree.Add(student5.Result);
  186.  
  187. binaryTree.OnTreeItemAdded += value =>
  188. {
  189. Console.WriteLine($"Добавлен новый элемент в ветку: {student1.Result}");
  190. };
  191.  
  192. foreach (var item in binaryTree)
  193. {
  194. Console.WriteLine(item);
  195. }
  196. Console.ReadKey();
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement