11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 5.4 Binary Search Trees 163• Right sibling(r) = r + 1 if r is odd <strong>and</strong> r + 1 < n.5.4 Binary Search TreesSection 4.4 presented the dictionary ADT, along with dictionary implement<strong>at</strong>ionsbased on sorted <strong>and</strong> unsorted lists. When implementing the dictionary with anunsorted list, inserting a new record into the dictionary can be performed quickly byputting it <strong>at</strong> the end of the list. However, searching an unsorted list for a particularrecord requires Θ(n) time in the average case. For a large d<strong>at</strong>abase, this is probablymuch too slow. Altern<strong>at</strong>ively, the records can be stored in a sorted list. If the listis implemented using a linked list, then no speedup to the search oper<strong>at</strong>ion willresult from storing the records in sorted order. On the other h<strong>and</strong>, if we use a sortedarray-based list to implement the dictionary, then binary search can be used to finda record in only Θ(log n) time. However, insertion will now require Θ(n) time onaverage because, once the proper loc<strong>at</strong>ion for the new record in the sorted list hasbeen found, many records might be shifted to make room for the new record.Is there some way to organize a collection of records so th<strong>at</strong> inserting records<strong>and</strong> searching for records can both be done quickly? This section presents thebinary search tree (BST), which allows an improved solution to this problem.A BST is a binary tree th<strong>at</strong> conforms to the following condition, known asthe Binary Search Tree Property: All nodes stored in the left subtree of a nodewhose key value is K have key values less than K. All nodes stored in the rightsubtree of a node whose key value is K have key values gre<strong>at</strong>er than or equal to K.Figure 5.13 shows two BSTs for a collection of values. One consequence of theBinary Search Tree Property is th<strong>at</strong> if the BST nodes are printed using an inordertraversal (see Section 5.2), the resulting enumer<strong>at</strong>ion will be in sorted order fromlowest to highest.Figure 5.14 shows a class declar<strong>at</strong>ion for the BST th<strong>at</strong> implements the dictionaryADT. The public member functions include those required by the dictionaryADT, along with a constructor <strong>and</strong> destructor. Recall from the discussion in Section4.4 th<strong>at</strong> there are various ways to deal with keys <strong>and</strong> comparing records (threeapproaches being key/value pairs, a special comparison method such as using theCompar<strong>at</strong>or class, <strong>and</strong> passing in a compar<strong>at</strong>or function). Our BST implement<strong>at</strong>ionwill h<strong>and</strong>le comparison by explicitly storing a key separ<strong>at</strong>e from the d<strong>at</strong>a value<strong>at</strong> each node of the tree.To find a record with key value K in a BST, begin <strong>at</strong> the root. If the root storesa record with key value K, then the search is over. If not, then we must searchdeeper in the tree. Wh<strong>at</strong> makes the BST efficient during search is th<strong>at</strong> we needsearch only one of the node’s two subtrees. If K is less than the root node’s keyvalue, we search only the left subtree. If K is gre<strong>at</strong>er than the root node’s keyvalue, we search only the right subtree. This process continues until a record with

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!