Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.

Similar presentations


Presentation on theme: "CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009."— Presentation transcript:

1 CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009

2 Lecture Overview More Data Structures! Stacks Queues Maps  Hashtables / Hashmaps Quiz #4

3 Stack: LIFO Allows insertion and removal of elements only at one end  Traditionally called the top of the stack New items are added to the top of the stack Items are removed at the top of the stack Called last in, first out or LIFO order Traditionally, addition and removal operations are called push and pop Think of a stack of books

4 A Stack of Books

5 Queue: FIFO Add items to one end of the queue (the tail) Remove items from the other end of the queue (the head) Queues store items in a first in, first out or FIFO fashion Items are removed in the same order in which they have been added Think of people lining up  People join the tail of the queue and wait until they have reached the head of the queue Traditionally, addition and removal operations are called enqueue and dequeue

6 Stacks and Queues: Uses in Computer Science Queue  Anything where resources are shared, and an attempt is made to give fair treatment  User input: Mouse clicks and keystrokes  Queue of print jobs Stack  Run-time stack that a processor or virtual machine keeps to organize the variables of nested method calls

7 Stacks and Queues: Implementation Stacks and Queues are commonly implemented using a Linked List  Aggregation / Delegation We can use a list internally and limit the public interface  Which methods can be publicly invoked  The Stack or Queue class delegates the actual storage to a list, and then defines how the list is used [ Example on Board ]

8 Map A map is a data structure which creates a relationship between two objects  A key and a value  The key is mapped to the value Typically, the key is an easily reproducible object  E.g. a String or an Integer The value is what the map is actually intended to store.

9 Maps: interface put / insert(key, value) : insert the key / value in the map if they key is not already in the map.  If the key is in the map, replace the associated value get(key) : retrieve the value associated with the key remove(key) : remove the key and value from the map

10 Maps: implementation Linked List [ Example on Board ] Binary Tree [ Example on Board ] Hash Table

11 Hashtables An efficient way to implement the map data structure: Can usually perform operations in constant time public void put (Object key, Object value); public Object get (Object key); public Object remove (Object key);

12 Hashtables: Implementation Internally, hashtables use an array Each element of the internal array in a hashtable is referred to as a bucket Where an element is stored in the array is determined by the hash function of the hash table Object[] array = new Object[11];

13 Hashtables: Implementation The hash function converts the supplied key to an integer “But Nick, our key is an Object! How the heck do we convert an object to an integer!?!?” public void put (Object key, Object value) { int hash = this.hashFunction(key);... }

14 Hashtables: Implementation Java helps us out with a predefined method: public void put (Object key, Object value) { int hash = key.hashCode();... }

15 Hashtables: Implementation We can override hashCode in our objects if we need to reflect equality, as with the.equals method String overrides hashCode so that all equivalent Strings are hashed identically String foo = “foo”; String foo2 = “foo”; // foo.hashCode() == foo2.hashCode()

16 Hashtables: Implementation The result of the hash function must next be sized to provide a valid index into the array  0 – array.length -1  Do any arithmetic operators come to mind? public void put (Object key, Object value) { int hash = key.hashCode(); int index = hash % this.array.length; // insert into this.array[index] }

17 Hashtables: Implementation Because of the Modulus operator is used with most simple hash functions, it is best to size your array with a prime number  Why do you think this is? It helps prevent collisions 2 or more values vying for the same bucket  Java’s java.util.Hashtable class has an initial array size of 11

18 Hashtables: Handling Collisions If all keys into a hashtable are known before time, a perfect hash function can be used to avoid collisions  Most often, this is not the case We won’t know all keys prior to execution  Two prevailing methods for handling collisions when they occur Open Addressing Chaining

19 Hashtables: Handling Collisions Open Addressing  Check next bucket to see if it is occupied If so, check the next bucket until we find an open one  -or- Apply a secondary hash function to find another bucket Pros: We only have to use our array for internal storage Cons: The hashtable fills up much more quickly

20 Hashtables: Handling Collisions Chaining  Simply have each bucket store a list  When a collision occurs, add the item to the bucket’s list DoublyLinkedList[] array = new DoublyLinkedList[11];

21 Hashtables: Chaining Pros:  Table can hold more elements before suffering bad performance Cons:  Table has a bigger memory footprint & requires allocation on each insert

22 Hashtables: Implementation Initialization public void put (Object key, Object value) public Object get (Object key) public Object remove (Object key)

23 Hashtables: Rehashing When a hashtable gets too full, its internal array must be resized in order to regain more efficient performance All elements must be rehashed with new array size in mind In Lab 4, you will not need to worry about rehashing

24 Conclusion Questions? Midterm in class next Tuesday (13 th )  Practice problems on the web Lab 3 Due tonight at Midnight Lab 4 Assigned now  Due Oct. 15  Get started early! I will be in lab now


Download ppt "CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009."

Similar presentations


Ads by Google