1 / 36

Advanced Java Programming

Advanced Java Programming. CSS446 Spring 2014 Nan Wang. Chapter Goals. To understand the implementation of linked lists and array lists To analyze the efficiency of fundamental operations of lists and arrays To implement the stack and queue data types

efrazier
Télécharger la présentation

Advanced Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Java Programming CSS446 Spring 2014 Nan Wang

  2. Chapter Goals • To understand the implementation of linked lists and array lists • To analyze the efficiency of fundamental operations of lists and arrays • To implement the stack and queue data types • To implement a hash table and understand the efficiency of its operations

  3. Implementing Linked Lists Node Class • A linked list stores elements in a sequence of nodes. • Node object stores an element and a reference to the next node • make Node a private inner class of the LinkedList class

  4. Node Class • LinkedList class holds a reference firstto the first node (or null, if the list is completely empty)

  5. Adding the First Element • When a new node is added, it becomes the headof the list, and the node that was the old list head becomes its next node

  6. Removing the First Element • The successor of the first node becomes the first node of the shorter list. Then there are no further references to the old node, and the garbage collector will eventually recycle it.

  7. Implementing Array Lists • Array lists allowing you to add and remove elements at any position.

  8. Getting and Setting Elements • An array list maintains a reference to an array of elements. The array is large enough and when the array gets full, it is replaced by a larger one. • For simplicity, our ArrayList manages elements of type Object. • To access array list elements, we provide getand setmethods. There are O(1) operations for get and set method.

  9. Removing or Adding Elements • When removing an element at position k, the elements with higher index values need to move. Removing the ith element  There are O(n) operations.

  10. Removing or Adding Elements • Adding an element to an array list cost only O(1).

  11. Growing the Internal Array • If there is no more room in the internal array, then we need to grow it. • The new array is typically twice the size of the current array and the elements are then copied to the new array.----O(n)

  12. Stacks as Linked Lists • Add and remove notes from the same end of the node sequence.

  13. Stacks as Linked Lists • The push and pop are both O(1) operations.

  14. Stacks as Arrays • Store the value in an array and therefore saving the storage of references. • The array can grow when it gets full. • The push and pop are both O(1)+ operations.

  15. Queues as Linked Lists • Add nodes at one end of the queue and remove them at the other end. • The add and remove operations of a queue are O(1) operations

  16. Hash Function • Hash Set and Hash Map • Hash function is to compute hash code from an object in such a way that different objects are likely to yield different hash code. • Int h=x.hashCode();

  17. Hash Function for String

  18. Hash Codes • The basic idea behind hashing is to place object into an array, at a location that can be determined from the object itself. • It is possible for two or more distinct objects to have the same hash code - Collision

  19. Hash Tables • A hash table uses the hash code to determine where to store each element. • Idea 1: A hash code is used as an array index into a hash table. • No collision • Large enough Array Needed • Solution: • Compress

  20. Hash Table Compression • Pick an array of reasonable size and then compress the hash code to become a valid array index. • Problem: Collsion • Solution: bucket

  21. Implementing a Hash Table Hash Table • using a hash function to compute an index into an array of buckets or slots, from which the correct value can be found. • Elements in a bucket or slot are stored as a linked list

  22. Good design of Hash Function • Rule: evenly distributed in all buckets • Load factor F = n/L • n: the number of element • L: the table length • 0.75 is for the standard Java library

  23. Finding an Element • Algorithm for finding an object obj in a hash table: • 1. Compute the hash code and compress it. This gives an index h into the hash table. • 2. Iterate through the elements of the bucket at position h. For each element of the bucket, check whether it is equal to obj. • 3. If a match is found among the elements of that bucket, then obj is in the set. • Taking O(1) constant time.

  24. Adding and Removing Elements • First compute the hash code to locate the bucket and then insert: • 1. Compute the compressed hash code h. • 2. Iterate through the elements of the bucket at position h. For each element of the bucket, check whether it is equal to obj. • 3. If a match is found among the elements of that bucket, then exit. • 4. Otherwise, add a node containing obj to the beginning of the node sequence. • 5. If the load factor exceeds a fixed threshold, reallocate the table. • Adding an element to a hash table is O(1)+

  25. Iterating over a Hash Table • Iterating over a Hash Table takes O(n) time.

  26. Implementation of HashSet

  27. HashSetIterator

  28. next()

  29. HashSet Class

  30. contains()

  31. add()

  32. Demo

  33. Prepare for Chapter 17

More Related