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

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

View Full Document Right Arrow Icon
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 different, and there is an ordering (denoted by < ) defined on keys. 80 / 102
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 differing (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
Background image of page 2
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 specified key, we first use Search to find the node X containing the given item. One of three possible cases then occurs. 82 / 102
Background image of page 3

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

View Full DocumentRight Arrow Icon
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, find 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
Background image of page 4
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 ).
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
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 Right Arrow Icon
Ask a homework question - tutors are online