Vous êtes sur la page 1sur 14

Data Structures Review

Basic concept

➢What are key properties of a good


algorithm?
➢What are key properties of good data
structure?

2
Linked list
Given a singly linked list Head containing n integer
numbers, your task is to write function sum(head) to
calculate the sum of all odd numbers of the list Head.

3
Complexity 1
Create a matrix of size n.
(1) for (i = 0 ; i < n ; i++)
(2) for (j = 0 ; j < n ; j++)
(3) if (i == j)
(4) A[i][j] = 1;
(5) else
(6) A[i][j] = 0;

Complexity:
4
Complexity 2
1) sum = 0;
2) for ( i = 0; i < n; i + +)
3) for ( j = i + 1; j < = n; j + +)
4) for ( k = 1; k < 10; k + +)
5) sum = sum + i * j * k ;

Complexity:
5
Complexity 3
1) sum = 0;
2) for ( i = 0; i < n; i + +)
3) for ( j = i + 1; j < = n; j + +)
4) for ( k = 1; k < m; k + +) {
5) x = 2*y;
6) sum = sum + i * j * k
}

Complexity:

6
Complexity 4
1) for (i = 0; i < n; i ++)
2) for (j = 0; j < m; j ++) {
3) int x = 0;
4) for (k = 0; k < n; k ++)
5) x = x + k;
6) for (k = 0; k < m; k++)
7) x = x +k;
}

7
Hash table
Given a list of students (id, name): (7,An), (3,Be), (9,
Cu), (4, Da), (18, Gi), (14, En), (6, Ba), (25, Go).

Your task is to draw the hash table with the hash


function f(x) = x mod 11 using both collision handling
methods.

8
Heap tree
Do following tasks with a heap tree:

➢ Construct a max heap tree including: 3, 19, 38, 9, 12,


64, 22, 3, 16, 28, 73.

➢ Insert the following numbers into the above max


heap tree: 8, 13

9
Binary search tree
Do the following tasks with a binary search tree:

➢ Create a binary search tree from following numbers:


24, 15, 35, 22, 19, 42, 10, 80, 29

➢ Draw BSTs after deleting keys 15 and 29 from the


above tree.

10
Tree traversal

Show the order of nodes in the preorder traversal.

b c

f
d e

g j
h i

11
Tree traversal

Show the order of nodes in post-order traversal.

b c

f
d e

g j
h i

12
Balanced heap tree
Draw balanced heap trees for following lists of numbers

❖1 4 8 6 20 15 4 9 21 35 67 5 7

❖12 5 7 9 20 11 56 3 21 19

13
Balanced binary search tree
Draw balanced binary search trees for following lists of
numbers

❖1 4 8 6 20 15 4 9 21 35 67 5 7

❖12 5 7 9 20 11 56 3 21 19

14