05-MoreBinaryTrees

# 05-MoreBinaryTrees - Binary Search Trees(continued Deletion...

This preview shows pages 1–11. Sign up to view the full content.

Binary Search Trees (continued)

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

View Full Document
Deletion from a BST Inserting into a BST is relatively simple when compared to deletion The reason is that insertions compared to deletion. always occur at the leaves. Deletion of leaf nodes is also easy -- the interior (non-leaf) nodes are is also easy the interior (non leaf) nodes are what present difficulties. Hard to delete Easy to delete
Deleting a Root Node The general problem is to determine what value should replace the deleted value in the root of a non-empty subtree (i.e., a non-leaf node). left 4 right left 2 left 6 right right left 1 right left 3 right left 5 right left 7 right

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

View Full Document
BST Conditions on the Replacement Value If the number 4 is deleted, it must be replaced with a value that is greater than everything in the left subtree and not greater than anything in the right subtree. left 4 right left 2 left 6 right right left 1 right left 3 right left 5 right left 7 right
The Inorder Predecessor The inorder predecessor of a value is the largest value that is less than it The inorder predecessor value that is less than it. in the below example is a suitable replacement. left 4 right left 2 left 6 right right The inorder predecessor of 4 left 1 right left 3 right left 5 right left 7 right of 4.

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

View Full Document
The Inorder Predecessor The inorder predecessor of a value is the largest value that is less than it The inorder predecessor value that is less than it. in the below example is a suitable replacement. left 3 right left 2 left 6 right right left 1 right left 5 right left 7 right
The Inorder Successor The inorder successor of a value is the smallest value that is not less than it The inorder successor value that is not less than it. The inorder successor is a suitable replacement. left 4 right left 2 left 6 right right The inorder successor of 4 left 1 right left 3 right left 5 right left 7 right of 4.

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

View Full Document
The Inorder Successor The inorder successor of a value is the smallest value that is not less than it The inorder successor value that is not less than it. The inorder successor is a suitable replacement. left 5 right left 2 left 6 right right left 1 right left 3 right left 7 right
Datasets with Duplicates Suppose the dataset can have duplicates. If the BST condition requires that keys in the left subtree are less than the key in the root, do we need to use the inorder predecessor, successor, or does it matter? left 4 right left 2 left 6 right right left 1 right left 3 right left 5 right left 7 right

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

View Full Document
Datasets with Duplicates If the less-than inequality is used as the BST condi- tion then the inorder successor must be used tion, then the inorder successor must be used. left 4 right left 2 left 6 Moving 2 (inorder predecessor of 4) into the root will cause a violation of the BST condition right right the BST condition.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern