cmsc 341 lecture 14
DESCRIPTION
CMSC 341 Lecture 14. Bottom Up Implementation. Perform insertion; new node is red Check for balance violations Are all nodes red or black? Is every NULL pointer black? If a node is red, are both of its children black? - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/1.jpg)
CMSC 341Lecture 14
![Page 2: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/2.jpg)
Bottom Up ImplementationPerform insertion; new node is redCheck for balance violations
– Are all nodes red or black?– Is every NULL pointer black?– If a node is red, are both of its children black?– Does every path from a node to a descendent leaf
contain the same number of black nodes?If necessary, make adjustments starting at altered node
![Page 3: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/3.jpg)
Bottom Up Insertion (cont)Insert node; X is pointer to itCases
0: X is the root -- color it black1: Both parent and uncle are red -- color parent and uncle
black, color grandparent red, point X to grandparent, check new situation
2: Parent is red, but uncle is black. X and its parent are both left or both right children -- color parent black, color grandparent red, rotate right on grandparent
3: Parent is red, but uncle is black. X and its parent are opposite type children -- color grandparent red, color X black, rotate left on parent, rotate right on grandparent
![Page 4: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/4.jpg)
Title:redblack_insert_flowchart.epsCreator:fig2dev Version 3.2 Patchlevel 0-beta2Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 5: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/5.jpg)
Title:redblack_insert_example.epsCreator:fig2dev Version 3.2 Patchlevel 0-beta2Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 6: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/6.jpg)
Reorienttemplate <class Comparable>void RedBlackTree<Comparable>::handleReorient (const
Comparable & item) {current->color = RED;current->left->color = current->right->color = BLACK;if (parent->color == RED) {grand->color = RED;if ((item < grand->element) != (item < parent->element)parent = rotate(item, grand); // start dbl rotatecurrent = rotate(item, great); current->color = BLACK;}header->right->color = BLACK; // make root black}
![Page 7: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/7.jpg)
Title:red_black_bottomup_deletion.epsCreator:fig2dev Version 3.2 Patchlevel 1Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 8: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/8.jpg)
Title:redblack_bottomup_delete_example1.epsCreator:fig2dev Version 3.2 Patchlevel 1Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 9: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/9.jpg)
Rotatetemplate <class Comparable>RedBlackNode<Comparable> *RedBlackTree<Comparable>::rotate(const
Comparable &item, RedBlackNode<Comparable> *theParent) const; {if (item < theParent->element) {if (item < theParent->left->element) rotateWithLeftChild(theParent->left) // LLelserotateWithRightChild(theParent->left) // LRreturn theParent->left;}else {if (item < theParent->right->element)rotateWithLeftChild(theParent->right) // RLelse rotateWithRightChild(theParent->right) // RRreturn theParent->right;}}
![Page 10: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/10.jpg)
Asymptotic CostO(lg n) to descend to insertion pointO(1) to do insertionO(lg n) to ascend and readjust -- worst case only for case 1
Total: O(lg n)
![Page 11: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/11.jpg)
Bottom Up DeletionNormal BST deletion
0: if node is leaf, just delete1: if node has one child, replace it with child2: if node has two children, replace value at node by value
of its inorder predecessor, then recursively delete inorder predecessor
Eventually case 0 or 1 will be reached, where node is replaced by another node V. If node to be deleted then is red, no adjustments are needed. If black, adjustments must be made to preserve number of black nodes.
![Page 12: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/12.jpg)
Bottom Up Deletion (cont)Cases
0: If S, sibling of V, is red. Do rotation at recoloring to produce a situation that can be handled as another.
1: Node S is black and both its children are black. Color S red. If P is red, make P black and terminate. Otherwise, check again and continue.
2: Node S is black and its right child is red. Do a rotation followed by a color swap between S and P. Now done.
3: Node S is black, its left child is red, and its right child is black. Do a rotation around S and a recoloring. The situation can now be handled as 2.
![Page 13: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/13.jpg)
Title:redblack_bottomup_delete_example2.epsCreator:fig2dev Version 3.2 Patchlevel 1Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 14: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/14.jpg)
Title:redblack_bottomup_delete_example3.epsCreator:fig2dev Version 3.2 Patchlevel 1Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 15: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/15.jpg)
Top Down ImplementationMake adjustments on way down to site for operation (insert,
remove)Perform operation
![Page 16: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/16.jpg)
Inserttemplate <class Comparable>void RedBlackTree<Comparable>::insert(const Comparable
&x) {current = parent = grand = header;nullNode->element = x;while (current->element != x) {great = grand; grand = parent; parent = current;current = (x < current->element) ? current->left : current->right;// if two red children, fixif ((current->left->color == RED)&&( current->right->color == RED))handleReorient(x);}
![Page 17: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/17.jpg)
Insert (cont)// insertion fails if already presentif (current != nullNode) return;current = new RedBlackNode<Comparable>
(x, nullNode, nullNode);
// attach to parentif (x < parent->element)
parent->left = current;else
parent->right = current;handleReorient(x);}
![Page 18: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/18.jpg)
Title:red_black_topdown_delete.epsCreator:fig2dev Version 3.2 Patchlevel 1Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 19: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/19.jpg)
Title:red_black_topdown_delete_example.epsCreator:fig2dev Version 3.2 Patchlevel 1Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.
![Page 20: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/20.jpg)
Proof: # Internal NodesTheorem 1: Any red-black tree, with root x, has at least n =
2bh(x)-1 internal nodes, where bh(x) is the black-height of node x.
Proof: by induction on height of x– Base: x is a leaf, height is zero, bh(x) = 0, 20 - 1 = 0– Induction: Let x be an internal node with two children. If a child of
x is red, its black height is bh(x). If the child is black, its black height is bh(x)-1. In any event, the IH holds for the children since their height is less than that of x. Therefore, each child subtree has at least 2bh(x)-1-1 internal nodes. Therefore, the tree rooted at x has at least
2(2bh(x)-1-1)+1=2bh(x)-1internal nodes.
![Page 21: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/21.jpg)
Proof: # BlackTheorem 2: In a red-black tree, at least half of the nodes on
any path from root to a leaf must be black.Proof: Since no red node can have a red child, the maximum
number of red nodes in any path from root to leaf will be every other node.
![Page 22: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/22.jpg)
Proof: Path LengthTheorem 3: In a red-black tree, no path from any node N to a
leaf is more than twice as long as any other path from N to any other leaf.
Proof: By definition, every path from a node to a leaf contains the same number of black nodes. By Theorem 2, at least half of the nodes on any such path must be black. Therefore, there can be no more than twice as many red nodes on any path from a node to a leaf. Therefore, the length of every path is no more than twice as long as that of any other path.
![Page 23: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/23.jpg)
Proof: HeightTheorem 4: A red-black tree with n internal nodes has height
h 2 lg(n+1)Proof: Let h be the height of the red-black tree. By theorem 2,
bh(x) h/2. Therefore,n 2h/2 -1n - 1 2h/2 lg (n-1) h/22 log(n-1) h
![Page 24: CMSC 341 Lecture 14](https://reader036.vdocument.in/reader036/viewer/2022062501/56816791550346895ddcc3b5/html5/thumbnails/24.jpg)
Title:red_black_topdown_insert.epsCreator:fig2dev Version 3.2 Patchlevel 1Preview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.