module4

# module4 - Dictionary ADT A dictionary is an ADT consisting...

This preview shows pages 1–6. Sign up to view the full content.

Dictionary ADT A dictionary is an ADT consisting of a collection of items with operations Insert , Delete and Search (this operation may also be named Find ). We search for items by the value of their keys . Typically, items may contain additional satellite data. Usually, we will assume that all keys are diﬀerent, and there is an ordering (denoted by < ) deﬁned on keys. 80 / 102

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

View Full Document
Simple Implementations We can implement the dictionary ADT using an unordered array, an ordered array or a binary search tree. Suppose the dictionary contains n items. The various operations have diﬀering (worst-case) complexities depending on the data structure used in the implementation, as summarized in the following table: implementation Insert Search Delete unordered array Θ(1) Θ( n ) Θ(1) ordered array Θ( n ) Θ(log n ) Θ( n ) binary search tree Θ(height) Θ(height) Θ(height) In the case of Delete in an unordered array, we assume that we have previously performed a successful search for the item to be deleted. 81 / 102
Operations in Binary Search Trees (review) For any node X in a binary search tree and for any descendant Y of X , we have Y. key < X. key if Y is in the left subtree of X , and key > X. key if Y is in the right subtree of X . Search is basically a binary search : start at the root and follow the appropriate path down the tree until the desired key value is found or we reach an empty subtree. To Insert an item with a new key, we perform an (unsuccessful) Search , and then insert the new node at the leaf node where the search terminated. Delete an item with a speciﬁed key, we ﬁrst use Search to ﬁnd the node X containing the given item. One of three possible cases then occurs. 82 / 102

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

View Full Document
Deletion in Binary Search Trees X is a leaf node. Delete X from the tree. X has only one child. Attach the nonempty subtree of X to the parent of X , and then delete X . X has two children. Find the successor of X , say Y , which is the leftmost node in the right subtree of X . Y has no left child. Attach the right subtree of Y as the left subtree of the parent of Y . Then replace X by Y . Alternatively, ﬁnd the predecessor of X , say Y , which is the rightmost node in the left subtree of X . Y has no right child. Attach the left subtree of Y as the right subtree of the parent of Y . Then replace X Y . 83 / 102
Height of a Binary Search Tree Suppose we build a binary search tree by performing n Insert operations, where the n items have n random (but distinct keys). It can be shown that the expected height of this random binary search tree is Θ(log n ). (A related result is shown on the next slide.) However, in the worst case, a binary search tree can have height Θ( n ). Therefore, Search , Insert and Delete all have worst-case complexity Θ( n ).

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/21/2012 for the course PSYCH 101 taught by Professor Ennis during the Winter '09 term at Waterloo.

### Page1 / 23

module4 - Dictionary ADT A dictionary is an ADT consisting...

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

View Full Document
Ask a homework question - tutors are online