L09 - Balanced search trees.pdf

L09 - Balanced search trees.pdf - Balanced search trees(2,3...

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

View Full Document Right Arrow Icon
Balanced search trees
Image of page 1

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

View Full Document Right Arrow Icon
(2,3) Trees
Image of page 2
Binary search trees (BST) } Recall the binary search tree data structure © University of Sydney 3
Image of page 3

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

View Full Document Right Arrow Icon
© Goodrich and Tamassia 4 Binary Search Trees (review) } A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property: } Let u , v , and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v . We have key ( u ) key ( v ) key ( w ) } External nodes do not store items } An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8
Image of page 4
© Goodrich and Tamassia 5 Search (review) } 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 T.isExternal ( v ) return v if k < key ( v ) return TreeSearch ( k , T.left ( v )) else if k = key ( v ) return v else { k > key ( v ) } return TreeSearch ( k , T.right ( v )) 6 9 2 4 1 8 < > =
Image of page 5

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

View Full Document Right Arrow Icon
© Goodrich and Tamassia 6 Insertion (review) } 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
Image of page 6
© Goodrich and Tamassia 7 Deletion (review) } To perform operation remove ( 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 < >
Image of page 7

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

View Full Document Right Arrow Icon
© Goodrich and Tamassia 8 Deletion (cont.) (review) } 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
Image of page 8
© Goodrich and Tamassia 9 Performance (review) } 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 remove take O ( h ) time } The height h is O ( n ) in the worst case and O (log n ) in the best case
Image of page 9

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

View Full Document Right Arrow Icon
Inorder successors in BSTs (review) } Delete operation on an internal node with two children: } replace with inorder successor } Or, replace with inorder predecessor } Finding inorder successors: } The inorder successor of a node v is the “left-most” node of the right subtree } The inorder successor is either a leaf or an internal node that only a left child } Therefore inorder successors are “easy” to delete from a binary search tree © Goodrich and Tamassia 10
Image of page 10
Inorder successor (review) InorderNext(v): (if v has no right subtree, then there is no successor) temp = right child of v while (temp has a left child) temp = left child of temp return temp © Goodrich and Tamassia
Image of page 11

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

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