lecture 41: course review

33
LECTURE 41: COURSE REVIEW CSC 212 – Data Structures

Upload: alexa-ratliff

Post on 02-Jan-2016

50 views

Category:

Documents


1 download

DESCRIPTION

CSC 212 – Data Structures. Lecture 41: Course Review. Final Exam. Thurs., Dec. 15 th from 12:30 – 2:30PM in OM 200 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 41: Course Review

LECTURE 41:COURSE REVIEW

CSC 212 – Data Structures

Page 2: Lecture 41: Course Review

Thurs., Dec. 15th from 12:30 – 2:30PM in OM 200

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: Lecture 41: Course Review

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 4: Lecture 41: Course Review

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 5: Lecture 41: Course Review

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

Field in superclass hidden by redeclaring in subclass 2 fields with the same name now in subclass Use the field for variable’s type

Overriding & Hiding

Page 6: Lecture 41: Course Review

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 7: Lecture 41: Course Review

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 8: Lecture 41: Course Review

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 9: Lecture 41: Course Review

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 10: Lecture 41: Course Review

Queue Stack Deque

Simplest ADTs

Page 11: Lecture 41: Course Review

DEQUE QUEUE STACK

addFront()addLast()

enqueue() push()

getFront()getLast()

front() top()

removeFront()removeLast()

dequeue() pop()

ADT Operations

Page 12: Lecture 41: Course Review

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 13: Lecture 41: Course Review

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 14: Lecture 41: Course Review

Collection which we can access all elements Add element before an existing one Return the 3rd element in List Loop over all elements without removing

them LIST ADTs differ in how they provide

access INDEXLIST uses indices for absolution

positioning Can only use relative positions in NODELIST

All LISTS are ITERABLE

IndexList & NodeList

Page 15: Lecture 41: Course Review

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Page 16: Lecture 41: Course Review

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Page 17: Lecture 41: Course Review

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 18: Lecture 41: Course Review

Traversal Methods

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

traversal

Page 19: Lecture 41: Course Review

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 20: Lecture 41: Course Review

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 21: Lecture 41: Course Review

Tree

D

Visualization of Tree

B

DA

C E

F

B

A F

C E

Tree

root

size6

Page 22: Lecture 41: Course Review

BinaryTree

Picturing Linked BinaryTree

B

CA

D

B

A C

D

BinaryTree

root

size4

Page 23: Lecture 41: Course Review

Priority Queue ADT

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

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 24: Lecture 41: Course Review

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 min() & removeMin() return priority & value

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

theoretical only

Page 25: Lecture 41: Course Review

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 26: Lecture 41: Course Review

Entrys in a PriorityQueue

PriorityQueues use Entry to hold data As with Position, implementations may

differ Entry has 2 items that define how it

gets used PQ will only use key – the priority given to

the Entry Value is important data to be processed by

program

Page 27: Lecture 41: Course Review

Sequence-based Priority Queue Simplest implementation of a Priority

Queue Instance of Sequence used to store Entrys

Many implementations possible for Sequence But we already know how to do that, so… Assume O(1) access and ignore all other

details But how to store Entrys in the Sequence? Order Entrys by priority within the Sequence

-OR- Sequence unordered & searched when

needed

Page 28: Lecture 41: Course Review

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 29: Lecture 41: Course Review

Hints for Studying

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

Page 30: Lecture 41: Course Review

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 31: Lecture 41: Course Review

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 32: Lecture 41: Course Review

“Subtle” Hint

Do NOT bother with

memorizationBe ready to lookup &use information quickly

Page 33: Lecture 41: Course Review

Final Exam Schedule

Lab Mastery Exam is:Tues., Dec. 13th from 8:00 – 10:00PM in OM 119

Final Exam is: Thur., Dec. 15th from 12:30 – 2:30PM in OM 200