{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

module4

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 different, and there is an ordering (denoted by < ) defined 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 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
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 Y. 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. To 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

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, 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 by 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.

{[ snackBarMessage ]}

### What students are saying

• 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.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern