Presentation is loading. Please wait.

Presentation is loading. Please wait.

Now with a speaking professor! (hopefully...)

Similar presentations


Presentation on theme: "Now with a speaking professor! (hopefully...)"— Presentation transcript:

1 Now with a speaking professor! (hopefully...)
Dictionaries Now with a speaking professor! (hopefully...)

2 Assignment 2 Your next homework assignment is due a week from today
You will need to implement a dictionary class and write a program that allows for stocks to be tracked (looking up stocks by name, updating stocks, etc.) We'll talk more about this in a bit... You can work alone or in groups of two There are two parts to the assignment that can be done separately

3 Dictionaries Today, we’ll be talking about a very prevalent abstract data structure: dictionaries A dictionary is a data structure that allows for multiple pieces of data (or values) to be stored, with each piece of data being associated with a key that allows us to look it up Dictionaries are also known as: Maps, associative containers, key-value stores, hashes*, etc.

4 Examples of dictionaries in the real world
Remember phone books? Given a name, allows you to look up a phone number Given a string, look up another string Remember physical dictionaries? Given a word, look up a definition Remember thesauruses? Given a word, look up a number of words Given a string, look up a list of strings

5 Other examples of dictionaries?
What other examples can you think of where a dictionary would be useful or appropriate?

6 Implementing a dictionary
In class, we’ll see many examples of how dictionaries can be implemented Different implementation methods have different performance characteristics In class today, we’ll see a dictionary implementation that follows the Schroeder mantra For your next homework assignment, you’ll be implementing a more intelligent implementation technique.

7 Dictionary ADT All of our implementations of the Dictionary ADT will have the same functions with the same semantics How can we make sure that different classes all allow the same set of functions?

8 Dictionary abstract base class
We will use the same Dictionary class for all of our dictionary implementations Let’s take a look at this class…

9 Dictionary implementation: SimpleDictionary
To keep track of keys and values, we can have an array of keys and an array of values To have a key associated with a value, we can have the element at index i in the keys array correspond to the element at index i in the values array

10 Example SimpleDictionary<string, int> d(10); d.add("car", 12);
d.add("zoo", 90); d.add("ant", 8); d.getValue("zoo"); d.remove("car"); d.add("cat", 7);

11 SimpleDictionary Let's take a look at how SimpleDictionary is implemented...

12 SimpleDictionary performance
Let's take a look at the Big-O performance for some of these functions...

13 SortedDictionary: a better Dictionary
Almost all of our functions are O(n) If we're clever, we can improve some functions to O(log n) while keeping some still at O(n) Key insight: if keys are stored in a sorted order, it's really fast to look up a key's index How?

14 Binary search Give me an integer between 1 and 100

15 Binary search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 20 28 32 46 51 54 62 65 75 78 79 98

16 SortedDictionary We need to make sure our keys are stored in an appropriate order Our keys must always be sorted When we add a key/value pair, we need to: Find the right index to insert the new key at Shift every key and value over to make room for the new key/value pair Add the new key/value pair at the newly-empty index When we remove a key/value pair, we need to: Shift elements over to overwrite the element to be deleted

17 SortedDictionary, cont'd
Whenever we need to find an index, we should use binary search to find the appropriate index int findIndex(K key, int low, int high) Return the index of the index in the range low to high of the first item in the keys array greater than or equal to 'key' I strongly strongly suggest making this a recursive function to make life easier

18 Finishing up Let's look at main.cpp and what the stock tracker should act like


Download ppt "Now with a speaking professor! (hopefully...)"

Similar presentations


Ads by Google