css446 spring 2014 nan wang. to understand the implementation of linked lists and array lists to...
TRANSCRIPT
CSS446Spring 2014
Nan Wang
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
2
Node ClassA linked list stores elements in a sequence of nodes.Node object stores an element and a reference to the next nodemake Node a private inner class of the LinkedList class
3
LinkedList class holds a reference first to the first node (or null, if the list is completely empty)
4
When a new node is added, it becomes the head of the list, and the node that was the old list head becomes its next node
5
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.
6
Array lists allowing you to add and remove elements at any position.
19
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.
20
• For simplicity, our ArrayList manages elements of type Object.
• To access array list elements, we provide get and set methods.
There are O(1) operations for get and set method.
When removing an element at position k, the elements with higher index values need to move.
21
Removing the ith element
There are O(n) operations.
Adding an element to an array list cost only O(1).
22
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)
23
Add and remove notes from the same end of the node sequence.
25
The push and pop are both O(1) operations.
26
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.
27
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
28
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();
31
32
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
33
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
34
Pick an array of reasonable size and then compress the hash code to become a valid array index.
Problem: Collsion Solution: bucket
35
Hash Tableusing 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
36
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
37
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.
38
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)+
39
Iterating over a Hash Table takes O(n) time.
40
41
42
43
44
45
46
47
48
49
50
51