2015-t2 lecture 17 school of engineering and computer science, victoria university of wellington ...
TRANSCRIPT
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria
University of Wellington
Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis, and Thomas Kuehne, VUW
CO
MP 1
03
Marcus Frean
Linked Structures
2
RECAP-TODAY
RECAP We were looking at costs, Binary Search, then Sorting
and recursing
TODAY A whole new way to implement Collections: Linked
Structures
Announcements oops, I forgot to record the QuickSort lecture –
hopefully fixed now
3
(from the end of last lecture)
Stable or Unstable? Faster if almost-sorted?
MergeSort: Stable: doesn’t jump any item over an unsorted region
⇒ two equal items preserve their order
Same cost on all input “natural merge” variant doesn’t sort already sorted
regions⇒ will be very fast: O(n) on almost sorted lists
QuickSort: Unstable: Partition “jumps” items to the other end
⇒ two equal items likely to reverse their order
Cost depends on choice of pivot simplest choice can be very slow: O(n2) even on almost
sorted lists better choice (median of three) ⇒ O(n log(n)) on almost
sorted lists
4
(from the end of last lecture)
Some “Big-Oh” costs revisitedImplementing Collections:
ArrayList: O(n) to add/remove, except at end
Stack: O(1)
ArraySet: O(n) (cost of searching)
SortedArraySet O( log(n) ) to search (with binary search)
O(n) to add/remove (cost of
moving up/down)
O( n2 ) to add n itemsO( n log(n) ) to initialise with n
items. (with fast sorting)
5
Better Implementations for Collections So far we have relied on arrays as a supporting data
structure for collection classes → ArrayList, SortedArraySet, ArrayQueue,
ArrayPriorityQueue….
Array advantages: quick random access
Array disadvantages: growing them is costly
(remember “ensureCapacity”?) need contiguous chunks of memory maintaining order is costly (Lecture 12)
How to address these disadvantages?
6
Array versus Linked Structure (analogy)
Acreelman.blogspot.com
7
How can we insert faster? Fast lookup in array
⇒ items must be sorted
Maintaining the sorting order is costly since inserting new items requires moving old ones
⇒ You can’t insert fast with a sorted array! To make insert faster, we would need each item to
know its neighbours not by position but by topology
But, how do we keep track of the order?
BA EDC G IH
F
B
A
ED
C H
I
G
F
8
Linked List Put each value in an object with a field for a link to the
next
Traverse the list by following the links Insert (F) by changing links ⇒ No need to shift
everything up Remove (D) by changing links ⇒ No need to shift
things down
B
AE
D
C
G
I
H
F
order of operations is
important
9
Every node in a linked list can be considered to be the “head”of a sublist
That’s why linked lists – and other linked structures – lend themselves to be used with recursive methods
Linked Lists are Recursive Structures
List of length 3List of length 2
List of length 1
C M XP
shorthand for "null": end of the
list
10
Memory management What are references/pointers?
A pointer/reference is an address of a chunk of memory, where the data can be stored
How do you get this memory allocated? You’ve been doing it since the start of Comp102, using new:
creating an object allocates some chunk memory for the object new returns the address of the chunk of memory copying the address does not copy the chunk of memory
Memory should be recycled after use: In Java, you don’t have to worry about freeing memory. The
garbage collector automatically frees up any memory chunks that no longer have anything pointing/referring to them
In languages without a garbage collector (eg C, C++), you have to do this yourself. OK to ignore in small programs, but requires special care in large programs!
11
A Linked Node class:
public class LinkedNode {private int value;private LinkedNode next;
public LinkedNode(int item, LinkedNode nextNode) {value = item; next = nextNode;
}
public int getValue() { return value; }public LinkedNode next() { return next; }
public void setValue(int item) {value = item;
}
public void setNext(LinkedNode nextNode) {next = nextNode;
}
}
4 5
12
A Generic Linked Node class:public class LinkedNode <E> {
private E value;private LinkedNode<E> next;
public LinkedNode(E item, LinkedNode<E> nextNode) {value = item; next = nextNode;
}
public E getValue() { return value; }public LinkedNode<E> next() { return next; }
public void setValue(E item) {value = item;
}
public void setNext(LinkedNode<E> nextNode) {next = nextNode;
}
}
an E
13
Using Linked NodesLinkedNode<String> colours = new LinkedNode<String> (“red”, null);
colours.setNext(new LinkedNode<String>(“blue”, null));
colours = new LinkedNode<String>(“green”, colours);
System.out.printf(“1st: %s ”, colours.getValue() );
System.out.printf(“2nd: %s ”, colours.next().getValue() );
System.out.printf(“3rd: %s ”, colours.next().next().getValue() );
1st: green 2nd: red 3rd: blue
colours
“red” “blue”
“green”
14
Using Linked Nodes Removing a node in the middle:
colours.setNext( );
“Copy” colours, then “remove” first node
LinkedNode<String> copy = colours;
colours = colours.next();
“red”
colours
“green”
“blue”
copy
No references.
Garbage collector will get it eventually
colours.next().next()
15
Using Linked Nodes Making a list of squared ints:
LinkedNode<Integer> squares = null;
for (int i = 1; i < 1000; i++) squares = new LinkedNode<Integer> ( i*i, squares );
This builds the list backwards… Exercise: Build the list working forwards
4
9
1
squares /
16
Iterating through a linked list
LinkedNode<Integer> rest = squares;while (rest != null) {
UI.println(rest.getValue());rest = rest.next();
}
or
for (LinkedNode<Integer> rest = squares; rest != null; rest = rest.next()) {UI.println(rest.getValue());
}
or we could do it with recursion....
4
9
1
squares
rest /
17
list list’
Two ways to print a linked list
/** Print the values in the list starting at a node */public void printList(LinkedNode<E> list) {
if (list == null) return;
UI.println(list.getValue());
if (list.next() == null) return;
printList(list.next());
}
“dog”“cat” “cow”
recursive call
base case
public void printList(LinkedNode<E> list’) {
if (list’ == null) return;
UI.println(list’.getValue());
if (list’.next() == null) return;
printList(list’.next());
}
handling an empty list
18
/** Print the values in the list starting at a node */
public void printList(LinkedNode<E> list ) {if (list == null) return;
UI.println(list .getValue());
printList(list .next());
}
Example: printList ( the recursive version )
printList( list )
"cat"
"dog"
"cow"
“dog”“cat” “cow”animals
printList( animals );
printList( list’ )
printList( list’’ )
printList( list’’’ )
call
call
call
return
return
return