css446 spring 2014 nan wang. to understand the implementation of linked lists and array lists to...

36
CSS446 Spring 2014 Nan Wang

Upload: vivien-agnes-goodwin

Post on 17-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

CSS446Spring 2014

Nan Wang

Page 2: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 3: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 4: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

LinkedList class holds a reference first to the first node (or null, if the list is completely empty)

4

Page 5: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 6: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 7: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

Array lists allowing you to add and remove elements at any position.

19

Page 8: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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.

Page 9: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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.

Page 10: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

Adding an element to an array list cost only O(1).

22

Page 11: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 12: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

Add and remove notes from the same end of the node sequence.

25

Page 13: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

The push and pop are both O(1) operations.

26

Page 14: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 15: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 16: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 17: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

32

Page 18: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 19: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 20: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

Pick an array of reasonable size and then compress the hash code to become a valid array index.

Problem: Collsion Solution: bucket

35

Page 21: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 22: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 23: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 24: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

Page 25: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

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

40

Page 26: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

41

Page 27: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

42

Page 28: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

43

Page 29: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

44

Page 30: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

45

Page 31: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

46

Page 32: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

47

Page 33: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

48

Page 34: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

49

Page 35: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

50

Page 36: CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations

51