14. BinarySearchTrees_outside

Binary search trees 6 search to search for a key k we

This preview shows page 6 - 10 out of 10 pages.

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
Image of page 6

Subscribe to view the full document.

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
Image of page 7
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 ), which removes w and its parent Example: remove 4 6 9 2 4 1 8 5 v w 6 9 2 5 1 8 < > © 2010 Goodrich, Tamassia
Image of page 8

Subscribe to view the full document.

Binary Search Trees 9 Deletion (cont.) We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key ( w ) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeExternal ( z ) Example: remove 3 3 1 8 6 9 5 v w z 2 5 1 8 6 9 v 2 © 2010 Goodrich, Tamassia
Image of page 9
Binary Search Trees 10 Performance Consider an ordered map with n items implemented by means of a binary search tree of height h the space used is O ( n ) methods get , floorEntry , ceilingEntry , put and erase take O ( h ) time The height h is O ( n ) in the worst case and O (log n ) in the best case © 2010 Goodrich, Tamassia
Image of page 10

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

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