Data Structures
Chapter 1
Introduction
Data Structures
• A data structure is a scheme for organizing data in the
memory of a computer.
• Common data structures: lists, stacks, queues, heaps,
trees, and graphs.
• The way in which the data is organized affects the
performance of a program for different tasks (as you will
see shortly).
• Computer programmers decide which data structures to
use based on the nature of the data and the operations
performed on that data.
2
Example: A Queue
• A queue is an example of commonly used simple data
structure. A queue has beginning and end, called the
front and back of the queue.
• Data enters the queue at one end and leaves at the
other, also called FIFO (First in First out).
• Ex: people in a checkout line at a supermarket.
3
Example: A Binary Tree
• A binary tree is another
commonly used data
structure. It is organized
like an upside down tree.
• Each spot on the tree,
called a node, holds an
item of data along with a
left pointer and a right Binary Tree
pointer.
4
Choosing Data Structures
• By comparing the queue with
the binary tree, you can see
how the structure of the data
affects what can be done
efficiently with the data.
5
Choosing Data Structures
• Queue: good to use for
storing things that need to
be kept in order, ex: set of
documents waiting to be
printed on a network printer.
• The jobs will be printed in
the order in which they are
received.
• Most networked print
servers maintain such a
print queue.
6
Choosing Data Structures
• A binary tree is a good data
structure to use for searching
sorted data.
• Also for indexing in databases.
7
Design and Analysis
• Data Structures
– How to efficiently store, access, and manage data
– Data structures affect algorithm’s performance
• Algorithm Design and Analysis
– How to predict an algorithm’s performance
– How well an algorithm scales up
– How to compare different algorithms for a problem
8
Algorithm Analysis: The Big-O Notation
• Analyze algorithm after design
• Example
– 50 packages delivered to 50 different houses
– 50 houses one mile apart, in the same area
FIGURE 1-1 Gift shop and each dot representing a house
9
Algorithm Analysis: The Big-O Notation
(cont’d.)
• Example (cont’d.)
– Driver picks up all 50 packages
– Drives one mile to first house, delivers first package
– Drives another mile, delivers second package
– Drives another mile, delivers third package, and so on
– Distance driven to deliver packages
• 1+1+1+… +1 = 50 miles
– Total distance traveled: 50 + 50 = 100 miles
FIGURE 1-2 Package delivering scheme
10
Algorithm Analysis: The Big-O Notation
(cont’d.)
• Example (cont’d.)
– Similar route to deliver another set of 50 packages
• Driver picks up first package, drives one mile to the first
house, delivers package, returns to the shop
• Driver picks up second package, drives two miles,
delivers second package, returns to the shop
– Total distance traveled
• 2 * (1+2+3+…+50) = 2550 miles
FIGURE 1-3 Another package delivery scheme
11
Algorithm Analysis: The Big-O Notation
(cont’d.)
• Example (cont’d.)
– n packages to deliver to n houses, each one mile
apart
– First scheme: total distance traveled
• 1+1+1+… +1+n=2n
• Function of n
– Second scheme: total distance traveled
• 2 * (1+2+3+…+n) = 2*(n(n+1) / 2) = n2+n
• Function of n2
12
Algorithm Analysis: The Big-O Notation
(cont’d.)
• Analyzing an algorithm
– Count number of operations performed
• Not affected by computer speed
TABLE 1-1 Various values of n, 2n, n2, and n2 + n
13
Algorithm Analysis: The Big-O Notation
(cont’d.)
• Example 1-1
– Illustrates fixed number of executed operations
Total operations = 1+2+1+1+3 = 8
In this example, we compare two numbers to find out which one is maximum. However, this is
not our main objective. Our objective is to find the total number of operations. 14
Algorithm Analysis: The Big-O Notation
– Illustrates dominant operations
If the loop runs for 10 times, Total operations = 5(10) + 1 + 5 + 9 or 5(10) + 1 + 5 + 8
=> 5(10) + 15 or 5(10) + 14
If the loop executes n times, then 5n + 15 or 5n + 14
In these expressions, for very large values of n, the term 5n becomes the dominating term
and the terms 15 and 14 become negligible.
15
Algorithm Analysis: The Big-O Notation
• Example of Search algorithm
– n: represents list size
– f(n): count function
– c: computer time to execute one operation
– c . f(n): computer time to execute f(n) operations
Constant c depends on computer speed (varies)
f(n): number of basic operations (constant)
– We can determine algorithm efficiency by
• Knowing how function f(n) grows as problem size grows
16
Algorithm Analysis: The Big-O Notation
TABLE 1-2 Growth rates of various functions
17
Algorithm Analysis: The Big-O Notation
(cont’d.)
TABLE 1-3 Time for f(n) instructions on a
computer that executes 1 billion
instructions per second
Figure 1-4 Growth rate
of functions in Table 1-3
18
Algorithm Analysis: The Big-O Notation
• Notation useful in describing algorithm behavior
– It shows how a function f(n) grows as n increases
without bound
• Asymptotic
– Study of the function f as n becomes larger and larger
without bound
– Examples of functions
• g(n)=n2 (no linear term)
• f(n)=n2 + 4n + 20
19
Algorithm Analysis: The Big-O Notation
(cont’d.)
• As n becomes larger and larger
– Term 4n + 20 in f(n) becomes insignificant
– Term n2 becomes dominant term
TABLE 1-4 Growth rate of n2 and n2 + 4n + 20n
20
Algorithm Analysis: The Big-O Notation
• Algorithm analysis
– If function complexity can be described by complexity
of a quadratic function without the linear term
• We say the function is of O(n2) or Big-O of n2
21
Algorithm Analysis: The Big-O Notation
TABLE 1-5 Some Big-O functions that appear in algorithm analysis
22
Classes
• OOD first step: identify components (objects)
• Encapsulation: object combines data and data
operations in a single unit
• Class: collection of a fixed number of components
– Called class members (variables and functions)
– Class member categories (access specifiers)
• Private, public, protected
23
Classes (cont’d.)
• Constructors
– Declared variables are not automatically initialized
• They should be initialized in the constructors
– Overloaded constructor: constructor with parameters
– Default constructor: constructor without parameters
– Properties
• Constructor name equals class name
• Constructor has no return type
• All class constructors have the same name
• Multiple constructors: different formal parameter lists
• Execute automatically: when class object enters its scope
• Execution: depends on values passed to class object
24
Classes (cont’d.)
• Unified Modeling Language diagrams
– Graphical notation describing a class and its
members
– Private and public members
Class
Data
Members
and their Member function
data types name, parameter
list, return type of
function
+ public member
- Private member
FIGURE 1-5 UML class diagram of the class clockType
25
Classes (cont’d.)
• Variable (object) declaration
– Once class defined
• Variable declaration of that type allowed
– Class variable
• A.K.A. class object, class instance, or object in C++
– A class can have both types of constructors (default
and overloaded)
– Upon declaring a class object
• Default constructor executes or constructor with
parameters executes
26
Classes (cont’d.)
• Accessing class members
– When an object of a class is declared
• Object can access class members
– Member access operator
• The dot, . (period) [if ptr object, then -> is used]
– Only public members are accessed outside the class,
but not private.
27
Classes (cont’d.)
• Implementation of member functions
– Function prototype often included for member
functions
• Function definition can be long, difficult to comprehend
• Providing function prototypes hides data operation
details
– Writing definitions of member functions
• Use scope resolution operator, :: (double colon), to
reference identifiers local to the class
28
Classes (cont’d.)
• Implementation of member functions (cont’d.)
– Example: definition of the function setTime
29
Classes (cont’d.)
• Execute statement
myClock.setTime(3,48,52);
FIGURE 1-6 Object myClock after the statement
myClock.setTime(3, 48, 52); executes
30
Classes (cont’d.)
– Example: definition of the function equalTime
? clockType::equalTime(?)
{
31
Classes (cont’d.)
– Objects of type clockType
• myClock and yourClock
FIGURE 1-7 Objects myClock and yourClock
32
Classes (cont’d.)
if(myClock.equalTime(yourClock)) …
• Object myClock accesses member function
equalTime
• otherClock is a reference parameter
• Address of actual parameter yourClock passed to the
formal parameter otherClock
FIGURE 1-8 Object myClock and parameter otherClock
33
Classes (cont’d.)
– equalTime execution
– Variables hr , min , sec in equalTime function body
• Instance variables of variable myClock
– Once class properly defined, implemented
• Can be used in a program
– Client
• Program or software using and manipulating class
objects
– Instance variables
• Have own instance of data
34
Classes (cont’d.)
• Reference parameters and class objects (variables)
– Variable passed by value
• Formal parameter copies value of the actual parameter
– Changing one will not affect the other.
– Variable passed by reference
• Corresponding formal parameter receives only the
address of the actual parameter
• It is always desirable to pass objects by reference
35
Classes (cont’d.)
• If a variable or object is passed by reference then
– When the formal parameter changes, the actual
parameter also changes.
– Sometimes, however, you do not want this to happen
– In C++, you can pass a variable or object by reference
and still prevent the function from changing its value by
using the keyword const in the formal parameter
declaration.
36
Classes (cont’d.)
– Two built-in operations
• Member access (.) [to use a class member: done]
• Assignment (=)
• Assignment operator and classes
– Assignment statement performs a memberwise copy
– Example: myClock = yourClock;
• Values of the three instance variables of yourClock
copied into corresponding instance variables of
myClock
37
Classes (cont’d.)
• Class scope
– Automatic
• Objects created each time when control reaches
declaration
• Destroyed when control exits surrounding block
– Static
• Created once when control reaches declaration
• Destroyed when program terminates
38
Classes (cont’d.)
• Functions and classes
– Rules
• Class objects can be passed as parameters to
functions and returned as function values
• Class objects can be passed either by value or by
reference as parameters to functions
• Class objects passed by value: instance variables of
the actual parameter contents are copied into the
corresponding formal parameter instance variables
39
Classes (cont’d.)
• Constructors and default parameters
– Constructors can have default parameters
– Rules declaring formal parameters
• Same as declaring function default formal parameters
– Actual parameters passed with default parameters
• Use rules for functions with default parameters
– Default constructor
• No parameters or all default parameters
40
Classes (cont’d.)
• Destructors
– Functions
– No type
– Neither value-returning nor void function
– One destructor per class
• No parameters
– Name
• Tilde character (~) followed by class name
– Automatically executes
• When class object goes out of scope
41
Classes (cont’d.)
• Structs
– Special type of classes
– All struct members public
– C++ defines structs using the reserved word struct
– If all members of a class are public, C++
programmers prefer using struct to group the
members
– Defined like a class
42
Data Abstraction, Classes, and Abstract
Data Types
• Abstraction
– Separating design details from use
• Data abstraction
– Process
• Separating logical data properties from implementation
details
• Abstract Data Type (ADT)
– Data type separating logical properties from
implementation details
– Includes type name, domain, set of data operations
43
Data Abstraction, Classes, and Abstract
Data Types (cont’d.)
• ADT
– Example: defining the clockType ADT
44
Data Abstraction, Classes, and Abstract
Data Types (cont’d.)
• Implementing an ADT
– Represent the data; write algorithms to perform
operations
– C++ classes specifically designed to handle ADTs
45
Identifying Classes, Objects, and
Operations
• Object-oriented design
– Hardest part
• Identifying classes and objects
• Technique to identify classes and objects
– Begin with problem description
– Identify all nouns and verbs
• From noun list: choose classes
• From verb list: choose operations
46