|
| 1 | + /* Class node is defined as : |
| 2 | + class Node |
| 3 | + int val; //Value |
| 4 | + int ht; //Height |
| 5 | + Node left; //Left child |
| 6 | + Node right; //Right child |
| 7 | +
|
| 8 | + */ |
| 9 | + |
| 10 | +static Node insert(Node root,int val) { |
| 11 | + Node newNode = new Node(); |
| 12 | + newNode.val = val; |
| 13 | + newNode.ht = 0; |
| 14 | + newNode.left = null; |
| 15 | + newNode.right = null; |
| 16 | + |
| 17 | + if (root==null) { |
| 18 | + root = newNode; |
| 19 | + } else { |
| 20 | + root=avlinsert(newNode, root); |
| 21 | + } |
| 22 | + |
| 23 | + return root; |
| 24 | +} |
| 25 | + |
| 26 | +// return height of tree rooted at x |
| 27 | +static public int height(Node x) { |
| 28 | + if (x == null) return -1; |
| 29 | + else return x.ht; |
| 30 | +} |
| 31 | + |
| 32 | +static public Node rotatewithleft(Node c) { |
| 33 | + Node p; // left child of c |
| 34 | + |
| 35 | + p = c.left; |
| 36 | + c.left = p.right; |
| 37 | + p.right = c; |
| 38 | + |
| 39 | + c.ht = Math.max(height(c.left), height(c.right)) + 1; |
| 40 | + p.ht = Math.max(height(p.left), height(p.right)) + 1; |
| 41 | + |
| 42 | + return p; |
| 43 | +} |
| 44 | + |
| 45 | +static public Node rotatewithright(Node c) { |
| 46 | + Node p; // right child of c |
| 47 | + |
| 48 | + p = c.right; |
| 49 | + c.right = p.left; |
| 50 | + p.left = c; |
| 51 | + |
| 52 | + c.ht = Math.max(height(c.left), height(c.right)) + 1; |
| 53 | + p.ht = Math.max(height(p.left), height(p.right)) + 1; |
| 54 | + |
| 55 | + return p; |
| 56 | +} |
| 57 | + |
| 58 | +static public Node doublerotatewithleft(Node c) { |
| 59 | + Node tmp; |
| 60 | + |
| 61 | + c.left = rotatewithright(c.left); |
| 62 | + tmp = rotatewithleft(c); |
| 63 | + |
| 64 | + return tmp; |
| 65 | +} |
| 66 | + |
| 67 | +static public Node doublerotatewithright(Node c) { |
| 68 | + Node tmp; |
| 69 | + |
| 70 | + c.right = rotatewithleft(c.right); |
| 71 | + tmp = rotatewithright(c); |
| 72 | + |
| 73 | + return tmp; |
| 74 | +} |
| 75 | + |
| 76 | +static public Node avlinsert(Node newNode, Node par) { |
| 77 | + Node newpar = par; // root of subtree par |
| 78 | + |
| 79 | + if (newNode.val < par.val) { |
| 80 | + if (par.left == null) { |
| 81 | + par.left = newNode; //attach new node as leaf |
| 82 | + } |
| 83 | + else { |
| 84 | + par.left = avlinsert(newNode, par.left); // branch left |
| 85 | + if ((height(par.left) - height(par.right)) == 2) { |
| 86 | + if (newNode.val < par.left.val) { |
| 87 | + newpar=rotatewithleft(par); |
| 88 | + } else { |
| 89 | + newpar=doublerotatewithleft(par); |
| 90 | + } //else |
| 91 | + } //if |
| 92 | + } // else |
| 93 | + } // if |
| 94 | + else if (newNode.val > par.val) { // branch right |
| 95 | + if (par.right == null) { |
| 96 | + par.right = newNode; // attach new node as leaf |
| 97 | + } else { |
| 98 | + par.right = avlinsert(newNode, par.right); // branch right |
| 99 | + if ((height(par.right) - height(par.left)) == 2) { |
| 100 | + if (newNode.val > par.right.val) { |
| 101 | + newpar=rotatewithright(par); |
| 102 | + } //if |
| 103 | + else { |
| 104 | + newpar=doublerotatewithright(par); |
| 105 | + } //else |
| 106 | + } //if |
| 107 | + } //else |
| 108 | + } // else if |
| 109 | + |
| 110 | + // Update the height, just in case |
| 111 | + |
| 112 | + if ((par.left == null) && (par.right != null)) |
| 113 | + par.ht = par.right.ht + 1; |
| 114 | + else if ((par.right == null) && (par.left != null)) |
| 115 | + par.ht = par.left.ht + 1; |
| 116 | + else |
| 117 | + par.ht = Math.max(height(par.left), height(par.right)) + 1; |
| 118 | + |
| 119 | + return newpar; // return new root of this subtree |
| 120 | + } |
0 commit comments