|
| 1 | +package BinaryTrees; |
| 2 | + |
| 3 | +public class AVL { |
| 4 | + int count = 5; |
| 5 | + public class Node{ |
| 6 | + private int val; |
| 7 | + private int height; |
| 8 | + Node left; |
| 9 | + Node right; |
| 10 | + public Node(int val){ |
| 11 | + this.val = val; |
| 12 | + } |
| 13 | + public int getVal(){ |
| 14 | + return val; |
| 15 | + } |
| 16 | + } |
| 17 | + public Node root; |
| 18 | + public int height(Node node){ |
| 19 | + if(node==null){ |
| 20 | + return -1; |
| 21 | + } |
| 22 | + return node.height; |
| 23 | + } |
| 24 | + public boolean isEmpty(){ |
| 25 | + return root==null; |
| 26 | + } |
| 27 | + public void insert(int val){ |
| 28 | + root = insert(root,val); |
| 29 | + } |
| 30 | + public Node insert(Node node,int val){ |
| 31 | + if(node==null){ |
| 32 | + node = new Node(val); |
| 33 | + return node; |
| 34 | + } |
| 35 | + if(node.val>val){ |
| 36 | + node.left = insert(node.left,val); |
| 37 | + } |
| 38 | + if(node.val<val) { |
| 39 | + node.right = insert(node.right, val); |
| 40 | + } |
| 41 | + node.height = Math.max(height(node.left),height(node.right))+1; |
| 42 | + return rotate(node); |
| 43 | + } |
| 44 | + private Node rotate(Node node){ |
| 45 | + if(height(node.left)-height(node.right)>1){ |
| 46 | + //left heavy |
| 47 | + if(height(node.left.left)-height(node.left.right)>0){ |
| 48 | + return rightRotate(node); |
| 49 | + } |
| 50 | + if(height(node.left.left)-height(node.left.right)<0){ |
| 51 | + node.left = leftRotate(node.left); |
| 52 | + return rightRotate(node); |
| 53 | + } |
| 54 | + } |
| 55 | + if((height(node.left) - height(node.right)) < -1){ |
| 56 | + //right heavy |
| 57 | + if(height(node.right.left)-height(node.right.right)<0){ |
| 58 | + return leftRotate(node); |
| 59 | + } |
| 60 | + if(height(node.right.left)-height(node.right.right)>0){ |
| 61 | + node.right = rightRotate(node.right); |
| 62 | + return leftRotate(node); |
| 63 | + } |
| 64 | + } |
| 65 | + return node; |
| 66 | + } |
| 67 | + public Node rightRotate(Node p){ |
| 68 | + Node c = p.left; |
| 69 | + Node t = c.right; |
| 70 | + c.right = p; |
| 71 | + p.left = t; |
| 72 | + |
| 73 | + p.height = Math.max(height(p.left),height(p.right))+1; |
| 74 | + c.height = Math.max(height(c.left),height(c.right))+1; |
| 75 | + |
| 76 | + return c; |
| 77 | + } |
| 78 | + public Node leftRotate(Node p){ |
| 79 | + Node c = p.right; |
| 80 | + Node t = c.left; |
| 81 | + c.left = p; |
| 82 | + p.right = t; |
| 83 | + |
| 84 | + p.height = Math.max(height(p.left),height(p.right))+1; |
| 85 | + c.height = Math.max(height(c.left),height(c.right))+1; |
| 86 | + return c; |
| 87 | + } |
| 88 | + public void display(){ |
| 89 | + display2D(root,0); |
| 90 | + } |
| 91 | + public void display2D(Node root, int space){ |
| 92 | + if(root==null){ |
| 93 | + return; |
| 94 | + } |
| 95 | + space+=count; |
| 96 | + display2D(root.right,space); |
| 97 | + System.out.println(); |
| 98 | + for(int i=count;i<space;i++){ |
| 99 | + System.out.print(" "); |
| 100 | + } |
| 101 | + System.out.println(root.val); |
| 102 | + display2D(root.left,space); |
| 103 | + } |
| 104 | + public void populateSorted(int[] nums){ |
| 105 | + populateSorted(nums,0,nums.length); |
| 106 | + } |
| 107 | + public void populateSorted(int[] nums,int s,int e){ |
| 108 | + if(s>=e){ |
| 109 | + return ; |
| 110 | + } |
| 111 | + int mid = (s+e)/2; |
| 112 | + this.insert(nums[mid]); |
| 113 | + populateSorted(nums,s,mid); |
| 114 | + populateSorted(nums,mid+1,e); |
| 115 | + } |
| 116 | + public boolean isBalanced(Node node){ |
| 117 | + if(node==null){ |
| 118 | + return true; |
| 119 | + } |
| 120 | + return Math.abs(height(node.left)-height(node.right))<=1 && isBalanced(node.left) && isBalanced(node.right); |
| 121 | + } |
| 122 | +} |
0 commit comments