|
| 1 | +package DSA.Trees.BST; |
| 2 | + |
| 3 | +import DSA.Trees.BinaryTree; |
| 4 | + |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.Queue; |
| 7 | + |
| 8 | +public class RBTreeInsert{ |
| 9 | + enum Color{ |
| 10 | + Red,Black |
| 11 | + } |
| 12 | + |
| 13 | + Node rootTree; |
| 14 | + public static class Node{ |
| 15 | + Node left, right,parent; |
| 16 | + int key; |
| 17 | + Color color; |
| 18 | + Node(int key, Node parent){ |
| 19 | + left = right = null; |
| 20 | + this.parent = parent; |
| 21 | + this.key = key; |
| 22 | + color = Color.Red; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + public void insert(int key, Node root){ |
| 28 | + |
| 29 | + if(key<root.key){ |
| 30 | + if(root.left!=null) |
| 31 | + insert(key, root.left); |
| 32 | + else { |
| 33 | + root.left = new Node(key, root); |
| 34 | + correctInsertionOrder(root.left); |
| 35 | + } |
| 36 | + }else{ |
| 37 | + if(root.right!=null) |
| 38 | + insert(key, root.right); |
| 39 | + else { |
| 40 | + root.right = new Node(key, root); |
| 41 | + correctInsertionOrder(root.right); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public Node correctInsertionOrder(Node n){ |
| 47 | + if(n.parent==null){ |
| 48 | + n.color = Color.Black; |
| 49 | + return n; |
| 50 | + } |
| 51 | + if(n.parent.color == Color.Black){ |
| 52 | + return n; |
| 53 | + } |
| 54 | + |
| 55 | + if(getUncle(n) !=null && getUncle(n).color == Color.Red){ |
| 56 | + n.parent.color = Color.Black; |
| 57 | + getUncle(n).color = Color.Black; |
| 58 | + n.parent.parent.color = Color.Red; |
| 59 | + |
| 60 | + n.parent.parent = correctInsertionOrder(n.parent.parent); |
| 61 | + } else { |
| 62 | + if(checkLeft(n.parent)){ |
| 63 | + if(checkLeft(n)){ |
| 64 | + Node g =n.parent.parent; |
| 65 | + Node p = n.parent; |
| 66 | + Node u = getUncle(n); |
| 67 | + rightRotate(n.parent.parent); |
| 68 | + swapColor(g); |
| 69 | + swapColor(p); |
| 70 | + }else{ |
| 71 | + Node g =n.parent.parent; |
| 72 | + Node p = n.parent; |
| 73 | + Node u = getUncle(n); |
| 74 | + leftRotate(n.parent); |
| 75 | + rightRotate(n.parent.parent); |
| 76 | + swapColor(g); |
| 77 | + swapColor(n); |
| 78 | + } |
| 79 | + }else{ |
| 80 | + if(checkLeft(n)){ |
| 81 | + Node g =n.parent.parent; |
| 82 | + Node p = n.parent; |
| 83 | + Node u = getUncle(n); |
| 84 | + |
| 85 | + rightRotate(n.parent); |
| 86 | + leftRotate(n.parent.parent); |
| 87 | + swapColor(g); |
| 88 | + swapColor(n); |
| 89 | + |
| 90 | + }else{ |
| 91 | + Node g =n.parent.parent; |
| 92 | + Node p = n.parent; |
| 93 | + Node u = getUncle(n); |
| 94 | + |
| 95 | + leftRotate(n.parent.parent); |
| 96 | + swapColor(g); |
| 97 | + swapColor(p); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + return n; |
| 104 | + } |
| 105 | + |
| 106 | + void leftRotate(Node n){ |
| 107 | + Node temp = n.parent; |
| 108 | + n.parent = n.right; |
| 109 | + n.right = n.right.left; |
| 110 | + n.parent.left = n; |
| 111 | + n.parent.parent = temp; |
| 112 | + if(temp!=null) |
| 113 | + temp.right = n.parent; |
| 114 | + else{ |
| 115 | + rootTree = n.parent; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + void swapColor(Node n){ |
| 120 | + if(n.color == Color.Red) |
| 121 | + n.color=Color.Black ; |
| 122 | + else |
| 123 | + n.color=Color.Red; |
| 124 | + } |
| 125 | + |
| 126 | + boolean checkLeft(Node n){ |
| 127 | + return n.parent.left == n; |
| 128 | + } |
| 129 | + |
| 130 | + boolean checkRight(Node n){ |
| 131 | + return n.parent.right == n; |
| 132 | + } |
| 133 | + |
| 134 | + void rightRotate(Node n){ |
| 135 | + Node temp = n.parent; |
| 136 | + n.parent = n.left; |
| 137 | + n.left = n.left.right; |
| 138 | + n.parent.right =n; |
| 139 | + n.parent.parent = temp; |
| 140 | + if(temp!=null) |
| 141 | + temp.left = n.parent; |
| 142 | + else |
| 143 | + rootTree = n.parent; |
| 144 | + } |
| 145 | + |
| 146 | + Node getUncle(Node n){ |
| 147 | + if(n.parent.parent.left == n.parent) |
| 148 | + return n.parent.parent.right; |
| 149 | + |
| 150 | + return n.parent.parent.left; |
| 151 | + } |
| 152 | + |
| 153 | + void levelOrderTraversal(Node root){ |
| 154 | + if(root==null) |
| 155 | + return; |
| 156 | + Queue<Node> queue = new LinkedList<Node>(); |
| 157 | + queue.add(root); |
| 158 | + queue.add(null); |
| 159 | + while(!queue.isEmpty()){ |
| 160 | + Node it = queue.poll(); |
| 161 | + if(it==null){ |
| 162 | + if(!queue.isEmpty()){ |
| 163 | + queue.add(null); |
| 164 | + System.out.println(""); |
| 165 | + } |
| 166 | + }else { |
| 167 | + System.out.print(it.key + ", "+it.color+" "); |
| 168 | + if (it.left != null) |
| 169 | + queue.add(it.left); |
| 170 | + if (it.right != null) |
| 171 | + queue.add(it.right); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + public static void main(String[] args) { |
| 178 | + |
| 179 | + |
| 180 | + RBTreeInsert tree = new RBTreeInsert(); |
| 181 | + |
| 182 | + tree.rootTree = new Node(7,null); |
| 183 | + tree.rootTree.color = Color.Black; |
| 184 | + |
| 185 | + tree.insert(3, tree.rootTree); |
| 186 | + tree.insert(8,tree.rootTree); |
| 187 | + tree.insert(10,tree.rootTree); |
| 188 | + tree.insert(11,tree.rootTree); |
| 189 | + tree.insert(18,tree.rootTree); |
| 190 | + tree.insert(22,tree.rootTree); |
| 191 | + tree.insert(26,tree.rootTree); |
| 192 | + tree.insert(2,tree.rootTree); |
| 193 | + tree.insert(6,tree.rootTree); |
| 194 | + tree.insert(13,tree.rootTree); |
| 195 | + |
| 196 | + tree.levelOrderTraversal(tree.rootTree); |
| 197 | + |
| 198 | + } |
| 199 | +} |
0 commit comments