DOC PREVIEW
Penn CIT 594 - CIT 594 Quick Overview

This preview shows page 1-2-3-21-22-23-43-44-45 out of 45 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CIT 594 Quick OverviewPointers and ReferencesMachine addressesC (and C++) vs. JavaReferencesData structuresA trivial exampleA more serious exampleBinary trees in JavaSize of objectsThe magic of VectorsVector’s secret trickPowerPoint PresentationSlide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Types of AlgorithmsAlgorithm classificationA short list of categoriesAnalysis of AlgorithmsTime and spaceWhat does “size of the input” mean?Characteristic operationsExact valuesHigher-level languagesAverage, best, and worst casesConstant timeLinear timeConstant time is (usually) better than linear timeWhat about the constants?Simplifying the formulaeBig O notationBig O for subset algorithmCan we justify Big O notation?y = x2 + 3x + 5, for x=1..10y = x2 + 3x + 5, for x=1..20Common time complexitiesThe EndJan 12, 2012CIT 594 Quick OverviewJan 12, 2012Pointers and ReferencesMachine addressesComputer memory consists of one long list of addressable bytesA pointer is a data item that contains an address3FA71CF23FA71CF33FA71CF43FA71CF53FA71CF63FA71CF73FA71CF83FA71CF93FA71CFA3FA71CFB3FA71CFC3FA71CFD3FA71CFE3FA71CFF3FA71D003FA71D01::3FA71CF6• A reference is a data item that contains an address3FA71CF6• C has pointers, but Java has references• So what’s the difference?C (and C++) vs. JavaIn C you can dereference (follow) a pointerIn Java you can dereference (follow) a referenceIn C you can assign one pointer variable to anotherIn Java you can assign one reference variable to anotherIn C you can determine whether one pointer is larger than, equal to, or smaller than another pointerIn Java you can determine whether one reference is equal to another referenceIn C you can create a pointer to anythingIn Java you can have references to objectsIn C you can do integer arithmetic on pointersReferencesAn Abstract Data Type (ADT) has a set of values and a set of operations on those valuesPointers and references have the same set of values (memory addresses)Pointers have more defined operations than referencesPointers are more flexible and more general than referencesReferences are safer than pointers (from error or malicious misuse)References allow automatic garbage collection (pointers don’t)A (non-abstract) Data Type also has an implementationThe implementations of pointers and references are similarJava references carry information about the thing referenced; in C, it’s up to the compiler to figure out what it canData structuresBasically, pointers and references are the same thing; they point to (refer to) something else in memoryA Data Structure is a description of how data is organized in memoryMany (not all) data structures are built from objects pointing/referring to one anotherUnderstanding pointers (references) is fundamental to this courseIf this course were taught in C or C++ instead of Java, all the “nuts and bolts” would be the sameThis course is in Java, but it’s not about JavaYou need to know how to create your own data structuresI will also teach some Java-specific packagesIn real life, it’s stupid to redo work that’s already been done for youA trivial exampleWe use a lot of references in Java:class Person { String name; Person spouse; Person (String n) { name = n; }}…Person john = new Person("John");Person mary = new Person("Mary");john.spouse = mary;mary.spouse = john;"John""Mary"johnnamespousemarynamespouseA more serious exampleA binary tree is a data structure in which every node (object) has zero, one, or two children (references to other nodes)Arithmetic expressions can be represented as binary treesTo evaluate an arithmeticexpression:If it is a leaf, return its valueOtherwise, evaluate its two subtrees, and perform the indicated operation+/12nullnull4nullnull7nullnullA binary tree representing thearithmetic expression 12/4+7Binary trees in Javapublic class BinaryTree { public Object value; // the information in this node private BinaryTree leftChild; private BinaryTree rightChild; // Constructors and methods...}To make binary trees as useful as possible, we make the value in a node an ObjectAs implementers of the BinaryTree class, our job is to make sure that the structure of the binary tree is always validWe don’t really care what the user puts in the value fieldSize of objectsObjects in Java have, generally speaking, a fixed sizeThe size of all primitives is knownObjects don’t actually “contain” other objects—they just have references to other objects, and a reference is 4 bytesSo what about Vectors?A Vector is like an array, but it gets bigger as you put things into itA Vector actually has two “sizes”—its capacity, which is how many references it can hold, and its size, which is how many references are in it right nowWhen you exceed the capacity of a Vector, Java creates a new Vector for youThe magic of VectorsSuppose you have two references to a Vector, and you use one of them to add elements to the VectorWhat happens if Java decides to replace this Vector with a bigger one?It looks like the second reference is a “dangling pointer,” referring to nothingThis doesn’t happen! Java protects you from this errorBut how?v1v2Vector’s secret trickA reference to a Vector is actually a reference to a reference to a VectorIn this way, the “real” reference has to be changed in only one place, and all the other, indirect references automatically workIt’s clever, but it isn’t magicv2v1Jan 12, 2012Recursion14Definitions IA recursive definition is a definition in which the thing being defined occurs as part of its own definitionExample:An atom is a name or a number A list consists of:An open parenthesis, "("Zero or more atoms or lists, andA close parenthesis, ")"15Definitions IIIndirect recursion is when a thing is defined in terms of other things, but those other things are defined in terms of the first thingExample: A list is:An open parenthesis,Zero or more Symbolic expressions, andA close parenthesisA Symbolic expression is an atom or a list16Recursive functions...er, methodsThe mathematical definition of factorial is:We can define this in Java as:long factorial(long n) { if (n <= 1) return 1; else return n * factorial(n – 1);}This is a recursive function because it calls


View Full Document

Penn CIT 594 - CIT 594 Quick Overview

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download CIT 594 Quick Overview
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CIT 594 Quick Overview and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CIT 594 Quick Overview 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?