data structures introduction 1. welcome to data structures! what are data structures and algorithms?...

37
Data Structures Introduction 1

Upload: easton-pennell

Post on 14-Dec-2015

334 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Data Structures

Introduction

1

Page 2: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Welcome to Data Structures!What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms Some DefinitionsObject-Oriented ProgrammingProblems with Procedural Software EngineeringJava for C++ ProgrammersJava Library Data StructuresSummary

2

Page 3: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Definition of a Data StructureA data structure is an arrangement of data

in a computer’s memory (or disk).Question to ponder:

What are some examples of data structures you already know about from your Java course?

3

Page 4: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Definition of an AlgorithmAn algorithm provides a set of instructions

for manipulating data in structures.Questions to ponder:

What’s an example of an algorithm?

How can the design of an algorithm affect performance? How can it affect memory?

4

Page 5: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Data Structure or Algorithm?Linked List

Sort

Search

Stack

Vector

5

Page 6: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Real World Data StorageReal-world data: data that describes physical

entities external to the computer. Can we think of some examples?

What’s an example of non-computer data storage?Addresses and phone #s: Book names, titles, ISBNs:

What method do these use?

6

Page 7: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Real World Data StorageSay we wanted to convert one of these

systems to a computer program, what must we consider?Memory consumption:

Scalability:

Algorithms (which ones do we care about?):

7

Page 8: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Programmer ToolsDo not store real-world data, but perform some

function internal to the machine. Example - A ‘stack’ of data, where I can only

insert or remove elements from the top:

What programmer tool would this be useful for? Hint: Think in terms of a variables in a program.

8

Page 9: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Real World ModelingEffectively, ‘simulate’ a real-world situation.For example, what could the following

represent:

9

Page 10: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Real World ModelingHow about airline routes?This type of structure is called an

‘undirected graph’

We’ll cover it!!10

Page 11: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Real World ModelingHow about a ‘queue’ of data, where the first

element in is the first element out (termed ‘FIFO’):

Example applications:Grocery store linesTraffic (Queues are actually used when

determining timing of traffic lights!! How? Let’s think about it)

11

Page 12: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Data Structure Trade-offsA structure we have dealt with before:

arraysRequirement that is enforced:

Arrays store data sequentially in memory.Let’s name the advantages (i.e., when is an

array efficient?)

Let’s name the disadvantages

12

Page 13: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Overall Costs for Structures We’ll StudyStructure

Access Search Insert Delete Impl. Memory

Array Low High Med High Low Low

Ord. Array

Low Med High High Med Low

Linked List

High High Low Low Med Med

Stack Med High Med Med Med Med

Queue Med High Med Med Med Med

Bin. Tree

Med Low Low Low High High

R-B Tree

Med Low Low Low Very High

High

234 Tree

Med Low Low Low Very High

High

Hash Table

Med Med Low High Low High

Heap Med Med Low Low High High

Graph High High Med Med Med Med

Point that you should be getting: No ‘universal’ data structure!!!13

Page 14: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Algorithms We’ll StudyInsertion/Searching/DeletionIteration. Java contains data types called

iterators which accomplish this.Sorting. Believe it or not, there LOTS of ways

to do this! So much, you get two chapters on it. :p

Recursion. What’s the key attribute of a recursive function

in Java?method

This technique will be used to develop some of the afore mentioned algorithms.

14

Page 15: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

DatabasesA database refers to all data that will be dealt

with in a particular situation. We can think of a database as a table of rows and columns:

What is the ‘database’ in the case of a collection of index cards and names/phone #s? address book

15

Page 16: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Database RecordsA record is the unit into which a database is

divided. How is a record represented in a table:

What would be a ‘record’ in a stack of index cards? What about a banking system? How about a cookbook?

What basic construct is used to create records in Java?16

Page 17: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Database FieldsA field is the unit into which a record is

divided. What represents a field in our table:

What would some example fields be for a banking system?

A clothing catalogue?

17

Page 18: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Database KeysGiven a database (a collection of records), a

common operation is obviously searching. In particular we often want to find a single particular record. But what exactly does this mean? Each record contains multiple fields, i.e.:

So how do we designate a record? We need something unique to each record, that’s the key. What is the key above?

Last First Acct No.

City State Zip

Azuma Goro 123456789

St. Pete FL 33712

Smith John 867530912

Clearwater

FL 33720

Gunnz JT 102938475

South Bend

IN 46545

Zozzfuzzel

Ziggy 000000000

St. Pete FL 6158918

Page 19: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Database KeysSo if I wanted to search for a particular

record, I would want to do it by key (i.e. Acct No).

Note that searching by anything else could yield multiple records.

Last First Acct No.

City State Zip

Azuma Goro 123456789

St. Pete FL 33712

Smith John 867530912

Clearwater

FL 33720

Gunnz JT 102938475

South Bend

IN 46545

Zozzfuzzel

Ziggy 000000000

St. Pete FL 6158919

Page 20: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Java.util PackageIncludes Vector, Stack, Dictionary, and

Hashtable. We won’t cover these particular implementations but know they are there and accessible through:import java.util.*;You may not use these on homeworks unless

I explicitly say you can. Several other third-party libraries availableA central purpose of Java

20

Page 21: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Review of Object-Oriented ProgrammingProcedural Programming Languages

Examples: C, Pascal, early BASICWhat is the main unit of abstraction?

Object-Oriented Languages:Examples: C++, Java, C#What is the main unit of abstraction?

OOP was found to be inadequate for large and complex programs.

Obviously procedural languages weren’t good enough in all cases. Let’s rediscover why.

21

Page 22: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Main Limitations of Procedural Programming1. Poor Real World Modeling. Let’s discuss why.

The thermostat on your furnace, for example, carries out tasks (turning the furnace on and off) but also stores information.If you wrote a thermostat control program in a procedural language, you might end up with two methods, furnace_on() and furnace_off(), but also two global variables, currentTemp (supplied by a thermometer) and desiredTemp (set by the user). However, these methods and variables wouldn’t form any sort of programming unit; there would be no unit in the program you could call thermostat.

2. Crude Organization. Let’s discuss why.Procedural programs were organized by dividing the code into methods. To simplify slightly, data could be local to a particular method, or it could be global—accessible to all methods. There was no way (at least not a flexible way) to specify that some methods could access a variable and others couldn’t.

22

Page 23: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Idea of ObjectsA programming unit which has associated:

Variables (data), andMethods (functions to manipulate this data).

How does this address the two problems on the previous slide?Real World Modeling

Organization

23

Page 24: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Idea of Classes (Java, C++)Objects by themselves can create lots of

redundancy. Why?

With a class, we specify a blueprint for one or more objects. For example, in Java (cf. book page 16):

class thermostat { private float currentTemp; private float desiredTemp;

public void furnaceOn() { … }}

24

Page 25: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Instances of a ClassJava creates objects using the new

keyword, and stores a reference to this instance in a variable:

thermostat therm1;therm1 = new thermostat();thermostat therm2 = new thermostat();

Object creation (or instantiation) is done through the constructor.

Which constructor did we use here?

25

Page 26: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Invoking Methods of an ObjectParts of the program external to the class

can access its methods (unless they are not declared public):

Dot operator:therm2.furnace_on();

Can I access data members similarly?therm2.currentTemp = 77;

What would I need to change to do so?Is this change good programming practice?How, ideally, should data members be

accessed?

26

Page 27: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Another ExampleIf you have your book, look at the

BankAccount class on page 18. If you don’t have it, don’t worry I’ll write it on the board.

Look at the output. Let’s go over why this is generated.

27

Page 28: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

InheritanceCreation of one class, called the base classCreation of another class, called the derived class

Has all features of the base, plus some additional features.

Example:A base class Animal may have associated methods

eat() and run() and variable name, and a derived class Dog can inherit from Animal, gaining these methods plus a new method bark().

If name is private, does Dog have the attribute?How do we enforce Dog to also have attribute

name?

28

Page 29: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

PolymorphismIdea: Treat objects of different classes in

the same way.What’s the requirement?

Let’s return to an example with Animal and Dog, and throw in another derived class Cat.

29

Page 30: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Final Review of some Java ConceptsDifference between a value and a reference:int intVar;BankAccount bc1;

Which is a value and which is a reference?

How did the interpreter know to allocate them differently?

What does the address stored in bc1 contain right now?

What must I do before I use bc1?

30

Page 31: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Java AssignmentsWhat must be noted about the following code

snippet:

int intVar1 = 45;int intVar2 = 90;BankAccount bc1(72.45);BankAccount bc2 = bc1;intVar1 = intVar2;intVar1 = 33; // Does this modify intVar2?bc2.withdraw(30.00); // Does this modify

object bc1?

31

Page 32: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Java Garbage CollectionWhen is the memory allocated to an object

reclaimed in Java?

Code like this would leak memory in C++, but does not in Java because of the garbage collector:

while (true) { Integer tmp = new Integer(); …}

32

Page 33: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Passing by Value vs. ReferenceSame idea:void method1() { BankAccount ba1 = new

BankAccount(350.00); float num = 4;}void method2(BankAccount acct) { … }void method3(float f) { … }

If I change f in method3, does that affect num?If I change acct in method2, does that affect

object ba1?

33

Page 34: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

== vs. equals()carPart cp1 = new carPart(“fender”);carPart cp2 = cp1;// What’s the difference between this:if (cp1 == cp2) System.out.println(“Same”);

// And this:if (cp1. equals(cp2) System.out.println(“Same”);Does “Same” print twice, once, or not at

all?

34

Page 35: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Primitive Sizes and Value Ranges

Source: roseindia.net35

Page 36: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Screen OutputSystem.out is an output stream which

corresponds to standard output, which is the screen:

int var = 33;// What’s do these three statements

print?System.out.print(var);System.out.println(var);System.out.println(``The answer is `` +

var);36

Page 37: Data Structures Introduction 1. Welcome to Data Structures! What Are Data Structures and Algorithms? Overview of Data Structures Overview of Algorithms

Keyboard InputPackage: java.ioRead a string:

Read a character:

Read an integer:

Read a float:

InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);String s = br.readLine();

char c = s.charAt(0);

int i = Integer.parseInt(s);

double d = Double.valueOf(s).doubleValue();

37