This preview shows pages 1–6. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 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 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 . Worstcase ( n ) implementation of Put and Remove . 6 Ordered Linked List ( n ) implementation of VisitInOrder . Once a location is found, insertion/deletion can be done in (1) time. Worstcase ( 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 ....
View
Full
Document
 Spring '09
 Howell

Click to edit the document details