360 likes | 369 Views
Explore linked lists, array lists, tasks, queues, hash functions, hash tables; implementation examples explained in detail with code samples
E N D
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 • To implement a hash table and understand the efficiency of its operations
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
Node Class • LinkedList class holds a reference firstto the first node (or null, if the list is completely empty)
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
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.
Implementing Array Lists • Array lists allowing you to add and remove elements at any position.
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.
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.
Removing or Adding Elements • Adding an element to an array list cost only O(1).
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)
Stacks as Linked Lists • Add and remove notes from the same end of the node sequence.
Stacks as Linked Lists • The push and pop are both O(1) operations.
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.
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
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();
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
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
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
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
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
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.
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)+
Iterating over a Hash Table • Iterating over a Hash Table takes O(n) time.