40
LECTURE 41: COURSE REVIEW CSC 212 – Data Structures

CSC 212 – Data Structures. Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220 Plan on exam taking full 2 hours If major problem, come talk to me ASAP

Embed Size (px)

Citation preview

Page 1: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

LECTURE 41:COURSE REVIEW

CSC 212 – Data Structures

Page 2: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Tues., Dec. 10th from 8:00 – 10:ooAM in OM220

Plan on exam taking full 2 hours If major problem, come talk to me ASAP

Exam covers material from entire semester Open-book & open-note so bring what

you’ve got My handouts, solutions, & computers are

not allowed Cannot collaborate with a neighbor on the

exam Problems will be in a similar style to 2

midterms

Final Exam

Page 3: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Classes vs. Objects

Classes are blueprints describing data type By itself, class only used for static fields &

methods Objects are instances of a class

New objects created (instantiated) using new

Fields describe state of an object Object’s behavior represented by methods

Page 4: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

static v. Instance-based

Methods that are instance-based have this Aliased to instance on which method called Can directly use fields & call methods in

class No this parameter in static methods

Code directly using instance-based members illegal…

… using static fields & methods perfectly legal

As always, can use object to access its members

Call static methods via class if protection allows

Page 5: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Abstract Methods

Methods declared abstract cannot have body IOU for subclasses which will eventually

define it abstract methods only in abstract

classes Cannot instantiate an abstract class But could still have fields & (non-abstract)

methods abstract methods declared by

interfaces Interfaces cannot declare fields public abstract methods only in

interfaces

Page 6: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Interfaces

Can only declare important constant fields public static final must be used for

fields Interface declares public abstract

methods Methods must be defined by classes

implementing it But method’s body cannot be defined in

interface

Page 7: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Interfaces

CANNOT INSTANTIATE

AN INTERFACE Only classes can be instantiated

Page 8: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Inheritance

implements & extends used for relationships Both imply there exists an IS-A relationship

public class Student extends Person {…}

public class Cat extends Mammal { … }

public class AQ<E> implements Queue<E>{…}

Page 9: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

All Java classes extend exactly 1 other class All fields & methods inherited from the

superclass Within subclass, can access non-private

members Private methods inherited, but cannot be

accessed Classes can implement any number of

interfaces Must implement methods from the

interface

Inheritance

Page 10: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Subclass can override/overload inherited methods Instance’s type determines which method is

called Parameter list stays the same to override the

method Overload method by modifying parameter list

Overriding & Hiding

Page 11: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Subclass can override/overload inherited methods Instance’s type determines which method is

called Parameter list stays the same to override the

method Overload method by modifying parameter list

Overriding & Hiding

Page 12: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Subclass can override/overload inherited methods

Instance’s type determines which method is called

Parameter list stays the same to override the method

Overload method by modifying parameter list

Overriding & Hiding

Page 13: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Exceptions in Java

throw an exception when an error detected Exceptions are objects - need an instance to throw

try executing code & catch errors to handle try only when you will catch 1 or more exceptions

Do not need to catch every exception If it is never caught, program will crash Not a bad thing – had an unfixable error!

Exceptions listed in methods’ throws clause Uncaught exception only need to be listed Should list even if thrown by another method

Page 14: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Concrete implementations used to hold data

Not ADTs Arrays are easier to use & provide

quicker access Also are impossible to grow Implementing ADTs harder due to lack of

flexibility Slower access & more complex to use

linked lists Implementing ADTs easier with increased

flexibility Can be singly, doubly, or circularly linked

Arrays vs. Linked Lists

Page 15: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Stack vs. Queue

Access data with Stack in LIFO order Last In-First Out is totally unfair (unless

always late) Data accessed in Queue using FIFO

order First In-First Out ensures early bird gets

the worm

Ord

er r

ead

if Q

ueue

Order read if S

tack

Page 16: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Queue Stack Deque

Simplest ADTs

Page 17: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Initial ADTs’ Operations

DEQUE QUEUE STACK

addFirst()addLast()

enqueue() push()

peekFirst()peekLast()

first() peek()

removeFirst()removeLast()

dequeue() pop()

Page 18: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

import java.util.Iterator;import java.lang.Iterable;

public interface Iterator<E> { E next() throws NoSuchElementException; boolean hasNext(); void remove() throws UnsupportedOperationException;}

public interface Iterable<E> { Iterator<E> iterator();}

Iterators & Iterables

Page 19: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Iterable v. Iterator

Iterable class is/has data we want to use Declaring it Iterable promises generic way

to access Does not do any work, but provides object

doing work While has access, Iterator (usually) separate

class Iterator instance returns values in other

class/array Always (almost) includes field with reference to data

holder Field (cursor) tracks next location in data to

be returned

Page 20: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Abstract work in processing with IteratorIterable<Integer> myList;Iterator<Integer> it;...for (it = myList.iterator(); it.hasNext(); ) { Integer i = it.next(); ...}

Process Iterable objects in an even easier way

...for (Integer i : myList) { ...}

More Iterator & Iterable

Page 21: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

LIST’s Operations

LIST DEQUE QUEUE STACK

*addFirst()addLast()

enqueue() push()

first()last()

peekFirst()peekLast()

first() peek()

remove()removeFirst()removeLast()

removeFirst()removeLast()

dequeue() pop()

contains() None! None! None!

Page 22: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

List Subinterfaces Defined

public interface OrderedList<T> extends List<T> {

public void add(T element);}

public interface UnorderedList<T> extends List<T> {

public void addToFront(T element); public void addToRear(T element); public void addAfter(T elem, T tgt);}

Page 23: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

LISTS are ITERABLE

List Key Idea

Page 24: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Trees vs. Binary Trees

Both represent parent-child relationships Both consist of single "root" node & its

descendants Nodes can have at most one parent

Root nodes are orphans -- do not have a parent

All others, the non-root nodes must have parent

Children not required for any node in the tree No limit to number of children for non-

binary trees 2 children for node in binary tree is the

maximum

Page 25: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order

traversal

Page 26: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order

traversal Post-order traversal does kids before doing

parents

Page 27: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order

traversal Post-order traversal does kids before doing

parents Do left kid, parent, then right kid in in-order

traversal

Page 28: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order traversal Post-order traversal does kids before doing

parents Do left kid, parent, then right kid in in-order

traversal Really, really, really simple to record what is

done Follow simple algorithm to see how it works

Page 29: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order traversal Post-order traversal does kids before doing parents Do left kid, parent, then right kid in in-order traversal

Really, really, really simple to record what is done Follow simple algorithm to see how it works Took CSC212 before you were born & I need to

trace it

Page 30: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order traversal Post-order traversal does kids before doing parents Do left kid, parent, then right kid in in-order traversal

Really, really, really simple to record what is done Follow simple algorithm to see how it works Took CSC212 before you were born & I need to trace

it Pro tip: Just $#&*@ trace it on paper

Page 31: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

BinaryTree

Picturing LinkedBinaryTree

B

B

A C

LinkedBinaryTree

root

size4

D

A C

D

Page 32: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Priority Queue ADT

Priority queue uses strict ordering of data Elements assigned priority when added to

queue Priorities used to process in completely

biased order

First you get the sugar,

then you get the power,

then you get the women

Page 33: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Priority Queue ADT

PriorityQueue yet another Collection Prioritize each datum contained in the

collection PQ is organized from lowest to highest

priority Access smallest priority only sort of like Queue find() & remove() return element

Implementation not defined: this is still an ADT Remember that organization & order is

theoretical only

Page 34: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

PriorityQueue yet another Collection Prioritize each datum contained in the

collection PQ is organized from lowest to highest

priority Access smallest priority only sort of like Queue min() & removeMin() return priority & value

Implementation not defined: this is still an ADT Remember that organization & order is

theoretical only

Priority Queue ADT

order is theoretical only

Page 35: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Heaps

Binary-tree based PQ implementation Still structured using parent-child

relationship At most 2 children & 1 parent for each node

in tree Heaps must also satisfy 2 additional

properties Parent at least as important as its children Structure must form a complete binary tree

2

95

67

Page 36: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Hints for Studying

Will NOT require memorizing: ADT’s methods Node implementations Big-Oh time proofs (Memorizing anything)

Page 37: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

You should know (& be ready to look up): How ADT implementations work (tracing &

more) For each method what it does & what it

returns Where & why each ADT would be used For each ADT implementations, its pros &

cons How to compute big-Oh time complexity

Hints for Studying

Page 38: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

1. What does the ADT do? Where in the real-world is this found?

2. How is the ADT used? What are the applications of this ADT? How is it used and why?

3. How do we implement the ADT? Given the implementation, why do we do it

like that? What tradeoffs does this implementation

make?

Studying For the Exam

Page 39: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

“Subtle” Hint

Do NOT bother with

memorizationBe ready to lookup &use information quickly

Page 40: CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP

Final Exam Schedule

Final Exam is: Tues., Dec. 10th from 8:00 – 10:ooAM in OM220

Lab Mastery Exam is:Fri., Dec. 13th from 2:45 – 3:45PM in SH1008