lecture-15

There is more than one we can choose either the left

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: nd instead replace the item in the node with another node’s item D D Remove B B A ? C A CPSC 223  ­ ­ Fall 2010 C 8 4 10/21/10 Removing Nodes Which item do we then use as a replacement? –  There is more than one we can choose … –  Either the left-most node of the right subtree –  or the right-most node of the left subtree D B A D Remove B C D ? C A C A Here, the right subtree does not have a left-most node CPSC 223  ­ ­ Fall 2010 9 Removing Nodes Which item do we then use as a replacement? –  There is more than one we can choose … –  Either the left-most node of the right subtree –  or the right-most node of the left subtree E B A F D C E Remove B ? A F D C E right child C A F D le+ ­most node A more typical example CPSC 223  ­ ­ Fall 2010 10 5 10/21/10 Removing Nodes •  The left-most node of the right subtree is often referred to as the “inorder successor” –  The node visited next in an inorder traversal E The inorder successor of B is C B A F D right child le+ ­most node C CPSC 223  ­ ­ Fall 2010 11 Removing Nodes •  To find the inorder successor … –  Go to the right child –  Then go left until you reach a node without a left child Oops … E has a right child! D ? B A G C E B Remove D H A F copy item C G E H F CPSC 223  ­ ­ Fall 2010 12 6 10/21/10 Removing Nodes •  Remove the inorder successor … –  It will either be a leaf or have one right child (why?) –  Now we simply remove it … Oops … E has a right child! ? B A E copy item C G E B H A G C F H F CPSC 223  ­ ­ Fall 2010 13 Removing Nodes Should we use recursion or iteration to find the inorder successor? –  Typically use iteration –  By looping until the left most node is found –  Recall: Navigating a path is easy using iteration (loops) CPSC 223  ­ ­ Fall 2010 14 7 10/21/10 Exercise: Removing Nodes •  Form groups of 2 –  Your job is to write down the steps for remove void remove(const Key& key) –  Re...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 223 at Gonzaga.

Ask a homework question - tutors are online