Introduction to Algorithms, Second Edition

Info iconThis 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 satisFes 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 .
Background image of page 1

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

View Full DocumentRight 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 ) ±nds the element with maximum key value in the subtree rooted at x . Minimum ( x ) ±nds 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.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Lecture 18-19 - The University of Texas at Austin...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online