Académique Documents
Professionnel Documents
Culture Documents
A A B C D E
B
A C F
D
c Tim French
! CITS2200 Introduction to Data Structures Slide 3 c Tim French
! CITS2200 Introduction to Data Structures Slide 4
Why so many? 2.2 Abstract Data Types
Space efficiency
Allow user to abstract away from implementation detail.
Time efficiency:
Consider the statement: I put my lunch in my bag and went to Uni.
store (add to collection)
What is meant by the term bag in this context?
search (find an object)
retrieve (read information) Most likely it is a backpack, or satchel, but it could also be a hand bag, shopping
bag, sleeping bag, body bag . . . (but probably not a bean bag).
remove or replace
clone (make a copy) It doesnt actually matter. To parse the statement above, we simply understand
that a bag is something that we can
Algorithm 1:
Simplicity and Clarity
Look through each word in turn until you find a match.
All things being equal we prefer simplicity, but they rarely are. . .
Algorithm 2:
Space Efficiency
go to half way point
space occupied by data overheads compare your word with the word found
space required by algorithm (eg recursion) if < repeat on earlier half
can it blow out? else > repeat on later half
c Tim French
! CITS2200 Introduction to Data Structures Slide 7 c Tim French
! CITS2200 Introduction to Data Structures Slide 8
Performance 2.4 ADTs and Java
number of Algorithm 1 Algorithm 2 Java classes are ideal for implementing ADTs.
words max. comparisons max. comparisons
ADTs require:
10 10 4
100 100 7
1000 1000 10 Some references (variables) for holding the data
10000 10000 14 (usually hidden from the user)
100000 100000 17 Some operations that can be performed on the data
1000000 1000000 20 (available to the user)
c Tim French
! CITS2200 Introduction to Data Structures Slide 9 c Tim French
! CITS2200 Introduction to Data Structures Slide 10
class declaration
Variables can be made private
variable declarations // data held no access by users
. Methods can be made public
.
used to create and manipulate data structure
.
method declarations // operations on the data This encapsulation is good programming practice
.
. can change
.
the way the data is stored
the way the methods are implemented
c Tim French
! CITS2200 Introduction to Data Structures Slide 11 c Tim French
! CITS2200 Introduction to Data Structures Slide 12
Example: A Matrix Class public void set (int i, int j, int value) {
matrixArray[i][j]=value;
public class Matrix { }
private int[][] matrixArray; public int get (int i, int j) {return matrixArray[i][j];}
c Tim French
! CITS2200 Introduction to Data Structures Slide 13 c Tim French
! CITS2200 Introduction to Data Structures Slide 14
For a matrix with n rows and m columns, how many (array access) operations are private int[][] matrixArray;
needed? private boolean isTransposed;
Can you think of a more efficient implementation? One that doesnt move any public MatrixReloaded (int rows, int columns) {
data? matrixArray = new int[rows][columns];
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
matrixArray[i][j] = 0;
isTransposed = false;
}
c Tim French
! CITS2200 Introduction to Data Structures Slide 15 c Tim French
! CITS2200 Introduction to Data Structures Slide 16
public void set (int i, int j, int value) { What is the time performance of transpose()?
public int get (int i, int j) { How do the changes affect the users program?
}
}
c Tim French
! CITS2200 Introduction to Data Structures Slide 17 c Tim French
! CITS2200 Introduction to Data Structures Slide 18
c Tim French
! CITS2200 Introduction to Data Structures Slide 19 c Tim French
! CITS2200 Introduction to Data Structures Slide 20