This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

Editors' Picks Books

Hand-picked favorites from

our editors

our editors

Editors' Picks Audiobooks

Hand-picked favorites from

our editors

our editors

Editors' Picks Comics

Hand-picked favorites from

our editors

our editors

Editors' Picks Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

Top-Down Deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that child 3. If vertex to be deleted has two children, replace the value of by it’s in-order predecessor’s value then delete the inorder predecessor (a recursive step)

**What can go wrong?
**

1. If the delete node is red? Not a problem – no RB properties violated 2. If the deleted node is black? If the node is not the root, deleting it will change the black-height along some path

**The goal of T-D Deletion
**

• To delete a red leaf • How do we ensure that’s what happens?

– As we traverse the tree looking for the leaf to delete, we change every node we encounter to red. – If this causes a violation of the RB properties, we fix it

**Bottom-Up vs. Top-Down
**

• Bottom-Up is recursive

– BST deletion going down the tree (winding up the recursion) – Fixing the RB properties coming back up the tree (unwinding the recursion)

– – – – –

Terminology

• Matching Weiss text section 12.2

X is the node being examined T is X’s sibling P is X’s (and T’s) parent R is T’s right child L is T’s left child

**• Top-Down is iterative
**

– Restructure the tree on the way down so we don’t have to go back up

• This discussion assumes X is the left child of P. As usual, there are left-right symmetric cases.

1

Proceed to step 2 2. T is Black X We are going to color X Red. to Red. T has 2 Black Children 2A2. it’s children are Black) Step 1 – Examine the root 1. X. P is Red. • When we change X to Red. Move X to the appropriate child of the root c. Make the root Red b. we can do either 2A2 or 2A3 Case 2A1 X and T have 2 Black Children P X T X P T X L Just recolor X. Otherwise designate the root as X and proceed to step 2B. we know – P is also Red (we just came from there) – T is black (since P is Red. we change every node we visit. then L around P Recolor X and P then continue down the tree L P T X P L1 L2 T 2 . If both of the root’s children are Black a. X has at least one Red child Case 2A X has two Black Children P T 2A1. T’s left child is Red 2A3. we continually encounter this situation until we reach the node to be deleted X is Black. X has 2 Black children 2B. Step 2 – the main case As we traverse down the tree.Basic Strategy • As we traverse the tree. P and T and move down the tree L1 L2 Case 2A2 X has 2 Black Children and T’s Left Child is Red Rotate L around T. T’s right child is Red ** if both of T’s children are Red. then recolor other nodes and possibly do rotation(s) based on the color of X’s and T’s children 2A.

continue down again If the new X is Black (T is Red. find the node to be deleted – a leaf or a node with one non-null child that is a leaf. P X T T P X Color the Root Black Step 4 If move to Black child (2B2) If move to the Red child (2B1) Rotate T around P. Descend down the tree. P. T and R then continue down the tree Case 2B X has at least one Red child Continue down the tree to the next level If the new X is Red. P is Black) Rotate T around P Recolor P and T Back to main case – step 2 P X T R X L R1 R2 T P R1 R R2 L Case 2B Diagram P X T Step 3 Eventually. arriving at 12. 3 . Delete the appropriate node as a Red leaf Move down the tree. moving X to 6 One of X’s children is Red (case 2B).Case 2A3 X has 2 Black Children and T’s Right Child is Red Rotate T around P Recolor X. Since the new X (12) is also Red (2B1). continue down the tree. arriving at 10. Color Root Red Descend the tree. Recolor P and T Move down again Back to step 2. the main case Example 1 Delete 10 from this RB Tree 15 6 3 10 7 12 13 16 18 17 20 23 3 Example 1 (cont’d) 15 X 6 12 10 13 16 17 20 18 23 7 Step 1 – Root has 2 Black children.

arriving at 10.Example 1 (cont’d) 15 6 3 10 7 12 16 13 18 17 20 23 3 Example 1 (cont’d) 15 6 12 7 13 16 18 17 20 23 X Step 3 -Since 10 is the node to be deleted. but it’s Black. Step 3 -. X’s sibling (3) also has 2 black children.Now delete 10 as a red leaf.Recolor the root black 4 . Case 2A1– recolor X. replace it’s value with the value of it’s only child (7) and delete 7’s red node The final tree after 7 has replaced 10 and 7’s red node deleted and (step 4) the root has been colored Black. continue down the tree. Set X = root and proceed to step 2 X has at least one Red child (case 2B). Proceed down the tree. Example 2 Delete 10 from this RB Tree 15 6 3 2 4 10 12 13 16 17 20 2 3 Example 2 (cont’d) X 6 12 4 10 13 16 15 17 20 Step 1 – the root does not have 2 Black children. Color the root red. P. Example 2 (cont’d) 15 6 3 2 Example 2 (cont’d) 15 6 17 P X 10 12 13 16 17 20 2 3 4 P X 10 12 16 13 20 T 4 T X has 2 Black children. Since 6 is also Red (case 2B1). so back to step 2. arriving at 6. Step 4 -. P and T. X has 2 Black children and T has 2 Black children – case 2A1 Recolor X. and T and continue down the tree. X is now the leaf to be deleted. arriving at 12.

but it’s Black. Recolor X. Since we arrived at a black node (case 2B2) assuring T is red and P is black). Step 4 -. Step 3 -. the leaf to be deleted. recolor T and P Back to step 2 Example 3 (cont’d) 15 Example 3 (cont’d) 15 5 5 10 3 2 4 7 6 P T 9 11 10 X 12 3 2 13 4 7 6 9 11 P 12 X 13 T Now X is Black with Red parent and Black sibling. P and T and continue traversal Having traversed down the tree. so back to step 2. Color Root red. P and T.Example 2 Solution 15 6 3 2 4 12 13 2 16 17 20 Example 3 Delete 11 from this RB Tree 15 10 5 3 4 7 6 9 11 12 13 Valid and unaffected Right subtree Step 1 – root has 2 Black children. rotate T around P.delete 11 as a red leaf. X and T both have 2 Black children (case 2A1) Just recolor X. arriving at 12. we arrive at 11. X and T both have two Black children.Recolor the root black 5 . Set X to appropriate child of root (10) Example 3 (cont’d) 15 10 5 3 2 4 7 6 9 11 Example 3 (cont’d) 15 10 X 12 13 2 3 4 5 P X 11 12 13 T 7 6 9 X has one Red child (case 2B) Traverse down the tree.

Example 3 Solution 15 5 10 3 2 4 7 6 9 12 13 6 .

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd