Introduction to Algorithms, Second Edition

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

View Full Document Right Arrow Icon
The University of Texas at Austin Binary Search Trees Department of Computer Sciences Professor Vijaya Ramachandran Lectures 18-19 CS357: ALGORITHMS, Spring 2006 1 Dictionaries A dictionary is a data structure that supports the operations of Search, Insert, Delete . Search ( S,k ) is given the set S and a key value k . It returns a pointer to an element x in S such that key ( x ) = k . If there is no element in S with key value k it returns NIL . Insert ( S,x ) is given the set S and a pointer to an element x (which comes with a key value supplied to it. It forms the set S ∪ { x } . Delete ( S,x ) is given the set S and a pointer to an element x in S . It forms the set S − { x } , i.e., it removes element x from the set S . There are two important variants of a dictionary. A static dictionary is a restricted version that supports only the Search op- eration. The goal here is to come up with a compact representation of the set while supporting very fast look-ups. When the elements of the set come from a totally ordered set, a dictionary on a total order supports Minimum, Maximum, Successor, Predecessor operations in addition to the three standard dictionary operations. 2 Binary Search Trees Recall that a binary search tree T is a rooted, ordered binary tree with each node holding a key from a totally ordered set in a manner that satisfies the following binary search tree property for each node x in T : key ( x ) key ( y ) for every node y in the subtree rooted at the left child of x . key ( x ) key ( y ) for every node y in the subtree rooted at the right child of x .
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
We will use p ( x ) to denote parent of x , left ( x ) to denote the left child of x and right ( x ) to denote the right child of x . If any of these nodes are missing, that will be denoted by NIL . For convenience we will assume that all keys stored in the key are distinct. Inorder Walk. We can print out the keys in binary search tree T in sorted order in linear time by performing an inorder walk on T . Let h be the height of binary search tree T . Each of the following operations can be performed on T in O ( h ) time. Search ( x,k ) determines if there is an element in the subtree rooted at x with key value k . Maximum ( x ) finds the element with maximum key value in the subtree rooted at x . Minimum ( x ) finds the element with minimum key value in the subtree rooted at x . Successor ( x ) is given a pointer to an element x in T , and returns a pointer to the smallest element in T with value greater than key value of x . Similarly, we have an operation Predecessor ( T,x ). Insert ( x,k ) inserts a new node with key value k in the subtree rooted at x . This operation is performed by searching for key value k , and then adding a new leaf at the position at which the search terminates. Delete ( T,x ) is given a pointer to x , and removes x from T , and suitably adjusts its parent and child pointers; if x has two children then it moves the successor of x into the location vacated by x .
Image of page 2
Image of page 3
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