lecture-15

# Lecture 15

• 17

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10/21/10 CPSC 223 Algorithms & Data Abstract Structures Lecture 15: Binary Search Tree Deletion (cont.), Treesort, and Heaps Today … •  Assignments –  Assignment 6 due (don’t forget about the destructor!) –  Assignment 7 •  Binary Search Tree Deletion (cont.) •  Treesort [pp. 568-569] •  Heaps (if time) [Sect. 11.2] CPSC 223  ­ ­ Fall 2010 2 1 10/21/10 Binary Search Tree Deletion CPSC 223  ­ ­ Fall 2010 3 Removing (Deleting) Nodes •  Removing nodes is where things get tricky … •  If we could restrict remove to just leaf nodes it would be easy •  For general removal, we need to consider some special cases … CPSC 223  ­ ­ Fall 2010 4 2 10/21/10 Removing Nodes Cases to consider … –  The tree is empty! –  The item to be removed is not in the tree –  The node containing the item is a leaf node –  The node containing the item has one child –  The node containing the item has two children CPSC 223  ­ ­ Fall 2010 5 Removing Nodes •  To remove a leaf node –  We delete the node –  And set the left or right pointer in the parent to NULL •  To remove a node with one child –  The parent of the node to be deleted “adopts” the child B Remove A C B A A C C Remove A C B B CPSC 223  ­ ­ Fall 2010 6 3 10/21/10 Removing Nodes •  Hardest Case: Remove a node with two children –  Both children cannot be “adopted” by the parent D D Remove B ? B A C A C CPSC 223  ­ ­ Fall 2010 7 Removing Nodes •  To remove a node with two children –  one solution is to not delete the node … –  a...
View Full 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