Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BinaryTreeNode<TNode> : IComparable<TNode> where TNode : IComparable<TNode>
- {
- public BinaryTreeNode(TNode value)
- {
- Value = value;
- }
- public BinaryTreeNode<TNode> Right { get; set; }
- public BinaryTreeNode<TNode> Left { get; set; }
- public TNode Value { get; private set; }
- public int CompareTo(TNode other) => Value.CompareTo(other);
- }
- // Сreating a binary tree
- public class BinaryTree<T> : IEnumerable<T> where T : IComparable<T>
- {
- public delegate void BinaryTreeAdd(T value);
- public event BinaryTreeAdd OnTreeItemAdded;
- private BinaryTreeNode<T> Head; // Set the pointer to the root of the tree
- #region Додавання нового вузла дерева
- public void Add(T value)
- {
- // First case: the tree is empty
- if (Head == null)
- Head = new BinaryTreeNode<T>(value);
- // Second case: the tree isn't empty, so use recursive algorithms
- // to find where to add a node
- else
- AddTo(Head, value);
- OnTreeItemAdded?.Invoke(value);
- }
- // Recursive algorithms
- private void AddTo(BinaryTreeNode<T> node, T value)
- {
- // First case: the value of the added node is less than the value of the current node
- if (value.CompareTo(node.Value) < 0)
- {
- // If the left child is missing-add it
- if (node.Left == null)
- node.Left = new BinaryTreeNode<T>(value);
- else
- // Re-iteration
- AddTo(node.Left, value);
- }
- // Second case: the value of the added node is the same or greader
- else
- {
- // If the rigth child is missing-add it
- if (node.Right == null)
- node.Right = new BinaryTreeNode<T>(value);
- else
- AddTo(node.Right, value);
- }
- }
- #endregion
- #region Нумератор
- public IEnumerator<T> GetEnumerator() => InOrderTraversal();
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
- #endregion
- #region Семметричный порядок
- public IEnumerator<T> InOrderTraversal()
- {
- if (Head != null)
- {
- // Save the node to the stack
- Stack<BinaryTreeNode<T>> stack = new Stack<BinaryTreeNode<T>>();
- BinaryTreeNode<T> current = Head;
- bool goLeftNext = true;
- // Beginning. Placing the root of the tree on the stack.
- stack.Push(current);
- while (stack.Count > 0)
- {
- // If we move to the left ...
- if (goLeftNext)
- {
- // A record of all left descendants in the stack..
- while (current.Left != null)
- {
- stack.Push(current);
- current = current.Left;
- }
- }
- // Return of the leftmost descendant
- yield return current.Value;
- // If he has a right descendant
- if (current.Right != null)
- {
- current = current.Right;
- // If we once go to the right, then we must again make a pass on the left side
- goLeftNext = true;
- }
- else
- {
- // If there is no right child, we must extract the parent node from the stack
- current = stack.Pop();
- goLeftNext = false;
- }
- }
- }
- }
- #endregion
- }
- public class StudentInfo : IComparable<StudentInfo>
- {
- public List<StudentInfo> StudenList;
- public StudentInfo(string Name, string Test, string Date, int Resault)
- {
- this.Name = Name;
- this.Test = Test;
- this.Date = Date;
- this.Result = Resault;
- }
- public string Name { get; set; }
- public string Test { get; set; }
- public string Date { get; set; }
- public int Result { get; set; }
- public int CompareTo(StudentInfo other)
- {
- return this.Result - other.Result;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Program program = new Program();
- BinaryTree<int> binaryTree = new BinaryTree<int>();
- StudentInfo student1 = new StudentInfo("Leonid Gricenko", "OM", "14.10.2019", 16);
- StudentInfo student2 = new StudentInfo("Olhovoi Roman", "OT", "16.10.2019", 3);
- StudentInfo student3 = new StudentInfo("Petrov Alex", "TVE", "11.10.2019", 10);
- StudentInfo student4 = new StudentInfo("Rudenko Vladislav", "TVN", "01.11.2019", 13);
- StudentInfo student5 = new StudentInfo("Povoroznuk Kostya", "TOE", "05.10.2019", 11);
- binaryTree.Add(student1.Result);
- binaryTree.Add(student2.Result);
- binaryTree.Add(student3.Result);
- binaryTree.Add(student4.Result);
- binaryTree.Add(student5.Result);
- binaryTree.OnTreeItemAdded += value =>
- {
- Console.WriteLine($"Добавлен новый элемент в ветку: {student1.Result}");
- };
- foreach (var item in binaryTree)
- {
- Console.WriteLine(item);
- }
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement