D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45...

31
DATA STRUCTURES 1

Transcript of D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45...

Page 1: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

DATA STRUCTURES 1

Page 2: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

INSTRUCTORS

Lectures: Dr. Shimaa Ibrahim HassanTime: Monday @ 9:[email protected]@gmail.com

Sections: Eng: Mohamed HussienTime: Sec.1 Sunday @ 9:00

Sec.2 Tuesday @ 9:00

2

Page 3: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

TEXTBOOKS

Main book: Data Structures and Algorithms in C++, 2nd edition

Goodrich, Michael T.; Tamassia, Roberto; Mount, David M.

My reference books: ADTs, Data Structures, and Problem Solving with

C++, Prentice Hall, Larry Nyhoff Data Structures And Problem Solving Using C++,

Mark Allen Weiss Data Structures and Algorithms in Java, Robert

Lafore Extra reference book:

The C++ Porgramming language, Addison Wesley, Stroustrup --- creator of C++

3

Page 4: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

COURSE OVERVIEW

A fundamental computer engineering course - Essential for programming - Essential for advanced courses A challenging course, which needs - Mathematical and logic thinking

- Programming

4

Page 5: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

COURSE PREREQUISITE Programming skills

Need to know C++ or JAVA PC programming environment Good programming skills Translate pseudo-codes into codes

Basic mathematical skills Solving recursive equations, manipulation of

symbols, etc. Computer architecture

Pointers, storage, memory access, etc.

5

Page 6: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

ASSIGNMENTS Lab assignments. Programming assignments

Due by time specified Run on PC Detailed analysis report

Late policy : Must be submitted by 12:00 am of the due date One day late costs 10% off Two days late costs 20% off Three days late costs 40 % off Four days late is not accepted

6

Page 7: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

COURSE OBJECTIVES

Study an effective programming method for software projects of realistic size. Difficulties arise not in finding a solution, but

rather in deciding on the best algorithm to use The greatest room for variability in algorithm

design is in the way in which the data are stored: How they are arranged in relation to each other Which data are kept in memory Which are calculated when needed Which are kept in files, and how the files are arranged

Present several ideas for data organization and several algorithms for important data processing tasks such as sorting and searching.

7

Page 8: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

8

COURSE OUTLINE Program development process Data modeling and ADT OOP and classes Arrays, Records and Sets Linked Lists Recursion Stacks and Queues Trees Sorting and Searching Graphs

Page 9: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

GRADING SYSTEM

Final exam ..................................... 75 degrees

Mid-term ...................................... 20 degrees

Lab and practical exam.................. 20 degrees

Project ........................................... 10 degrees

Page 10: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

PROGRAM DEVELOPMENT PROCESS

10

Page 11: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

SOFTWARE DEVELOPMENT CYCLE

Steps for writing small programs: Get the assignment Devise an algorithm for solving the problem Express the algorithm as a computer program in

a specific language Type the program into the computer Compute the program; revise it to correct errors

(compiler errors) Run the program with sample data; check for the

correct answers; correct the discovered errors (run time errors)

Run the program with actual data and get the results 11

Page 12: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

DEVELOPING A SOFTWARE SYSTEM

User requirement Between software team and user

System analysis Technical statement that shows the major

components of the system, data flow, required outputs, errors to check for, procedures to follow, constraints … etc.

System design Choosing data types and algorithms for each

major component of the system specified in the pervious stage.

Breaking the system into small functions May include writing some pseudo codes.

12

Page 13: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

DEVELOPING A SOFTWARE SYSTEM (CONT.)

Implementation The designed system is translated into code in

HLL Correcting the compiler’s error

Testing Running the system with data for which the

correct results are known and check for the output results and correcting the errors if found

Running the system with some data containing errors that requirements ask to be checked

Running the system with real data supplied by the client and fix errors if found.

13

Page 14: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

DEVELOPING A SOFTWARE SYSTEM (CONT.)

Installation The system and required software are placed on

the clients’ machines. The personnel who will operate the system are

trained Maintenance

This term includes everything that is done to the system after the user has accepted the initial version, such as: Correcting errors not detected earlier Adding new features Modification required related to H/W updates

14

Page 15: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

CHARACTERISTICS OF A GOOD PROGRAM

Correctness Reliability

Correct output for correct input Meaningful error messages for incorrect input

Portability Easily moved from one machine to another with

minimum modifications. Using popular programming language and avoiding non-standard language features

Maintainability Easily to be maintained by achieving readability

feature 15

Page 16: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

CHARACTERISTICS OF A GOOD PROGRAM (CONT.)

Readability Making the program easy to read by good

program design; using good comments and meaningful variable names

A readable program is: More likely to be correct Faster and cheaper to test Faster and cheaper to maintain Faster and cheaper to modify

Use of resources A good program is fast and uses minimum of

storage16

Page 17: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

PROGRAMING STYLE Choose meaningful names Declare all constants in the declaration section Minimize the number of global variables Declare the variable wisely in the program to

minimize used resources Use spaces, blank lines and end lines to promote

clarity Use comments intelligently:

Detailed comment at the beginning of the program and each function to describe their general purpose

Inline comments as required organize your program into functions, each with

coherent purpose The main program should be mainly function calls

17

Page 18: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

DATA MODELING & ADTS

18

Page 19: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

DATA MODELING Real-world applications need to be reduced

to a small number of existing problems (top-down design)

Real-world data need to be described in an abstract way in terms of fundamental structures

The collection of data in some organization is called a “Data Structure”

The sequences of operations to be done on the data are called “Algorithms”

An Algorithm is a procedure to do a certain task

An Algorithm is supposed to solve a general, well-specified problem

19

Page 20: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

20

DATA MODELING A real-world application is basically

Data Structures + Algorithms Data and the Operations on that data

are parts of an object that cannot be separated.

These two faces of an object are linked. Neither can be carried out independently of the other.

Page 21: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

21

THE DATA CONE

Real-world Data

ADTs

Data Structures

Fundamental Data

Types

Page 22: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

22

ABSTRACT DATA TYPES (ADTS) The most important attribute of data is its

type. Type implies certain operation. It also

prohibits other operations. For example, + - * / are allowed for types int

and double, but the modulus (%) is allowed for int and prohibited for double.

When a certain data organization + its operations are not available in the language, we build it as a new data type. To be useful to many applications, we build it as an Abstract Data Type.

Page 23: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

23

ABSTRACT DATA TYPES (ADTS) An ADT represents the logical or

conceptual level of the data. It consists of:

1. A collection of data items in some Data Structure

2. Operations (algorithms) on the data items

For example, a Stack supports retrieval in LIFO (Last In First Out) order. Basic operations are push and pop. It can be implemented using arrays (static or dynamic) or linked lists

Page 24: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

ABSTRACT DATA TYPES (ADTS)

The Data Structure used in implementing an ADT is usually dependent on the language.

In contrast, the definition of the ADT is separated from its implementation (Data Abstraction) e.g. ADT Stack can be implemented using a static array, a dynamic array or pointers.

An ADT can be used in more than one application.

24

Page 25: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

25

USING ADT’S

ADT ADT ADT ADT ADT

Application Application Application

Standard Types/Libraries User Built ADT’s

Page 26: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

A CLASSIFICATION OF ABSTRACT STRUCTURES According to the relationship between

members

Abstract Structures

Sets Linear Trees Graphs

26

Page 27: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

SETS Order of elements does not matter.

Only that they are members of the same set ({1,3,4} is identical to {1,4,3}).

Can be implemented using arrays or linked lists.

Used in problems seeking:groupscollection

27

Page 28: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

LINEAR STRUCTURES Sequential, one-to-one relationship.

Examples:Tables, Stacks, Queues, Strings

Can be implemented using arrays and linked lists (structs and pointers).

Used in problems dealing with: Searching, Sorting, stacking, waiting lines. Text processing, character sequences, patterns Arrangements, ordering, tours, sequences.

28

Page 29: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

TREES Non-Linear, hierarchical one-to-many.

Examples:Binary Trees, Binary Search Trees (BST)

Can be implemented using arrays, structs and pointers

Used in problems dealing with:SearchingHierarchyAncestor/descendant relationshipClassification 29

Page 30: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

GRAPHS Non-Linear, many-to-many. Can be implemented using arrays or

linked lists

Used to model a variety of problems dealing with:NetworksCircuitsWebRelationshipPaths 30

Page 31: D ATA S TRUCTURES 1. I NSTRUCTORS Lectures : Dr. Shimaa Ibrahim Hassan Time: Monday @ 9:45 shaimaa.rizk@feng.bu.edu.eg shimaahassan4779@gmail.com Sections.

31