title: AVL Tree Rotation Conditions
body: <p>I'm studying for a mid term about different tree algorithms like Binary trees, AVL trees, etc etc.</p>
  
  <p>The exam isn't so much about how to write the code but what happens when you're trying to fix the balance of the tree. But I'm trying to write it in code so I get comfortable for the upcoming assignments.  I think I wrote out the actual codes for the rotations as seen below but I keep getting a null pointer exception when trying to get the heights.</p>
  
  <p>The tree is implemented correctly (I randomly made the list of numbers and drew the tree out by hand, writing what the height should be per node and matching it with System.out). </p>
  
  <pre><code> private Node rightRotation(Node node) {   //Creates a temp node and rotates the child and grandchild
          Node temp = node.lC;
          node.lC = temp.rC;
          temp.rC = node;
          return temp;                              //Returns the temp node with the new alignment 
      }
  
      private Node lRotation(Node node) {
          Node temp = node.rC;
          node.rC = temp.lC;
          temp.lC = node;
          return temp;
      }
  
      private Node rLRotation(Node node) {
          node.rC = rightRotation(node.rC);
          return lRotation(node);
      }
  
      private Node lRRotation (Node node) {
          node.lC = lRotation(node.lC);
          return rightRotation(node);
      }
  
  </code></pre>
  
  <p>these are my conditions</p>
  
  <pre><code>
      void updateTree(){
          if (rC.height - lC.height &gt;= 1 &amp;&amp; rC.lC.height &gt; rC.rC.height | rC.rC == null) rLRotation(this);
          else if (lC.height - rC.height &gt;= 1 &amp;&amp; lC.rC.height &gt; lC.lC.height | lC.lC == null) lRRotation(this);
          else if (rC.height - lC.height &gt;= 1) lRotation(this);
          else if (lC.height - rC.height &gt;= 1) rightRotation(this);
      }
  
  </code></pre>