{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 6-twoup - The Dictionary ADT Precondition true...

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

The Dictionary ADT Precondition: true . Postcondition: Constructs an empty Dictionary . Dictionary () Precondition: k is a Key . Postcondition: Returns the element with key k , or nil if no item with key k is contained in the set. Dictionary.Get ( k ) Precondition: x negationslash = nil , and k is a Key that is not associated with any item in the set. Postcondition: Adds x to the set with key k . Dictionary.Put ( x,k ) 1 Precondition: k is a Key . Postcondition: If there is an item with key k in the set, this item is removed. Dictionary.Remove ( k ) Precondition: true . Postcondition: Returns the number of items in the set. Dictionary.Size () 2

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

View Full Document
The Visitor Pattern Sometimes we need to process the items in a predetermined order, which we denote by the operator on keys. We can facilitate such operations using the Visitor interface: Precondition: true . Postcondition: Completes without an error. May modify the state of x . Visitor.Visit ( x ) We can implement the Visit operation to do whatever processing we want on an single element. 3 The OrderedDictionary ADT We extend the Dictionary ADT with the following operation: Precondition: v is a Visitor . Postcondition: Applies v. Visit ( x ) to every item x in the set in order of their keys. OrderedDictionary.VisitInOrder ( v ) 4
Example: Printing Elements In Order We first define Printer , which implements Visitor : Precondition: true . Postcondition: Prints x . Printer.Visit ( x ) print x We can then print all elements in an OrderedDictionary d in order of their keys: d. VisitInOrder ( new Printer ()) 5 Ordered Array Θ(lg n ) implementation of Get using binary search. Θ( n ) implementation of VisitInOrder . Worst-case Θ( n ) implementation of Put and Remove . 6

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

View Full Document
Ordered Linked List Θ( n ) implementation of VisitInOrder . Once a location is found, insertion/deletion can be done in Θ(1) time. Worst-case Θ( n ) time to find a location. 7 Binary Search Tree Attempts to combine binary search with the ability to do local changes to the structure. Get , Put , and Remove operate in time proportional to the height. Θ( n ) implementation of VisitInOrder using inorder traversal. In the worst case, the height of a tree with n nodes is n 1 . 8
AVL Trees Defn: An AVL Tree is a binary search tree such that for each nonempty subtree, the children differ in height by at most 1 . An AVL tree of height h > 1 having the fewest nodes has one child of height h 1 ; and one child of height h 2 . Furthermore, each child contains the fewest nodes of any AVL tree of that height. 9 Let g ( h ) be the minimum number of nodes in an AVL tree of height h . g ( h ) = 1 if h = 0 2 if h = 1 g ( h 1) + g ( h 2) + 1 if h > 1 . Let g 1 ( h ) = g ( h ) if h 1 2 g ( h 2) + 1 otherwise Then g 1 ( h ) g ( h ) .

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 ]}