1.
CS 16: Balanced Trees CS 16: Balanced Trees 2-3-4 Trees Revealed 2-3-4 Trees and Red- Black Trees • Nodes store 1, 2, or 3 keys and have 2, 3, or 4 children, respectively • All leaves have the same depth k beh nr a cd fg i lm p sx 1 -- log ( N + 1 ) ≤ height ≤ log ( N + 1 ) - 2erm 204 erm 205 CS 16: Balanced Trees CS 16: Balanced Trees 2-3-4 Tree Nodes Why 2-3-4? • Introduction of nodes with more than 1 key, • Why not minimize height by maximizing and more than 2 children children in a “d-tree”? • Let each node have d children so that we a2-node: get O(log d N) search time! Right? logdN = log N/log d • same as a binary node <a >a a b3-node: • 2 keys, 3 links <a >b >a <b • That means if d = N1/2, we get a height of 2 • However, searching out the correct child a b c on each level requires O(log N1/2) by4-node: binary search • 3 keys, 4 links • 2 log N1/2 = O(log N) which is not as good <a >c as we had hoped for! >a >b • 2-3-4-trees will guarantee O(log N) height <b <c using only 2, 3, or 4 children per nodeerm 206 erm 207
2.
CS 16: Balanced Trees CS 16: Balanced Trees Insertion into 2-3-4 Trees Top Down Insertion • In our way down the tree, whenever we • Insert the new key at the lowest internal reach a 4-node, we break it up into two 2- node reached in the search nodes, and move the middle element up into the parent node e e • 2-node becomes 3-node n fn g d dg dfg g d • 3-node becomes 4-node f dg d f g • Now we can perform the insertion using one of the fn previous two cases • Since we follow this method from the root down de g • What about a 4-node? to the leaf, it is called • We can’t insert another key! top down insertionerm 208 erm 209 CS 16: Balanced Trees CS 16: Balanced Trees Splitting the Tree g n As we travel down the tree, if we encounter any 4-node we will break it up into 2-nodes. This guarantees that we will never have the problem c t of inserting the middle element of a former 4- node into its parent 4-node. a fil pr x g c n t pr x Whoa, cowboy n a fil g c t g n Whoa, cowboy a fil pr x c t a fil pr xerm 210 erm 211
3.
CS 16: Balanced Trees CS 16: Balanced Trees n Time Complexity of Insertion g c t in 2-3-4 Trees Whoa, cowboy a fil pr x Time complexity: • A search visits O(log N) nodes n • An insertion requires O(log N) node splits • Each node split takes constant time g ci t • Hence, operations Search and Insert each take time O(log N) a f l pr x Notes: • Instead of doing splits top-down, we can n perform them bottom-up starting at the in- sertion node, and only when needed. This ci t is called bottom-up insertion. • A deletion can be performed by fusing nodes (inverse of splitting), and takes a fg l pr x O(log N) timeerm 212 erm 213 CS 16: Balanced Trees CS 16: Balanced Trees Beyond 2-3-4 Trees Red-Black Tree A red-black tree is a binary search tree with theWhat do we know about 2-3-4 Trees? following properties: • edges are colored red or black • Balanced Siskel • no two consecutive red edges on any root-leaf path • O(log N) search time • same number of black edges Ebert on any root-leaf path (= black height of the tree) • Different node structures Roberto • edges connecting leaves are blackCan we get 2-3-4 tree advantages in a binary Black Red Edge Edge tree format??? Welcome to the world of Red-Black Trees!!!erm 214 erm 215
4.
CS 16: Balanced Trees CS 16: Balanced Trees 2-3-4 Tree Evolution More Red-Black Tree PropertiesNote how 2-3-4 trees relate to red-black trees N # of internal nodes L # leaves (= N + 1) 2-3-4 H height Red-Black B black height Property 1: 2B ≤ N + 1 ≤ 4B or 1 Property 2: -- log ( N + 1 ) ≤ B ≤ log ( N + 1 ) - 2 Property 3: log ( N + 1 ) ≤ H ≤ 2 log ( N + 1 ) Now we see red-black trees are just a way of representing 2-3-4 trees! This implies that searches take time O(logN)!erm 216 erm 217 CS 16: Balanced Trees CS 16: Balanced TreesInsertion into Red-Black Trees Insertion - Plain and Simple 1.Perform a standard search to ﬁnd the leaf where the key should be added Let: 2.Replace the leaf with an internal node with n be the new node the new key p be its parent 3.Color the incoming edge of the new node g be its grandparent red 4.Add two new leaves, and color their Case 1: Incoming edge of p is black incoming edges black 5.If the parent had an incoming red edge, we now have two consecutive red edges! We No violation g must reorganize tree to remove that violation. What must be done depends on the sibling of the parent. p STOP! R R n Pretty easy, huh?G G Well... it gets messier...erm 218 erm 219
5.
CS 16: Balanced Trees CS 16: Balanced Trees Restructuring More RotationsCase 2: Incoming edge of p is red, and its sibling is black Similar to a right rotation, we can do a “left rotation”... Uh oh! g Much g Better! p p p p n g n g n nWe call this a “right rotation” • No further work on tree is necessary Simple, huh? • Inorder remains unchanged • Tree becomes more balanced • No two consecutive red edges!erm 220 erm 221 CS 16: Balanced Trees CS 16: Balanced Trees Double Rotations Last of the RotationsWhat if the new node is between its parent and And this would be called agrandparent in the inorder sequence? “right-left double rotation”We must perform a “double rotation”(which is no more difﬁcult than a “single” one) g n g n p p g p g p n n This would be called a “left-right double rotation”erm 222 erm 223
6.
CS 16: Balanced Trees CS 16: Balanced Trees Bottom-Up Rebalancing Summary of InsertionCase 3: Incoming edge of p is red and its sibling is also red • If two red edges are present, we do either • a restructuring (with a simple or double g g rotation) and stop, or • a promotion and continue p p • A restructuring takes constant time and is performed at most once. It reorganizes an off-balanced section of the tree. n n • Promotions may continue up the tree and are executed O(log N) times. • We call this a “promotion” • The time complexity of an insertion is • Note how the black depth remains un- O(logN). changed for all of the descendants of g • This process will continue upward beyond g if necessary: rename g as n and repeat.erm 224 erm 225 CS 16: Balanced Trees CS 16: Balanced Trees An Example A Cool ExampleStart by inserting “REDSOX” into an empty tree C E D R E O S D R X O S E X D R Now, let’s insert “C U B S”... C O S Xerm 226 erm 227
7.
CS 16: Balanced Trees CS 16: Balanced Trees E An Unbelievable Example U E D R D R C O S C O S Double Rotation X E X U D R E C O S D R X C O U Oh No! What should we do? S X Uerm 228 erm 229 CS 16: Balanced Trees CS 16: Balanced Trees A Beautiful Example E E B D R D R Rotation C O U C O U B S X S X E E D R C RWhatnow? C O U B D O U B S X S Xerm 230 erm 231
8.
CS 16: Balanced Trees CS 16: Balanced Trees E A Super Example E S C R C R B D O U Use the B D O U Bat-Promoter!! S X S X E E S C R C R BIFF! B D O U B D O U We could’veHoly Consecutive placed it on SRed Edges, Batman! S X X either side S Serm 232 erm 233 CS 16: Balanced Trees E C R Rotation B D O U S X S RThe SUN laband Red-Battrees are safe... E U ...for now!!! C O S X B D Serm 234
Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.
Be the first to comment