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

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

View Full Document Right Arrow Icon
Binary Search Trees (continued)
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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.
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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.
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern