COP3502_24_BinaryTrees3 - Binary Trees: Deletion Computer...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Binary Trees: Deletion COP 3502 – Computer Science I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Trees: Deletion page 2 Binary Trees: Deletion Deletion From a Binary Search Tree Deleting nodes from a BST requires some thought There are 3 possible cases And we deal with each in a different fashion The 3 cases are: 1) Deleting of a leaf node 2) Deleting a node with one child 3) Deleting a node with two children We examine each case separately
Background image of page 2
Binary Trees: Deletion page 3 Binary Trees: Deletion Deleting a Leaf Node This one is pretty easy 10 14 6 4 2 8 12 18 16 Initial BST with node 12 marked for deletion 10 14 6 4 2 8 18 16 BST after deletion of node 12
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Trees: Deletion page 4 Binary Trees: Deletion Deleting a Leaf Node This one is pretty easy We start by identifying the parent of the node we wish to delete Which we actually do in ALL three cases We then just set the appropriate node to NULL: parent->left = NULL or parent->right = NULL So now instead of pointing to the toBeDeleted node The parent simply points to NULL
Background image of page 4
Binary Trees: Deletion page 5 Binary Trees: Deletion Deleting a Node with One Child This one is also not too complicated But does require more thought than deleting a leaf node Again, we start by finding the parent meaning, the parent of the node we want to delete The parent’s pointer to the node is changed to now point to the deleted node’s child This has the effect of lifting up the deleted nodes child by one level in the tree All great-great-…-great-grandchildren will lose one ‘great’ from their kinship designations
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Trees: Deletion page 6 Binary Trees: Deletion Deleting a Node with One Child parent->left = parent->left->left; 10 14 6 4 2 8 12 18 16 Initial BST with node 4 marked for deletion BST after deletion of node 4. Node 2 has taken the place of the deleted node 10 14 6 2 8 12 18 16
Background image of page 6
Binary Trees: Deletion page 7 Binary Trees: Deletion Deleting a Node with One Child The previous example illustrated when we delete a left node that has a left child Notice that it makes no difference whether the only child is a left child or a right child The next example illustrated when we delete a right node that has a right child Again, the deletion simply lifts up the subtree of the deleted node by one level The other possibilities is a left node that has a right child or a right node that has a left child
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Deletion page 8 Binary Trees: Deletion Deleting a Node with One Child parent->right = parent->right->right; Initial BST with node 14 marked for deletion BST after deletion of node 14. Node 18 has taken the place of the deleted node and the entire subtree moved up one level. 10
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/21/2011.

Page1 / 34

COP3502_24_BinaryTrees3 - Binary Trees: Deletion Computer...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online