RBTrees December 27, 2013

download RBTrees December 27, 2013

of 26

description

Red Black Trees Insertion, Deletion Presentation

Transcript of RBTrees December 27, 2013

PowerPoint Presentation

REDBLACK TREEPresented By:Mohammad Bilal MirzaIntroductionRed-black treeSelf-balancing binary search treeSearch, insertion, and deletion take O(log n) timeRelatively complexEfficient in practiceSelf-balancing binary search treeA BST designed to keep height as small as possibleUses operations such as tree rotation(avoiding T(n)=O(n))Red-Black PropertiesEach node in the tree must be colored either red or blackTo have a black root nodeLeaves of the tree should be blackBoth children of each red node must be blackEach path down from the root to leaf contain a same number of black node.These properties guarantee that the depth of the highest leaf in a red-black tree is no less than half the height of the tree.which is enough to ensure the height of the tree is O(log n)Red-Black Properties-42112561937nullnull14nullnullnullnullnullnullInsertion-42112561937nullnull14nullnullnullnullnullnull2828nullnullRecoloring and Rotation is Performed-1125619nullnull14nullnullnull4237nullnull28nullnullRecoloring and Rotation is Performed-1125619nullnull14nullnullnull4237nullnull28nullnullInsertionThere are five special cases for insertionIn each case, the tree is locally fixed, relative to some red node

InsertionCase 1(root)void insertCase1(Node n){If (n.parent == null) {n.isBlack == true ;} else {insertCase2(n);}}N12N12(root)InsertionCase 2void insertCase2(Node n){If (n.parent.isBlack) {return;} else {insertCase3(n);}}

P13N2InsertionCase 3void insertCase3(Node n){Node u = u.uncle();If (u != null && !u.isBlack) {n.parent.isBlack = true;u.isBlack = true;Node g = n.grandparent();g.isBlack = false;insertCase1(g);} else {insertCase4(n);}}

G12P3UN45G12P3UN45InsertionCase 4void insertCase4(Node n){Node g = n.grandparent();If (n == n.parent.right && n.parent == g.left){n.parent.rotateLeft();n = n.left;insertCase1(g);} else if (n == n.parent.left && n.parent == g.right){n.parent.rotateRight();n = n.right;}insertCase5(n);}

GP1U45N23GP1U45N23InsertionCase 5void insertCase5(Node n){Node g = n.grandparent();n.parent.isBlack = true;g.isBlack = false;If (n == n.parent.left ){g.rotationRight();} else {g.rotateLeft();}}

GN1U45P23PU44G31N52DeletionDeletion begins with ordinary BST deletionDeletionIf the node that has to be delete is red and its child is blackThen no problem arises when it is displaced.

PXN123PN123DeletionIf the node that has to be delete is black and its child is red.Then after displacing the node we can simply color its child black.PXN123PN123DeletionA problem when both the node is to be displaced and its child are black.In this case all paths down through the tree the paths through the node the path will pass no one fewer black node then the other path.

PXN123PN123DeletionThere are six special cases to locally fix this type of problem.DeletionCase 1(root)The current node is a rootvoid deleteCase1(Node n) {if (n.parent == null) {return;}else{deleteCase2(n);}}

N12DeletionCase 2 if the sibling of the current node is redvoid deleteCase2(Node n) {Node s = n.sibling();if (!s.isBlack) {n.parent.isBlack = false;s.isBlack = true;if(n == n.parent.left){n.parent.rotateLeft();} else{n.parent.rotateRight();}deleteCase3(n);}

PNSLR123456PNSLR123456DeletionCase 3 parents and its siblings are blackvoid deleteCase3(Node n) {Node s = n.sibling();if (n.parent.isBlack && s.isblack&& s.left.isblack && s.right.isblack) {s.isBlack = false;deleteCase1(n.parent);} else{deleteCase4(n);}}

PNSLR123456PNSLR123456DeletionCase 4Current node parent is red & sibling children are both blackvoid deleteCase4(Node n) {Node s = n.sibling();if (!n.parent.isBlack && s.isblack&& s.left.isblack && s.right.isblack) {s.isBlack = false;n.parent.isBlack = true;} else{deleteCase4(n);}}

PNSLR123456PNSLR123456DeletionCase 5 left child is red and right child is blackvoid deleteCase5(Node n) {Node s = n.sibling();if (n == n.parent.left &&s.isBlack && !s.left.isBlack &&s.right.isBlack ) {s.isBlack = false;s.left.isBlack = true;s.rotateRight();} else if (n == n.parent.right &&s.isBlack && !s.right.isBlack &&s.left.isBlack){s.isBlack = false;s.right.isBlack = true;s.rotateLeft();}deleteCase6(n);}SLR1234SLR1234DeletionCase 6void deleteCase6(Node n) {Node s = n.sibling();s.isBlack = n.parent.isBlack;n.parent.isBlack = true;if (n == n.parent.left) {s.right.isBlack = true;n.parent.rotateLeft();} else{s.left.isBlack = true;n.parent.rotateRight();}}

PNSR12345PNSR12345Thank You!