11package com .interview .tree ;
22
3+ import javax .swing .*;
34import java .util .Optional ;
5+ import java .util .concurrent .atomic .AtomicInteger ;
6+ import java .util .concurrent .atomic .AtomicReference ;
47
58/**
69 * Date 10/29/2015
@@ -30,24 +33,36 @@ static class Node {
3033 Node left ;
3134 Node right ;
3235 Node parent ;
36+ boolean isNullLeaf ;
3337 }
3438
3539 private static Node createBlackNode (int data ) {
3640 Node node = new Node ();
3741 node .data = data ;
3842 node .color = Color .BLACK ;
43+ node .left = createNullLeafNode (node );
44+ node .right = createNullLeafNode (node );
3945 return node ;
4046 }
4147
48+ private static Node createNullLeafNode (Node parent ) {
49+ Node leaf = new Node ();
50+ leaf .color = Color .BLACK ;
51+ leaf .isNullLeaf = true ;
52+ leaf .parent = parent ;
53+ return leaf ;
54+ }
55+
4256 private static Node createRedNode (Node parent , int data ) {
4357 Node node = new Node ();
4458 node .data = data ;
4559 node .color = Color .RED ;
4660 node .parent = parent ;
61+ node .left = createNullLeafNode (node );
62+ node .right = createNullLeafNode (node );
4763 return node ;
4864 }
4965
50-
5166 private void rightRotate (Node root , boolean changeColor ) {
5267 Node parent = root .parent ;
5368 root .parent = parent .parent ;
@@ -94,12 +109,12 @@ private void leftRotate(Node root, boolean changeColor) {
94109 }
95110 }
96111
97- private Optional <Node > findSiblingColor (Node root ) {
112+ private Optional <Node > findSiblingNode (Node root ) {
98113 Node parent = root .parent ;
99114 if (isLeftChild (root )) {
100- return Optional .ofNullable (parent .right );
115+ return Optional .ofNullable (parent .right . isNullLeaf ? null : parent . right );
101116 } else {
102- return Optional .ofNullable (parent .left );
117+ return Optional .ofNullable (parent .left . isNullLeaf ? null : parent . left );
103118 }
104119 }
105120
@@ -117,7 +132,7 @@ public Node insert(Node root, int data) {
117132 }
118133
119134 private Node insert (Node parent , Node root , int data ) {
120- if (root == null ) {
135+ if (root == null || root . isNullLeaf ) {
121136 //if parent is not null means tree is not empty
122137 //so create a red leaf node
123138 if (parent != null ) {
@@ -164,7 +179,7 @@ private Node insert(Node parent, Node root, int data) {
164179 if (root .color == Color .RED && root .left .color == Color .RED ) {
165180 //get the sibling of root. It is returning optional means
166181 //sibling could be empty
167- Optional <Node > sibling = findSiblingColor (root );
182+ Optional <Node > sibling = findSiblingNode (root );
168183 //if sibling is empty or of BLACK color
169184 if (!sibling .isPresent () || sibling .get ().color == Color .BLACK ) {
170185 //check if root is left child of its parent
@@ -202,7 +217,7 @@ private Node insert(Node parent, Node root, int data) {
202217 } else {
203218 //this is mirror case of above. So same comments as above.
204219 if (root .color == Color .RED && root .right .color == Color .RED ) {
205- Optional <Node > sibling = findSiblingColor (root );
220+ Optional <Node > sibling = findSiblingNode (root );
206221 if (!sibling .isPresent () || sibling .get ().color == Color .BLACK ) {
207222 if (!isLeftChild (root )) {
208223 leftRotate (root , true );
@@ -223,8 +238,148 @@ private Node insert(Node parent, Node root, int data) {
223238 return root ;
224239 }
225240
241+ public Node delete (Node root , int data ) {
242+ AtomicReference <Node > rootReference = new AtomicReference <>();
243+ delete (root , data , rootReference );
244+ if (rootReference .get () == null ) {
245+ return root ;
246+ } else {
247+ return rootReference .get ();
248+ }
249+ }
250+
251+ private void delete (Node root , int data , AtomicReference <Node > rootReference ) {
252+ if (root == null || root .isNullLeaf ) {
253+ return ;
254+ }
255+ if (root .data == data ) {
256+ if (root .right .isNullLeaf || root .left .isNullLeaf ) {
257+ deleteOneChild (root , rootReference );
258+ }
259+ Node inorderSuccessor = findSmallest (root .right );
260+ root .data = inorderSuccessor .data ;
261+ delete (root .right , inorderSuccessor .data , rootReference );
262+ }
263+ if (root .data < data ) {
264+ delete (root .right , data , rootReference );
265+ } else {
266+ delete (root .left , data , rootReference );
267+ }
268+ }
269+
270+ private Node findSmallest (Node root ) {
271+ Node prev = null ;
272+ while (root != null && root .isNullLeaf ) {
273+ prev = root ;
274+ root = root .left ;
275+ }
276+ return prev != null ? prev : root ;
277+ }
278+
279+ private void deleteOneChild (Node nodeToBeDelete , AtomicReference <Node > rootReference ) {
280+ Node child = nodeToBeDelete .right .isNullLeaf ? nodeToBeDelete .left : nodeToBeDelete .right ;
281+ replaceNode (nodeToBeDelete , child , rootReference );
282+ if (nodeToBeDelete .color == Color .BLACK ) {
283+ if (child .color == Color .RED ) {
284+ child .color = Color .BLACK ;
285+ } else {
286+ deleteCase1 (child , rootReference );
287+ }
288+ }
289+ }
290+
291+
292+ private void deleteCase1 (Node doubleBlackNode , AtomicReference <Node > rootReference ) {
293+ if (doubleBlackNode .parent == null ) {
294+ rootReference .set (doubleBlackNode );
295+ return ;
296+ }
297+ deleteCase2 (doubleBlackNode , rootReference );
298+ }
299+
300+ private void deleteCase2 (Node doubleBlackNode , AtomicReference <Node > rootReference ) {
301+ Node siblingNode = findSiblingNode (doubleBlackNode ).get ();
302+ if (siblingNode .color == Color .RED ) {
303+ if (isLeftChild (siblingNode )) {
304+ rightRotate (siblingNode , true );
305+ } else {
306+ leftRotate (siblingNode , true );
307+ }
308+ if (siblingNode .parent == null ) {
309+ rootReference .set (siblingNode );
310+ }
311+ }
312+ deleteCase3 (doubleBlackNode , rootReference );
313+ }
314+
315+ private void deleteCase3 (Node doubleBlackNode , AtomicReference <Node > rootReference ) {
316+
317+ Node siblingNode = findSiblingNode (doubleBlackNode ).get ();
318+
319+ if (doubleBlackNode .parent .color == Color .BLACK && siblingNode .color == Color .BLACK && siblingNode .left .color == Color .BLACK
320+ && siblingNode .right .color == Color .BLACK ) {
321+ siblingNode .color = Color .RED ;
322+ deleteCase1 (doubleBlackNode .parent , rootReference );
323+ } else {
324+ deleteCase4 (doubleBlackNode , rootReference );
325+ }
326+ }
327+
328+ private void deleteCase4 (Node doubleBlackNode , AtomicReference <Node > rootReference ) {
329+ Node siblingNode = findSiblingNode (doubleBlackNode ).get ();
330+ if (doubleBlackNode .parent .color == Color .RED && siblingNode .color == Color .BLACK && siblingNode .left .color == Color .BLACK
331+ && siblingNode .right .color == Color .BLACK ) {
332+ siblingNode .color = Color .RED ;
333+ doubleBlackNode .parent .color = Color .BLACK ;
334+ } else {
335+ deleteCase5 (doubleBlackNode , rootReference );
336+ }
337+ }
338+
339+ private void deleteCase5 (Node doubleBlackNode , AtomicReference <Node > rootReference ) {
340+ Node siblingNode = findSiblingNode (doubleBlackNode ).get ();
341+ if (siblingNode .color == Color .BLACK ) {
342+ if (isLeftChild (doubleBlackNode ) && siblingNode .right .color == Color .BLACK && siblingNode .left .color == Color .RED ) {
343+ rightRotate (siblingNode .left , true );
344+ } else if (!isLeftChild (doubleBlackNode ) && siblingNode .left .color == Color .BLACK && siblingNode .right .color == Color .RED ) {
345+ leftRotate (siblingNode .right , true );
346+ }
347+ }
348+ deleteCase6 (doubleBlackNode , rootReference );
349+ }
350+
351+ private void deleteCase6 (Node doubleBlackNode , AtomicReference <Node > rootReference ) {
352+ Node siblingNode = findSiblingNode (doubleBlackNode ).get ();
353+ siblingNode .color = siblingNode .parent .color ;
354+ siblingNode .parent .color = Color .BLACK ;
355+ if (isLeftChild (doubleBlackNode )) {
356+ siblingNode .right .color = Color .BLACK ;
357+ leftRotate (siblingNode , false );
358+ } else {
359+ siblingNode .left .color = Color .BLACK ;
360+ rightRotate (siblingNode , false );
361+ }
362+ if (siblingNode .parent == null ) {
363+ rootReference .set (siblingNode );
364+ }
365+ }
366+
367+ private void replaceNode (Node root , Node child , AtomicReference <Node > rootReference ) {
368+ child .parent = root .parent ;
369+ if (root .parent == null ) {
370+ rootReference .set (child );
371+ }
372+ else {
373+ if (isLeftChild (root )) {
374+ root .parent .left = child ;
375+ } else {
376+ root .parent .right = child ;
377+ }
378+ }
379+ }
380+
226381 private void printRedBlackTree (Node root , int space ) {
227- if (root == null ) {
382+ if (root == null || root . isNullLeaf ) {
228383 return ;
229384 }
230385 printRedBlackTree (root .right , space + 5 );
@@ -235,14 +390,58 @@ private void printRedBlackTree(Node root, int space) {
235390 printRedBlackTree (root .left , space + 5 );
236391 }
237392
393+ public boolean validateRedBlackTree (Node root ) {
394+
395+ if (root == null ) {
396+ return true ;
397+ }
398+ //check if root is black
399+ if (root .color != Color .BLACK ) {
400+ System .out .print ("Root is not black" );
401+ return false ;
402+ }
403+ AtomicInteger blackCount = new AtomicInteger (0 );
404+ //make sure black count is same on all path and there is no red red relationship
405+ return checkBlackNodesCount (root , blackCount , 0 ) && noRedRedParentChild (root , Color .BLACK );
406+ }
407+
408+ private boolean noRedRedParentChild (Node root , Color parentColor ) {
409+ if (root == null ) {
410+ return true ;
411+ }
412+ if (root .color == Color .RED && parentColor == Color .RED ) {
413+ return false ;
414+ }
415+
416+ return noRedRedParentChild (root .left , root .color ) && noRedRedParentChild (root .right , root .color );
417+ }
418+
419+ private boolean checkBlackNodesCount (Node root , AtomicInteger blackCount , int currentCount ) {
420+
421+ if (root .color == Color .BLACK ) {
422+ currentCount ++;
423+ }
424+
425+ if (root .left == null && root .right == null ) {
426+ if (blackCount .get () == 0 ) {
427+ blackCount .set (currentCount );
428+ return true ;
429+ } else {
430+ return currentCount == blackCount .get ();
431+ }
432+ }
433+ return checkBlackNodesCount (root .left , blackCount , currentCount ) && checkBlackNodesCount (root .right , blackCount , currentCount );
434+ }
435+
436+
238437 public void printRedBlackTree (Node root ) {
239438 printRedBlackTree (root , 0 );
240439 }
241440
242441 public static void main (String args []) {
243442 Node root = null ;
244443 RedBlackTree redBlackTree = new RedBlackTree ();
245- /* root = redBlackTree.insert(root, 10);
444+ root = redBlackTree .insert (root , 10 );
246445 root = redBlackTree .insert (root , 20 );
247446 root = redBlackTree .insert (root , 0 );
248447 root = redBlackTree .insert (root , 25 );
@@ -254,7 +453,7 @@ public static void main(String args[]) {
254453 root = redBlackTree .insert (root , 23 );
255454 root = redBlackTree .insert (root , 24 );
256455 root = redBlackTree .insert (root , 5 );
257- root = redBlackTree.insert(root, 4);*/
456+ root = redBlackTree .insert (root , 4 );
258457 root = redBlackTree .insert (root , 10 );
259458 root = redBlackTree .insert (root , 20 );
260459 root = redBlackTree .insert (root , -10 );
@@ -265,5 +464,6 @@ public static void main(String args[]) {
265464 root = redBlackTree .insert (root , 60 );
266465
267466 redBlackTree .printRedBlackTree (root );
467+ System .out .println (redBlackTree .validateRedBlackTree (root ));
268468 }
269469}
0 commit comments