Advertisement
KoMeDiAnT

Binary Trees. Add and Contains

Apr 30th, 2017
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. namespace BinaryTrees
  2. {
  3.     public class BinaryNode<T>
  4.     {
  5.         public BinaryNode<T> Left { get; set; }
  6.         public BinaryNode<T> Right { get; set; }
  7.         public T Element { get; set; }
  8.         public int Hash { get; }
  9.  
  10.         public BinaryNode(T element, int hash)
  11.         {
  12.             Element = element;
  13.             Hash = hash;
  14.         }
  15.  
  16.         public override int GetHashCode()
  17.         {
  18.             var prime = 514229;
  19.             var hash = 1;
  20.             hash *= prime;
  21.             hash ^= Hash;
  22.             return hash;
  23.         }
  24.     }
  25.  
  26.     public class BinaryTree<T>
  27.     {
  28.         public BinaryNode<T> Root;
  29.  
  30.         public void AddToRightOrLeft(BinaryNode<T> root, T element, int hash)
  31.         {
  32.             while (true)
  33.             {
  34.                 if (hash - root.Hash < 0)
  35.                 {
  36.                     if (root.Left == null)
  37.                     {
  38.                         root.Left = new BinaryNode<T>(element, hash);
  39.                         break;
  40.                     }
  41.                     root = root.Left;
  42.                 }
  43.                 else
  44.                 {
  45.                     if (root.Right == null)
  46.                     {
  47.                         root.Right = new BinaryNode<T>(element, hash);
  48.                         break;
  49.                     }
  50.                     root = root.Right;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         public void Add(T element)
  56.         {
  57.             var hash = element.GetHashCode();
  58.  
  59.             if (Root == null)
  60.             {
  61.                 Root = new BinaryNode<T>(element, hash);
  62.             }
  63.             else
  64.             {
  65.                 AddToRightOrLeft(Root, element, hash);
  66.             }
  67.         }
  68.  
  69.         public BinaryNode<T> FindRoot(T element, out BinaryNode<T> previusRoot)
  70.         {
  71.             var hash = element.GetHashCode();
  72.             var currentRoot = Root;
  73.             previusRoot = null;
  74.             while (currentRoot != null)
  75.             {
  76.                 if (hash - currentRoot.Hash == 0)
  77.                 {
  78.                     break;
  79.                 }
  80.                 if (hash - currentRoot.Hash > 0)
  81.                 {
  82.                     previusRoot = currentRoot;
  83.                     currentRoot = currentRoot.Right;
  84.                 }
  85.                 else if (hash - currentRoot.Hash < 0)
  86.                 {
  87.                     previusRoot = currentRoot;
  88.                     currentRoot = currentRoot.Left;
  89.                 }
  90.             }
  91.             return currentRoot;
  92.         }
  93.  
  94.         public bool Contains(T element)
  95.         {
  96.             BinaryNode<T> root;
  97.             return FindRoot(element, out root) != null;
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement