Course Hero Logo

Which adds substantial overhead and does not reduce

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 6 - 8 out of 10 pages.

which adds substantial overhead and does not reduce the number of cases to be handled.The code on the next page is a full implementation ofdelete()for LLRB 2-3 trees. It is basedon the reverse of the approach used forinsertin top-down 2-3-4 trees: we perform rotations andcolor flips on the way down the search path to ensure that the search does not end on a 2-node, sothat we can just delete the node at the bottom. We use the methodfixUp()to share the code for thecolor flip and rotations following therecursive calls in theinsert()code.WithfixUp(), we can leave right-leaning red links and unbalanced4-nodes along the search path, securethat these conditions will be fixed onthe way up the tree. (The approach isalso effective 2-3-4 trees, but requiresan extra rotation when the right nodeoff the search path is a 4-node.)As a warmup, consider thede-lete-the-minimumoperation, wherethe goal is to delete the bottom nodeon the left spine while maintainingbalance. To do so, we maintain the in-variant that the current node or its leftchild is red. We can do so by movingto the left unless the current node isred and its left child and left grandchild are both black. In that case, we can do a color flip, whichrestores the invariant but may introduce successive reds on the right. In that case, we can correctthe condition with two rotations and a color flip. These operations are implemented in themov-eRedLeft()method on the next page. WithmoveRedLeft(), the recursive implementation ofdele-teMin()above is straightforward.For general deletion, we also needmoveRedRight(), which is similar, but simpler, and we needto rotate left-leaning red links to the right on the search path to maintain the invariant. If the nodeto be deleted is an internal node, we replace its key and value fields with those in the minimumnode in its right subtree and then delete the minimum in the right subtree (or we could rearrangepointers to use the node instead of copying fields). The full implementation ofdelete()that der-vies from this discussion is given on the facing page. It uses one-third to one-quarter the amountof code found in typical implementations. It has been demonstrated before [2, 11, 13] that main-taining a field in each node containing its height can lead to code fordeletethat is similarly concise,but that extra space is a high price to pay in a practical implementation. With LLRB trees, we canarrange for concise code having a logarithmic performance guarantee and using no extra space.public void deleteMin(){root = deleteMin(root);root.color = BLACK;}private Node deleteMin(Node h){if (h.left == null) return null;if (!isRed(h.left) && !isRed(h.left.left))h = moveRedLeft(h);h.left = deleteMin(h.left);return fixUp(h);}Delete-the-minimum code for LLRB 2-3 trees
private Node moveRedLeft(Node h){colorFlip(h);if (isRed(h.right.left)){h.right = rotateRight(h.right);h = rotateLeft(h);colorFlip(h);}return h;}private Node moveRedRight(Node h){colorFlip(h);if (isRed(h.left.left)){h = rotateRight(h);colorFlip(h);}return h;}public void delete(Key key){root = delete(root, key);root.color = BLACK;}private Node delete(Node h, Key key){if (key.compareTo(h.key) < 0){

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 10 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
Staff
Tags
LG, Red black tree, Robert Sedgewick

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture