SHARE
TWEET

Untitled

ShreyasG101 Dec 6th, 2019 (edited) 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     private TreeNode<T> balance(TreeNode<T> curNode, T data) {
  2.                
  3.         if(curNode==null) return null;
  4.         int balDiff=balanceFactor(curNode);
  5.         // Left Rotate
  6.         if (balDiff > 1 && data.compareTo(curNode.left.data)<0) {
  7.             {
  8.                 return rotateRight(curNode);
  9.             }
  10.         }
  11.  
  12.         // Right Rotate
  13.         if (balDiff < -1 && data.compareTo(curNode.right.data)>0) {
  14.             return rotateLeft(curNode);
  15.         }
  16.  
  17.  
  18.         // Left Right Rotate
  19.         if (balDiff > 1 &&  data.compareTo(curNode.left.data)>0) {
  20.             curNode.left = rotateLeft(curNode.left);
  21.             return rotateRight(curNode);
  22.         }
  23.  
  24.         // Right Left Rotate
  25.         if (balDiff < -1 && data.compareTo(curNode.right.data)<0) {
  26.             curNode.right = rotateRight(curNode.right);
  27.             return rotateLeft(curNode);
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top