Download presentation
Presentation is loading. Please wait.
Published byViolet Jackson Modified over 5 years ago
1
Announcements Midterm will be given on 10/21/99
Extra help session at today & tomorrow at SN014 by TA Homework #4 is due next Tuesday Quiz #3 is given today Continue with lectures on R-B Trees UNC Chapel Hill M. C. Lin
2
Insertion in R-B Trees It can be done in O(lg n) time. Basic steps:
Use Tree-Insert from BST to insert a node x into T Color the node x red. Fix up the modified tree by re-coloring nodes and performing rotation to preserve R-B tree property. UNC Chapel Hill M. C. Lin
3
This routine runs in O(lg n) time.
UNC Chapel Hill M. C. Lin
4
RB-Insertion UNC Chapel Hill M. C. Lin
5
Example of RB-Insert Operation
UNC Chapel Hill M. C. Lin
6
RB-Insert UNC Chapel Hill M. C. Lin
7
RB-Insert For p[x] is a left child:
Case 1 (the child x, its parent and its uncle y are red): Push the pointers up the tree and change the colors, till x and y are of different colors. Case 2 (x and its parent are red, but y is black): if problem node is a right child, left rotate. Case 3 (x and its parent are red, but y is black): if problem node is a left child, right rotate. Symmetric case for p[x] as a right child UNC Chapel Hill M. C. Lin
8
Algorithm Analysis While loop only repeats if case 1 is executed, then the pointer x moves up the tree. The total number of times the while loop can be executed is O(h). The height of a R-B tree on n nodes is O(lg n), so Tree-Insert takes O(lg n) time. TOTAL: O(lg n) UNC Chapel Hill M. C. Lin
9
Review on Tree-Delete (T, x)
if x has no children case 0 then remove x if x has one child case 1 then make p[x] point to child if x has two children case 2 then swap x with its successor perform case 0 or case 1 to delete it UNC Chapel Hill M. C. Lin
10
RB-Delete Implementation: use sentinels to treat a NIL child of a node x as an ordinary node whose parent is x. (Sentinel is a dummy object that help simplify boundary conditions.) A minor modification of Tree-Delete: after splicing out a node, it calls RB-Delete-Fixup to change colors and performs rotations to restore red-black properties. Replace “NIL” by “nil[T]” line 7: p[x] p[y] is performed unconditionally RB-Delete-Fixup(T, x) UNC Chapel Hill M. C. Lin
11
UNC Chapel Hill M. C. Lin
12
Example of RB-Delete-Fixup
UNC Chapel Hill M. C. Lin
13
UNC Chapel Hill M. C. Lin
14
Analysis on RB-Delete Total procedure without RB-Delete-Fixup takes O(lg n) time RB-Delete-Fixup cases 1, 3 & 4 takes constant time case 2 takes at most O(lg n) time RB-Delete takes O(lg n) time All RB-tree operations take O(lg n) time UNC Chapel Hill M. C. Lin
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.