Data Structures and Algorithms
Data Structures and Algorithms
system
An organised aggregate of data items
1 2 3 N-1 N
Column
0 1 2 3 4 5 6 7 8 9 10 j n
0
1
R
2
O
: : : : : : : : : : : : : : :
W
i x
object
Collection
Head
node node
Data Next Data Next
object2 object
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Linked Lists - Add implementation
Implementation
struct t_node {
void *item;
Recursive type definition -
struct t_node *next;
C allows it!
} node;
typedef struct t_node *Node;
struct collection {
Node head;
……
};
int AddToCollection( Collection c, void *item ) {
Node new = malloc( sizeof( struct t_node ) );
new->item = item;
new->next = c->head;
c->head = new;
Error checking, asserts
return TRUE;
omitted for clarity!
}
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Linked Lists - Find implementation
Implementation
void *FindinCollection( Collection c, void *key ) {
Node n = c->head;
while ( n != NULL ) {
if ( KeyCmp( ItemKey( n->item ), key ) == 0 )
{
return n->item;
n = n->next;
}
return NULL;
}
tail
struct t_node {
void *item;
struct t_node *left;
struct t_node *right;
};
struct t_collection {
Node root;
……
};
Height, h
Nodes traversed in a path from the root to a leaf
Number of nodes, h
n = 1 + 21 + 22 + … + 2h = 2h+1 - 1
h = floor( log2 n )
Since we need at most h+1 comparisons,
find in O(h+1) or O(log n)
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Binary Tree - Traversing
Traverse: Pass through the tree, enumerating each
node once
PreOrder (also known as depth-first order)
1. Visit the root
2. Traverse the left subtree in preorder
3.Traverse the right subtree in preorder
InOrder (also known as symmetric order)
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse te right subtree in inorder
PostOrder (also known as symmetric order)
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
Two binary trees are MIRROR SIMILAR if they are both empty
or if they are nonempty, the left subtree of each is mirror
similar to the right subtree
A Hierarchical Tree
Addition to a Heap
To add an item to a heap, we follow the reverse procedure.
Place it in the next leaf position and move it up.
Again, we require O(h) or O(logn) exchanges.
- remove most recently pushed item from the top of the stack
Like a plate stacker
Other methods
int IsEmpty( Stack s );
Determines whether the stack has anything in it
function g( int z ) {
int p, q;
p = …. ; q = …. ;
return f(p,q);
} Context
for execution of f
A K 10 5 J 4 2 Q 9
9
40
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Quick Sort 1/2
Quick sort, also known as partition sort, sorts by employing a
divide-and-conquer strategy.
Algorithm:
Pick an pivot element from the input;
Partition all other input elements such that elements less than the
pivot come before the pivot and those greater than the pivot come
after it (equal values can go either way);
Recursively sort the list of elements before the pivot and the list of
elements after the pivot.
The recursion terminates when a list contains zero or one element.
Time complexity: O(nlogn) or O(n2)
Demo: http://pages.stern.nyu.edu/~panos/java/Quicksort/
Example: Sort the list {25, 57, 48, 37, 12}
41
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Quick Sort 2/2
Example of Divide and Conquer algorithm
Two phases
Partition phase
Divides the work into half
Sort phase
Conquers the halves!
< pivot > pivot
Time complexity
Time is proportional to n
We call this time complexity O(n)
Worst case: N + 1 comparisons
Best case: 1 comparison
Average case (successfull): (1+2+...+N)/N = (N+1)/2
Static caching
Use the relative access frequency of elements
store the most often accessed elements at the first
places
Dynamic caching
For each access, move the element to the first
position
Needs a linked list data structure to be efficient
Very difficult to analyze the complexity in
theory: very efficient in practice
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Binary Search
Sorted array on a key
first compare the key with the item
in the middle position of the array
If there's a match, we can return
immediately.
If the key is less than the middle
key, then the item sought must lie
in the lower half of the array
if it's greater then the item sought
must lie in the upper half of the
array
Repeat the procedure on the lower
(or upper) half of the array -
RECURSIVE
50
Small
problems - 40
n
we’re not Large 4 log n
Time
30
interested! problemsn -
20 we’re
log2n interested
10
Binary search in this gap!
More complex 0
Higher constant factor 0 10 20 30 40 50 60
n