module04

# module04 - Module 4 Dictionaries and Balanced Search Trees...

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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.

Unformatted text preview: Module 4: Dictionaries and Balanced Search Trees CS 240 - Data Structures and Data Management Arne Storjohann Based on lecture notes by R. Dorrigiv and D. Roche School of Computer Science, University of Waterloo Fall 2010 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 1 / 29 Dictionary ADT A dictionary is a collection of items , each of which contains a key and some data and is called a key-value pair (KVP). Keys can be compared and are typically unique. Operations: search ( k ) insert ( k , v ) delete ( k ) optional: join , isEmpty , size , etc. Examples: symbol table, license plate database Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 2 / 29 Elementary Implementations Common assumptions: Dictionary has n KVPs Each KVP uses constant space (if not, the “value” could be a pointer) Comparing keys takes constant time Unordered array or linked list search Θ( n ) insert Θ(1) delete Θ( n ) (need to search) Ordered array search Θ(log n ) insert Θ( n ) delete Θ( n ) Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 3 / 29 Binary Search Trees (review) Structure A BST is either empty or contains a KVP, left child BST, and right child BST. Ordering Every key k in T . left is less than the root key. Every key k in T . right is greater than the root key. 15 6 10 8 14 25 23 29 27 50 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 4 / 29 BST Search and Insert search ( k ) Compare k to current node, stop if found, else recurse on subtree unless it’s empty Example: search (24) 15 6 10 8 14 25 23 29 27 50 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 5 / 29 BST Search and Insert search ( k ) Compare k to current node, stop if found, else recurse on subtree unless it’s empty Example: search (24) 15 6 10 8 14 25 23 29 27 50 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 5 / 29 BST Search and Insert search ( k ) Compare k to current node, stop if found, else recurse on subtree unless it’s empty Example: search (24) 15 6 10 8 14 25 23 29 27 50 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 5 / 29 BST Search and Insert search ( k ) Compare k to current node, stop if found, else recurse on subtree unless it’s empty Example: search (24) 15 6 10 8 14 25 23 29 27 50 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 5 / 29 BST Search and Insert search ( k ) Compare k to current node, stop if found, else recurse on subtree unless it’s empty insert ( k , v ) Search for k , then insert ( k , v ) as new node Example: insert (24 ,... ) 15 6 10 8 14 25 23 24 29 27 50 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 5 / 29 BST Delete If node is a leaf, just delete it. 15 6 10 8 14 25 23 24 29 27 50 Arne Storjohann (CS, UW) CS240 - Module 4 Fall 2010 6 / 29 BST Delete If node is a leaf, just delete it....
View Full Document

## This note was uploaded on 01/14/2012 for the course CS 246 taught by Professor Wormer during the Winter '08 term at Waterloo.

### Page1 / 75

module04 - Module 4 Dictionaries and Balanced Search Trees...

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

View Full Document
Ask a homework question - tutors are online