Вы находитесь на странице: 1из 23

ABSTRACT DATA TYPES

Data structures and algorithms in


Java

Boro Jakimovski
ADT DESIGN
Operations are sufficient if together they meet all the ADTs
requirements.
Can the application be written entirely in terms of calls to these operations?
An operation is necessary if it is not surplus to the ADTs requirements.
Could the operation be safely omitted?
A well-designed ADT provides operations that are necessary and
sufficient for its requirements.
A constructor is an operation that creates a value of the ADT.
An accessor is an operation that uses a value of the ADT to compute a
value of some other type.
A transformer is an operation that computes a new value of the same
ADT.
A well-designed ADT provides at least one constructor, at least one
accessor, and at least one transformer. The constructors and transformers
together can generate all values of the ADT.
A transformer is:
mutative if it overwrites the old value with the new value
applicative if it returns the new value, without overwriting the old value.
The values of an ADT are:
mutable if the ADT provides at least one mutative transformer
immutable if the ADT provides no mutative transformer.
STRING ADT
What is a String?
Define it with its properties not its implementation.

A String is a sequence of characters.


The characters of a string have consecutive
indices.
A substring is String a subsequence of
consecutive characters taken from a string.
The length of a string is the number of
characters in that string.
The empty string has length zero.
REQUREMENTS
Requirements for String ADT:
The values are to be character strings of any length.
It must be possible to determine the length of a
string.
length()
It must be possible to obtain an individual character
of a string, given the character's index.
charAt(index)
It must be possible to obtain a substring of a string,
given the indices of the characters forming the
substring.
substing(start, end)
It must be possible to compare strings
lexicographically.
compareTo( other )
It must be possible to concatenate strings.
concat( other )
IMMUTABLE STRINGS
public class String {
// Each String value is an immutable string of characters,
// of any length, with consecutive indices starting at 0.
private ...;
//////////// Constructor ////////////
public String (char[] cs) ;
// Construct a string consisting of all the characters in cs.

//////////// Accessors ////////////


public int length ( );
public char charAt (int i);
public bool equals (String other);
public int compareTo (String other);
//////////// Transformers ////////////
public String substring (int i, int j);
public String concat (String other);
}
POSSIBLE IMPLEMENTATION
Array of characters SLL of characters

The most obvious An alternative


representation representation
preferably with a length
This indeed permits a field in its header.
simple and efficient The SLL representation
representation. is clearly space-
consuming:
Performance analysis
a (Unicode) character
(next slide) occupies two bytes, and
a link typically occupies
four bytes
Performance analysis
(next slide)
IMPLEMENTATION COMPLEXITIES
Operation Time complexity Time complexity
(Array) (SLL)
length O(1) O(1)
charAt O(1) O(n)
equals O(min(n1, n2)) O(min(n1, n2))
compareTo O(min(n1, n2)) O(min(n1, n2))
substing O(n2) O(n1)
concat O(n1+n2) O(n1+n2)

There is no doubt that the Array representation is the better choice for
this String ADT.
MUTABLE STRING
Immutable strings have important advantages:
they are easy to understand
they can be simply and efficiently represented using
arrays.
They have an equally important disadvantage:
every call to a transformer creates a new String
object.
REQUIREMENTS
Requirements of the mutable
All String requirements
It must be possible to change a character at custom
position
setCharAt(i, ch)
It must be possible to insert substing at arbitrary
position
insert(i, other)
It must be possible to append the sting with another
sting
append(other)
It must be possible to remove parts of the string
delete(i, j)
IMPLEMENTATION
public class MutableString {

// Each MutableString value is a mutable string of


// characters, of any length, with consecutive indices
// starting at 0.
private ...; // ... implemetation variables, not shown here.

//////////// Constructor ////////////


public MutableString ();

//////////// Accessors ////////////


public int length ( );
public char charAt (int i);
public int compareTo (MutableString other);

//////////// Transformers ////////////


public String concat(String other);
public String substring (int i, int j);

public void setCharAt (int i, char c) ;


public void append (String s) ;
public void insert (int i, String s);
public void delete (int i, int j);
}
IMPLEMENTATION
Array Single Linked Lists

Arrays were superior SSL was shown to be


comparing to SLL inefficient for String
On the other hand representation
face resizing problems Better performance
insert and append (at first sight)
Solution is to use insert
resizable array for append
holding the string delete
bigger than initial
string size
IMPLEMENTATION
Using resizing arrays is a solution for the
overflow, but is very (time) expensive
The following alternative strategies are simple
and quite effective in practice:
Whenever an overflow occurs, create a new array
that is a fixed number of characters longer than the
new string.
Whenever an overflow occurs, create a new array
that is twice as long as the old string.
IMPLEMENTATION COMPLEXITIES
Operation Time complexity Time complexity
(resize array) (list)
length O(1) O(1)
charAt O(1) O(n)
equals O(min(n1, n2)) O(min(n1, n2))
compareTo O(min(n1, n2)) O(min(n1, n2))
substing O(n2) O(n1)
concat O(n1+n2) O(n1+n2)
setCharAt O(1) O(n)
insert O(n1+n2) O(n1)
delete O(n1) O(n1)
append O(n1+n2) O(n1)
Find the complexities of each operation for mutable string
TEXT ADT: DEFINITION
A text is a sequence of characters
subdivided into lines.
One application of a text is the internal
buffer of a text editor.
Design a text ADT suitable to support a
text editor. Assume the usual editing
facilities:
insertion and deletion of characters, cut, copy,
and paste.
choose a representation for texts, and
implement your ADT in Java.
TEXT ADT: STEP 1
First lets select the implementation interface
public class Text{
private ;
///////// Constructors /////////
public Text();
///////// Accessors /////////
public char charAt(int row, int column);
public String lineAt(int row);
public int numberOfRows();
public int lineLength(int row);
///////// Transformers /////////
public void insertChar(int row, int column, char c);
public void insertLine(int row);
public void delete(int row, int column);
public void deleteLine(int row);
public Text cut(int beggingRow, int beggingCol, int endRow, int
endCol);
public Text copy(int beggingRow, int beggingCol, int endRow,int
endCol);
public void paste(int beggingRow, int beggingCol, Text text);
}
TEXT ADT: IMPLEMENTATION
Choose the data structure for storing the text
Possibilities:
Array of Strings
Matrix (array of arrays)
Linked lists
Array of MuttableString
List of MuttableString
TEXT ADT: STEP 2
import java.util.*;
public class Text{
// We are goning to use ArrayList of MuttableString
private ArrayList lines;
private int noOfLines;

}
TEXT ADT: STEP 3
import java.util.*;
public class Text{

///////// Constructors /////////
public Text(){
lines = new ArrayList();
noOfLines = 0;
}
}
TEXT ADT: STEP 3
import java.util.*; public int numberOfRows(){
public class Text{ return noOfLines;
}
///////// Accessors /////////
public char charAt(int row, int column) public int lineLength(int row){
throws IndexOutOfBoundsException{
if (noOfLines > row && row >= 0){
if (noOfLines > row && row >= 0){
MuttableString tmp =
MuttableString tmp = (MuttableString)lines.get(row);
(MuttableString)lines.get(row);
return tmp.getLength();
return tmp.charAt(column);
} else
} else
throw new
throw new IndexOutOfBoundsException();
IndexOutOfBoundsException();
}
}

public MuttableString lineAt(int row)
throws IndexOutOfBoundsException{ }
if (noOfLines > row && row >= 0){
MuttableString tmp =
(MuttableString)lines.get(row);
return tmp;
} else
throw new
IndexOutOfBoundsException();
}
TEXT ADT: STEP 3
import java.util.*; public void delete(int row, int column)
throws IndexOutOfBoundsException{
public class Text{
if (noOfLines > row && row >= 0}{

MuttableString tmp =
///////// Transformers ///////// (MuttableString)lines.get(row);
public void insertChar(int row, int tmp.delete(column);
column, char c) throws
IndexOutOfBoundsException{ } else
if (noOfLines > row && row >= 0){ throw new
IndexOutOfBoundsException();
MuttableString tmp =
(MuttableString)lines.get(row); }
tmp.insert(column, c);
} else public void deleteLine(int row) throws
IndexOutOfBoundsException{
throw new
IndexOutOfBoundsException(); if (noOfLines > row && row >= 0}
} lines.remove(row);
noOfLines--;
public void insertLine(int row) throws } else
IndexOutOfBoundsException{
throw new
if (noOfLines >= row && row >= 0){ IndexOutOfBoundsException();
lines.add(row, new }
MuttableString());

noOfLines++;
}
} else
throw new
IndexOutOfBoundsException();
}
TEXT ADT: STEP 3
public Text cut(int beginningRow, int beginningCol, int endRow, int endCol){
if (noOfLines > beginningRow && beginningRow >= 0 &&
noOfLines > endRow && endRow >= 0 &&
beginningRow <= endRow){
Text tmp = new Text();
MuttableString tmp1 = (MuttableString)lines.get(beginningRow);
tmp1 = tmp1.substring(beginningCol, tmp1.getLength());
tmp.lines.add(tmp1);
for (int i = beginningRow + 1; i <= endRow - 1; i++){
tmp1 = (MuttableString)lines.get(i);
tmp.lines.add(tmp1);
}
tmp1 = (MuttableString)lines.get(endRow);
tmp1 = tmp1.substring(0, endCol);
tmp.lines.add(tmp1);
tmp.noOfLines = endRow beginningRow + 1;

MuttableString tmp1 = (MuttableString)lines.get(beginningRow);


tmp1 = tmp1.substring(0, beginningCol - 1);
lines.set(beginningRow, tmp1);
for (int i = beginningRow + 1; i <= endRow - 1; i++)
lines.remove(beginningRow + 1);
tmp1 = (MuttableString)lines.get(beginningRow + 1);
tmp1 = tmp1.substring(endCol, tmp1.getLength());
lists.set(beginningRow + 1, tmp1);
noOfLiens -= endRow beginningRow - 1;
return tmp;
} else
throw new IndexOutOfBoundsException();
}
TEXT ADT: STEP 3
TEXT ADT: STEP 3
public Text copy(int beggingRow, int beggingCol, int endRow,int
endCol){
}
public void paste(int beggingRow, int beggingCol, Text text){
}

Вам также может понравиться