Download presentation
Presentation is loading. Please wait.
Published byMelissa Welch Modified over 9 years ago
1
CSC 212 – Data Structures
2
Fri., Dec. 14 th from 2:45 – 4:45PM in SH1028 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
3
Classes vs. Objects Classes are blueprints describing data type static By itself, class only used for static fields & methods Objects are instances of a class new New objects created (instantiated) using new Fields describe state of an object Object’s behavior represented by methods
4
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
5
Abstract Methods abstract Methods declared abstract cannot have body IOU for subclasses which will eventually define it abstract abstract abstract methods only in abstract classes abstract Cannot instantiate an abstract class But could still have fields & (non-abstract) methods abstract abstract methods declared by interfaces Interfaces cannot declare fields public abstract public abstract methods only in interfaces
6
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
7
Interfaces CANNOT INSTANTIATE AN INTERFACE Only classes can be instantiated
8
Inheritance implements extends 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 implements Queue {…}
9
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
10
Subclass can override/overload inherited methods Instance’s 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
11
Subclass can override/overload inherited methods Instance’s 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
12
Subclass can override/overload inherited methods Instance’s 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
13
Exceptions in Java throw throw an exception when an error detected Exception s are objects - need an instance to throw trycatch try executing code & catch errors to handle trycatch try only when you will catch 1 or more exceptions catch Do not need to catch every exception If it is never caught, program will crash Not a bad thing – had an unfixable error! throws Exceptions listed in methods’ throws clause Uncaught exception only need to be listed Should list even if thrown by another method
14
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
15
Stack vs. Queue Order read if Queue Order read if Stack
16
QueueStackDeque Simplest ADTs
17
D EQUE Q UEUE S TACK addFront() addLast() enqueue()push() getFront() getLast() front()top() removeFront() removeLast() dequeue()pop() ADT Operations
18
import java.util.Iterator; import java.lang.Iterable; public interface Iterator { E next() throws NoSuchElementException; boolean hasNext(); void remove() throws UnsupportedOperationException; } public interface Iterable { Iterator iterator(); } Iterators & Iterables
19
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
20
Abstract work in processing with Iterator Iterable myList; Iterator 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
21
Collection which we can access all elements Add element before an existing one Return the 3 rd element in List Loop over all elements without removing them L IST ADTs differ in how they provide access I NDEX L IST uses indices for absolution positioning Can only use relative positions in N ODE L IST All L ISTS are I TERABLE IndexList & NodeList
22
Sequence ADT Combines D EQUE, I NDEX L IST, & P OSITION L IST 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
23
Sequence ADT Combines D EQUE, I NDEX L IST, & P OSITION L IST 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
24
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 must 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
25
Traversal Methods Many traversals, differ in order nodes visited Do parent then do each kid in pre-order traversal
26
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
27
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
28
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
29
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
30
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
31
Tree D Visualization of Tree B D A CE F B AF CE Tree root size 6
32
BinaryTree Picturing Linked BinaryTree B C A D BACD BinaryTree root size 4
33
Priority Queue ADT Priority queue uses strict ordering of data Values assigned priority when added to the queue completely biased order Priorities used to process in completely biased order First you get the sugar, then you get the power, then you get the women
34
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
35
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
36
Entry s 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
37
Sequence -based Priority Queue
38
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
39
Hints for Studying Will NOT require memorizing: ADT’s methods Node implementations Big-Oh time proofs (Memorizing anything)
40
(& be ready to look up): 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
41
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
42
“Subtle” Hint
43
Final Exam Schedule Lab Mastery Exam is: Tues., Dec. 11 th from 3:45 – 4:45PM in SH1008 Final Exam is: Fri., Dec. 14 th from 2:45 – 4:45PM in SH1028
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.