Advertisement
KoMeDiAnT

Binary Trees. Enumerable

Apr 30th, 2017
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. namespace BinaryTrees
  5. {
  6.     public class BinaryNode<T>
  7.     {
  8.         public BinaryNode<T> Left { get; set; }
  9.         public BinaryNode<T> Right { get; set; }
  10.         public T Element { get; set; }
  11.         public int Hash { get; }
  12.  
  13.         public BinaryNode(T element, int hash)
  14.         {
  15.             Element = element;
  16.             Hash = hash;
  17.         }
  18.  
  19.         public override int GetHashCode()
  20.         {
  21.             var prime = 514229;
  22.             var hash = 1;
  23.             hash *= prime;
  24.             hash ^= Hash;
  25.             return hash;
  26.         }
  27.     }
  28.  
  29.     public class BinaryTree<T> : IEnumerable<T>
  30.     {
  31.         public BinaryNode<T> Root;
  32.  
  33.         //Для 2 - ой практики
  34.         public bool Left;
  35.         public Stack<BinaryNode<T>> Stack = new Stack<BinaryNode<T>>();
  36.         //-------------------------------------------------------------
  37.  
  38.         public void AddToRightOrLeft(BinaryNode<T> root, T element, int hash)
  39.         {
  40.             while (true)
  41.             {
  42.                 if (hash - root.Hash < 0)
  43.                 {
  44.                     if (root.Left == null)
  45.                     {
  46.                         root.Left = new BinaryNode<T>(element, hash);
  47.                         break;
  48.                     }
  49.                     root = root.Left;
  50.                 }
  51.                 else
  52.                 {
  53.                     if (root.Right == null)
  54.                     {
  55.                         root.Right = new BinaryNode<T>(element, hash);
  56.                         break;
  57.                     }
  58.                     root = root.Right;
  59.                 }
  60.             }
  61.         }
  62.  
  63.         public void Add(T element)
  64.         {
  65.             var hash = element.GetHashCode();
  66.  
  67.             if (Root == null)
  68.                 Root = new BinaryNode<T>(element, hash);
  69.             else
  70.                 AddToRightOrLeft(Root, element, hash);
  71.         }
  72.  
  73.         public BinaryNode<T> FindRoot(T element, out BinaryNode<T> previusRoot)
  74.         {
  75.             var hash = element.GetHashCode();
  76.             var currentRoot = Root;
  77.             previusRoot = null;
  78.  
  79.             while (currentRoot != null)
  80.             {
  81.                 if (hash - currentRoot.Hash == 0) break;
  82.  
  83.                 if (hash - currentRoot.Hash > 0)
  84.                 {
  85.                     previusRoot = currentRoot;
  86.                     currentRoot = currentRoot.Right;
  87.                 }
  88.                 else if (hash - currentRoot.Hash < 0)
  89.                 {
  90.                     previusRoot = currentRoot;
  91.                     currentRoot = currentRoot.Left;
  92.                 }
  93.             }
  94.  
  95.             return currentRoot;
  96.         }
  97.  
  98.         public bool Contains(T element)
  99.         {
  100.             BinaryNode<T> root;
  101.             return FindRoot(element, out root) != null;
  102.         }
  103.        
  104.         //Для 2 - ой практики
  105.         public IEnumerator<T> GetEnumerator()
  106.         {
  107.             var root = Root;
  108.  
  109.             while (Stack.Count != 0)
  110.             {
  111.                 if (Left) Left = false;
  112.                 else
  113.                     if (root.Left != null)
  114.                     {
  115.                         Stack.Push(root);
  116.                         root = root.Left;
  117.                         continue;
  118.                     }
  119.                 yield return root.Element;
  120.                 if (root.Right != null)
  121.                 {
  122.                     root = root.Right;
  123.                     continue;
  124.                 }
  125.                 root = Stack.Pop();
  126.                 Left = true;
  127.             }
  128.         }
  129.  
  130.         IEnumerator IEnumerable.GetEnumerator()
  131.         {
  132.             return GetEnumerator();
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement