14. BinarySearchTrees_outside

Visits the keys in increasing order 6 9 2 4 1 8 2010

This preview shows pages 5–9. Sign up to view the full content.

visits the keys in  increasing order 6 9 2 4 1 8 ©  2010 Goodrich, Tamassia

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

View Full Document
Binary Search Trees 6 Search To search for a key  k , we  trace a downward path  starting at the root The next node visited  depends on the  comparison of  k  with the  key of the current node If we reach a leaf, the key  is not found Example:  get (4): Call TreeSearch(4,root) The algorithms for  floorEntry  and  ceilingEntry  are similar Algorithm TreeSearch ( k , v ) if v.isExternal () return v if k < v.key () return TreeSearch ( k , v.left ()) else if k = v.key () return v else { k v.key () } return TreeSearch ( k , v.right ()) 6 9 2 4 1 8 < = ©  2010 Goodrich, Tamassia
Binary Search Trees 7 Insertion To perform operation  put (k,  o), we search for key k  (using TreeSearch) Assume k is not already in  the tree, and let w be the  leaf reached by the search We insert k at node w and  expand w into an internal  node Example: insert 5 6 9 2 4 1 8 6 9 2 4 1 8 5 < w w ©  2010 Goodrich, Tamassia

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

View Full Document
Binary Search Trees 8 Deletion To perform operation  erase ( k ), we search for key  k Assume key  k  is in the tree,  and let let  v  be the node  storing  k If node  v  has a leaf child  w we remove  v  and  w  from the  tree with operation  removeExternal ( w
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