- •Data Structures and Algorithms Topics for the 1st part of the course Prepared by Dr. Alex Ossyka
- •- Representation of objects in computer memory :
- •Iterative implementation (by loop)
- •1St print: 5
- •2Nd print: 9 3d print: 7
- •2Nd print: 5
- •1St print: 9 3d print: 7
- •Balanced Trees
- •Height-Balanced Binary Search Trees
Data Structures and Algorithms Topics for the 1st part of the course Prepared by Dr. Alex Ossyka
TOPIC 1: Concept of an Abstract Data Type (ADT)
Selection of data structures and design of correct and effective algorithms are the most important stages in software design. Nicklaus Wirth:
“ALGORITHM + DATA STRUCTURES = PROGRAM”.
You cannot develop an algorithm before you choose data structures to keep its input, intermediate and resulting data.
Example. A problem: Given three int numbers a, b, and c. Find the minimum number.
Solution1.
int a = 5, b = 12, c = 3; // input data are stored in separate variables.
if(a < b)
if(a < c) System.out.println(“min = “ + a);
else System.out.println(“min = “ + c);
else
if(b < c) System.out.println(“min = “ + b);
else System.out.println(“min = “ + c);
Solution2.
int A[3] = {5, 12, 3}; //input data are stored in an array
int min = A[0];
for(int j = 1; j < 3; j++)
if(A[j] < min) min = A[j];
System.out.println(“min = “ + min);
On the other hand, if you want to use some ready function in your program you must declare the data types (or data structures) required by the function.
Built-in data types and data structures in Java
primitive composite
i
ntegral
floating point boolean unstructured structured
byte char short int long float double class interface array file
These built-in data types and data structures (= structured data types, where data objects consist of many data items) are sufficient for constructing any algorithm. But they may be not the suitable data tools for designing some specific algorithms.
For example: suppose, a problem consists in finding the shortest route from point A to point B on a map. A road map and points A and B are the input data for an algorithm that solves this problem. If we represent a map in computer memory by means of int, character, float variables and arrays then an algorithm (hence a computer program) becomes very large and difficult to understand. If a map is represented in memory by a new user-defined graph data structure then the algorithm becomes small in size and logically clear.
So in many cases we should introduce our own (user-defined) data structures like trees, stacks, queues, lists, graphs, trees, etc.
DEFINITION: a data type is a set of items (simple or structured) and a set of operations that act on these items.
In order to understand these definition let us consider the data type of integer numbers (a set of integer items + set of operations).
Items: 0,+1,-1,+2,-2,+3,-3,…
Operations: + , - , * , / , % , == , != , < , > , = , operations implemented by standard library functions [ pow(x,y) , sqrt(x) and others].
Typically a user does not need to know more about the integer data type. But a person who designs a computer system must take care of many other details related to integer data type (and other data types also). Such a person must make decisions about:
