cs 233 data structures

27
1 CS 233 Data Structures CSE, POSTECH

Upload: chill

Post on 12-Feb-2016

41 views

Category:

Documents


0 download

DESCRIPTION

CS 233 Data Structures. CSE, POSTECH. What The Course Is About?. The computer program development process requires us to represent data in an effective way develop a suitable step-by-step procedure ( algorithm ), which can be implemented as a computer program - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 233 Data Structures

1

CS 233Data Structures

CSE, POSTECH

Page 2: CS 233 Data Structures

2

What The Course Is About? The computer program development process

requires us to– represent data in an effective way– develop a suitable step-by-step procedure (algorithm),

which can be implemented as a computer program Effective data representation ⇒ data structures Development of a suitable step-by-step procedure ⇒ algorithm design methods

The study of data structures and algorithms is fundamental to computer science.

Page 3: CS 233 Data Structures

3

Prerequisites CS101 & CS103 C or C++ programming

– Those who did not learn C++ are strongly recommended to get a C++ book (e.g., Richard Johnsonbaugh and Martin Kallin, "Object-Oriented Programming in C++," 2nd Edition, Prentice Hall, 2000) and study!

Logical and analytical thinking

Page 4: CS 233 Data Structures

4

Course Homepage http://dpnm.postech.ac.kr/cs233 Announcements, handouts, syllabus, assignments, useful

links, TA info., etc. POVIS eClass

Page 5: CS 233 Data Structures

5

Questions? Questions related to lectures, concepts, data structures,

algorithms, exams…– Use CS 233 BBS in POVIS

Questions related to assignments, systems, compilers, C++ programs and syntax….

– Ask the TA

Page 6: CS 233 Data Structures

6

Organization of the Textbook Three parts Part 1 : Chapters 1-4, Background Part 2 : Chapters 5-16, Data Structures Part 3 : Chapters 17-21, Algorithm Design Methods Each chapter : Concepts + Applications

Note: This course will focus mainly on Parts 1 & 2 and introduce briefly on part 3

Page 7: CS 233 Data Structures

7

Evaluation1. Assignments - 30% 2. Midterm Exam - 30% 3. Final Exam - 35% 4. Class Participation - 5%

Note: the above evaluation scheme may change slightly during the course

Page 8: CS 233 Data Structures

8

Class Schedule: 1st halfWeek 1 : Overview of C++, Program performance Week 2 : Performance MeasurementWeek 3 : Array-based and linked representations Week 3 : Week 4 : Arrays and matricesWeek 5 : StacksWeek 6 : QueuesWeek 7 : Skip lists and hashingWeek 8 : Review and Midterm Exam

Page 9: CS 233 Data Structures

9

Class Schedule: 2nd halfWeek 9 : Binary and other treesWeek 10 : Priority queues, heaps, and leftist treesWeek 11 : Tournament trees and bin packingWeek 12 : Binary Search treesWeek 13 : AVL treesWeek 14 : GraphsWeek 15 : Graph Search MethodsWeek 16 : Review and Final exam

Page 10: CS 233 Data Structures

10

Lecture 1: Programming in C++ Introduction “A better C” Support for data abstraction Support for object-oriented programming

Page 11: CS 233 Data Structures

11

Introduction C++ programming language is designed to

– Be a better C ⇒better support for procedural and modular programming

– Support data abstraction ⇒ability to define and use new data types (classes)

– Support object-oriented programming ⇒ability to express type hierarchies

Page 12: CS 233 Data Structures

12

A Better C Program and Output

#include <iostream.h>int main(){

cout << “Hello, World!\n”;}

Page 13: CS 233 Data Structures

13

A Better C Variables and Arithmetic

double d;int i;short s;// ..useful comments

d = d+i;i = s*i;

Page 14: CS 233 Data Structures

14

A Better C Pointers and Arrays

char v[10]; // array of 10 characterschar* p; // pointer to characterp = &v[3]; // p points to v’s fourth element

Page 15: CS 233 Data Structures

15

A Better C Tests and Loops

char ch = 0;cin >> ch;if (ch == ‘i’) { /* … */ }else if (ch == ‘c’) { /* … */ }

else { /* … */ }

int v1[10];int v2[10];// …for (int i = 0; i < 10; i++)

v1[i] = v2[i];

Page 16: CS 233 Data Structures

16

A Better C

Functions

int pow(int, int);double pow(double, double);//…x = pow(2,10);y = pow(2.0, 10.0);

Page 17: CS 233 Data Structures

17

A Better C Functions (swap)template<class T>void Swap1(T *a, T *b){T temp = *a; *a = *b; *b = temp;}

template<class T>void Swap2(T& a, T& b){T temp = a; a = b; b = temp;}

template<class T> void Swap3(T a, T b){T temp = a; a = b; b = temp;}

What would be the output for the following calls?

int a = 2, b = 3; Swap1(&a, &b); cout << a << ' ' << b << endl; 3 2 Swap2(a, b); cout << a << ' ' << b << endl; 2 3

Swap3(a, b); cout << a << ' ' << b << endl; 2 3

Page 18: CS 233 Data Structures

18

A Better C Modules

#include <math.h>#include “math1.h”

extern “C” double sqrt(double);extern void f();int main(){

cout << sqrt(4) << endl;cout << f() << endl;

}

%g++ main.C f.C –o silly

Page 19: CS 233 Data Structures

19

Support for Data Abstraction Initialization and Cleanup

class vector {int sz; // number of elementsint *v; // pointer to integers

public:vector(int); // constructor~vector(); // destructorint& operator[](int index);

};

Page 20: CS 233 Data Structures

20

Support for Data Abstraction Initialization and Cleanup

vector::vector(int s){

if (s <= 0) error(“bad vector size”);sz = s;v = new int[s]; // allocate an array of s integers

}vector::~vector(){

delete[] v; // deallocate the array // pointed to by v

}

Page 21: CS 233 Data Structures

21

Support for Data Abstraction Assignment and Initialization

class vector {int sz;int *v;

public:// …vector(const vector&); // initializationvoid operator=(const vector&); // assignment

};

Page 22: CS 233 Data Structures

22

Support for Data Abstraction Assignment and Initialization

void vector::operator=(const vector& a){

if (sz != a.sz) error(“bad vector size for =”);for (int i = 0; i < sz; i++) v[i] = a.v[i];

}

vector::vector(const vector& a){

sz = a.sz; // same sizev = new int[sz];for (int i = 0; i < sz; i++) v[i]= a.v[i];

}

Page 23: CS 233 Data Structures

23

Support for Data Abstraction Templates

template<class T> class Vector { // vector of Tsint sz;T* v;

public:Vector(int s) { if (s <= 0) error(“bad Vector size”); v = new T[sz=s]; // allocate an array of s Ts

}T& operator[](int i);int size() { return sz; }// …

}

Page 24: CS 233 Data Structures

24

Support for Data Abstraction Templates

void f(){

Vector<int> v1(100); // vector of 100 integersVector<complex> v2(200); // vector of 200 complex

// numbers

v2[i] = complex(v1[x], v1[y]);// …

}

Page 25: CS 233 Data Structures

25

Support for Data Abstraction Exception Handling

try {int **x = new int* [10];for (int i = 0; i < 10; i++)

x[i]= new int [5];return true;

} catch (xalloc) {return false;

}

Page 26: CS 233 Data Structures

26

Support for Object-Oriented Programming

Inheritance– Superclass (or base class) and subclass (derived

class)– inherits attributes and functions of base class

class my_task : public task {// my stuff

};class my_displayed : public displayed {

// my stuff};class my_displayed_task : public displayed, public task {

// my stuff};

Page 27: CS 233 Data Structures

27

Access Control1. Public: can access from other class functions2. Private: can only be accessed from within the class

member functions3. Friend: can be accessed by other classes or functions 4. Protected: behave like private members except that

derived classes can access protected members

Note that all source codes in the textbook can be found in http://www.cise.ufl.edu/~sahni/dsaac/

READING: Chapter 1

Support for Object-Oriented Programming