title: swapping nodes in binary tree
body: <p>I'm trying to write a function in <strong>C#</strong> that allows me to swap two nodes of a binary tree but it does not work as expected.</p>
  
  <p>Here is the <strong>class</strong> with the <strong>swap</strong> method:</p>
  
  <pre><code>class Node
  {
      public int value;
      public Node parent { get; set; }
      public Node left { get; set; }
      public Node right { get; set; }
  
      public Node addLeft(int value)
      {
          this.left = new Node(value);
          this.left.parent = this;
          this.left.left = null;
          this.left.right = null;
          return this.left;
      }
  
      public Node addRight(int value)
      {
          this.right = new Node(value);
          this.right.parent = this;
          this.right.left = null;
          this.right.right = null;
          return this.right;
      }
  
      public Node(int value)
      {
          this.value = value;
          this.parent = null;
          this.left = null;
          this.right = null;
      }
  
      public Node getRoot()
      {
          Node n = this;
          while(n.parent!=null)
          {
              n = n.parent;
          }
          return n;
      }
  
      public static void swap(ref Node n1,ref Node n2)
      {
          //updating references of n1 and n2 parents
          if(n1.Equals(n1.parent.left)) //n1 is a left child
          {
              n1.parent.left = n2;
          }
          else if(n1.Equals(n1.parent.right)) //n1 is a right child
          {
              n1.parent.right = n2;
          }
          else
          {
              throw new Exception("Something is wrong");
          }
          if (n2.Equals(n2.parent.left)) //n2 is a left child
          {
              n2.parent.left = n1;
          }
          else if (n2.Equals(n2.parent.right)) //n2 is a right child
          {
              n2.parent.right = n1;
          }
          else
          {
              throw new Exception("Something is wrong");
          }
          //updating references of n1 and n2 childs
          if(n1.left != null)
          {
              n1.left.parent = n2;
          }
          if (n1.right != null)
          {
              n1.right.parent = n2;
          }
          if (n2.left != null)
          {
              n2.left.parent = n1;
          }
          if (n2.right != null)
          {
              n2.right.parent = n1;
          }
          //Swapping n1 and n2 references
          Node t_p = n1.parent;
          Node t_l = n1.left;
          Node t_r = n1.right;
          n1.parent = n2.parent;
          n1.left = n2.left;
          n1.right = n2.right;
          n2.parent = t_p;
          n2.left = t_l;
          n2.right = t_r;
  
      }
  }
  </code></pre>
  
  <hr>
  
  <p>And here is my <strong>main</strong> function:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          Node root = new Node(10);
          Node a = root.addLeft(1);
          Node b = root.addRight(2);
          Node c = a.addLeft(3);
          Node d = a.addRight(4);
          Node e = b.addLeft(5);
          Node f = b.addRight(6);
          Node g = d.addLeft(7);
          Node h = d.addRight(8);
          Node.swap(ref a,ref d);
          Console.WriteLine("Value is: " + root.left.value);
          Console.WriteLine("Value is: " + root.left.right.value);
          Console.WriteLine("Root: " + a.getRoot().value);
          Console.WriteLine("Root: " + d.getRoot().value);
          Console.Read();
      }
  </code></pre>
  
  <hr>
  
  <p>The output of the code above is:</p>
  
  <pre><code>Value is: 4
  Value is: 1
  </code></pre>
  
  <p>It hangs after the second Console.WriteLine and I don't understand why. Can you please tell me what am I doing wrong?</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>And if I try to swap the nodes multiple times, the <strong>"Something is wrong"</strong> exception is thrown.</p>