42 34 20 42 67 28 15 2 attach the right subtree of the

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: the node to be deleted as the right subtree of the indicated node (it won't have a R child). 89 45 34 15 36 30 67 28 20 36 30 42 42 67 28 34 15 20 89 45 67 28 15 36 30 3. Now replace the deleted node by its child (it now only has one) 20 36 30 42 42 15 67 45 34 30 89 45 34 20 89 45 89 34 15 20 36 67 28 30 45 89 36 We can even delete the root this way. 11 42 28 34 15 20 30 34 15 28 20 30 36 67 36 67 45 45 89 Suppose the node we wish to delete has nodePtr nodePtr curr while its parent node has pointer parent. parent. if (curr->left == 0) // no left subtree (curr{ if (curr->right == 0) // no children (curr == 0) // no children { if (parent == 0) // curr is root root = 0; else if (parent->left == curr) (parentparentparent->left = 0; else parentparent->right = 0; } else if (curr->right == 0) // only left subtree (curronly { if (parent == 0) root = curr->left; currelse if (parent->left == curr) (parentparentparent->left = curr->left; currelse parentparent->right = curr->left; curr} else // has both subtrees 89 Let's see how we code these steps. if (curr->left == 0) // no left subtree (curr{ .... else // only right subtree { if (paren...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online