9 so we have void deleteleafvoid data nodeptr root int

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: ta, nodePtr& root) { int Result = TreeDataCmp(data,root->data); TreeDataCmp(data,rootif (Result == 0) { delete root; root = 0; 0; } else if (Result < 0 && root->left != 0) rootDeleteLeaf(data,rootDeleteLeaf(data,root->left); else if (Result > 0 && root->right != 0) rootDeleteLeaf(data,rootDeleteLeaf(data,root->right); So what if a node to be deleted has children? Consider this example. 42 67 28 34 15 89 45 } Unfortunately we don't know it's a leaf! Let's try to delete a node with one child. Replace it with its child. 20 36 30 Let's try to delete a node with one child. Replace it with its child. 42 42 67 28 34 15 20 89 45 34 20 36 30 67 28 36 30 But suppose we want to delete 28. But suppose we want to delete 28. Here's how we do it in a systematic way. 42 42 67 28 34 15 20 30 45 36 89 45 67 28 89 34 15 20 30 45 89 36 10 1. Find the node's predecessor in the inorder traversal. It will be the right-most node in the rightleft subtree. 42 34 20 42 67 28 15 2. Attach the right subtree of...
View Full Document

Ask a homework question - tutors are online