2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists.
Garfield AP Computer Science
2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
COMP 110 Introduction to Programming Mr. Joshua Stough.
B+ Trees COMP
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
Information and Computer Sciences University of Hawaii, Manoa
COMP 103 Hashing 2013-T2 Lecture 28 Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay.
2014-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Linked Structures.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
COMP 103 Hashing (II), and exam tips 2014-T2 Lecture 33 Marcus Frean School of Engineering and Computer Science, Victoria University of Wellington  Marcus.
FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington 
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
COMP 103 Linked Structures
Linked Structures – Part 2
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Lists CS 3358.
COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19
Introduction to Analysing Costs
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Binary Search Trees (I)
More complexity analysis & Binary Search
Depth-first vs breadth-first traversals
Arrays and Linked Lists
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Linked List and Selection Sort
Presentation 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 COMP 103 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(n 2 ) 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 revisited Implementing Collections:  ArrayList: O(n) to add/remove, except at end  Stack:O(1)  ArraySet:O(n) (cost of searching)  SortedArraySetO( log(n) ) to search (with binary search) O(n) to add/remove (cost of moving up/down)  O( n 2 ) to add n items O( 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 ? BAEDC GIH 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 A E 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 3 List of length 2 List of length 1 C M X P 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 { private E value; private LinkedNode next; public LinkedNode(E item, LinkedNode nextNode) { value = item; next = nextNode; } public E getValue() { return value; } public LinkedNode next(){ return next; } public void setValue(E item) { value = item; } public void setNext(LinkedNode nextNode) { next = nextNode; } an E

13 Using Linked Nodes LinkedNode colours = new LinkedNode (“red”, null); colours.setNext(new LinkedNode (“blue”, null)); colours = new LinkedNode (“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() );  1 st : green 2 nd : red 3 rd : blue colours “red”“blue”“green”

14 Using Linked Nodes  Removing a node in the middle: colours.setNext( );  “Copy” colours, then “remove” first node LinkedNode 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 squares = null; for (int i = 1; i ( i*i, squares );  This builds the list backwards…  Exercise: Build the list working forwards 49 1 squares /

16 Iterating through a linked list LinkedNode rest = squares; while (rest != null) { UI.println(rest.getValue()); rest = rest.next(); } or for (LinkedNode rest = squares; rest != null; rest = rest.next()) { UI.println(rest.getValue()); } or we could do it with recursion squares rest /

17 list list’ T wo ways to print a linked list /** Print the values in the list starting at a node */ public void printList(LinkedNode 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 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 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 return