Data Structures Overview: Topics
Data Structures Overview: Topics
Hash Table • stores data in an array format where each data value has its
own unique index value (in an associative manner).
6
1
Memory Allocation
• Static memory allocation:
– Store variables in stack and it happens at compile time
Memory Allocation based on variable definitions.
– Access to those variables is very fast
Static Memory Allocation – The stack is reserved in a LIFO order (dependent with
each other), the most recently reserved block is always
Dynamic Memory Allocation the next block to be freed. That is make stack simple to
keep track of its storage
9 10
2
• Static memory allocation Memory Allocation & De-allocation
int arr[10];
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int main(int argc, char** argv) {
int main(int argc, char** argv) {
int *Iptr = new (nothrow) int [5000000000]; //18.6 GB
int *Iptr = new (nothrow) int [9000000000]; // 34.3 GB
if(Iptr == NULL)
if(Iptr == NULL)
{
{
cout << "Error: memory could not be allocated " <<endl;
cout << "Error: memory could not be allocated " <<endl;
return 1;
return 1;
}
}
cout << "allocating memory successfully" <<endl;
cout << "allocating memory successfully" <<endl;
return 0;
return 0;
}
}
17 18
3
Question Question
int *P1; int *P1;
P1 = new int[5]; P1 = new int[5];
delete P1; delete P1;
if (P1 != NULL) // Does it works ? P1 = NULL;
cout << "P1 = " <<P1 <<" while *P1 = "<<*P1 <<endl; if (P1 != NULL) // Does it works ?
cout << "P1 = " <<P1 <<" while *P1 = "<<*P1 <<endl;
NO if condition is true & it will print, e.g.:
P1 = 0xc519f0 while *P1 = 12915952
YES if condition is false
19 20
Question
Question #include<stdio.h>
int main()
{ int x = 20, y = 10;
• What happens in this case? int *Ptr = &y;
*Ptr = x; // Are x and *Ptr are equal or not ?
int *Ptr_A; if (x == *Ptr)
Compilation error: invalid conversion from int to int * printf("first if: x and *Ptr are equal, they equal %d\n",*Ptr);
Ptr_A = 20; else
printf("first if: x and *Ptr are not equal, x = %d , *Ptr = %d\n“, x,*Ptr);
• What happens in this case? x = 1000;
Runtime error: memory not allocated if (x == *Ptr)
int *Ptr_A; printf("second if: x and *Ptr are equal, they equal %d",*Ptr);
*Ptr_A = 20; else
printf("second if: x and *Ptr are not equal, x = %d , *Ptr = %d“, x,*Ptr);
}
23 24
4
Pointers Static & Dynamic memory allocation
27 28
Reference variables
char c = 'z';
char& r = c;
char c2 = 'w';
&r = c2; // Does it compile ?
NO, reference variable cannot be re-initialized.
void add(int& x)
{
int a = 7;
x = &a; // Does it compile ?
} NO, reference variable cannot be re-initialized.
29 Dr Hany Hanafy Mahmoud Said 30
5
Using references & pointers Using references & pointers
• References are more safer (they must be initialized) • Do not initialize reference to constant variable (&var = 10).
• References are more simpler for syntax than pointers (do • Do not return reference from functions (out of scope when
not require dereferencing) function execution is finished).
• References is often used when we want to change variables • Avoid using references for variables with dynamic memory
dynamically through function as pointers, however allocations.
references does not use any extra memory. • Use pointers when NULL could be acceptable value
• Use references for input function parameters (that cannot • Use pointers for dynamically allocated variables
be NULL). • Use pointers for struct & class members
• Use reference for aliasing (int &s = Arr[i])
31 32
6
How to calculate Big O notation Example
1. Different steps get added T = c … (constant time)
Def func1(given_array):
2. Drop constants c is different from one
total = 0
computer to anther
3. Different inputs implies different variables Return total
T = O(1)+O(1) = c1 + c2 = c3
4. Find the fastest growing term = O(1)
5. Take out the coefficients
• if T = 5000 = O(1) Def find_sum(given_array): T= O(1) + N x O(1) + O(1)
total = 0 = c1 + N x c2
• if T = aN + b = O(N)
for each i in given_array: =O(N)
• if T = cN2 + dN + e = O(N2)
total = total + i
• if T = xN3 + yN2 + zN + w = O(N3) Return total
Example Example
T= O(1) + N2 x O(1) + O(1) Def sum_product(given_arr): T= O(1) + O(1) + N x O(1) +
Def find_sum(given_arr2d):
= c 1 + N2 x c 2 N x O(1) + O(1)
total = 0 total = 0
= c1 + 2 x N
for each row in given_arr2d: =O(N2) product = 1
= O(N)
for each i in row: for each i in given_arr:
total = total + I
total = total + i
for each i in given_arr:
Return total
product = product x I
print sum, product
Return
7
Constant time O(1) Linear time O(n)
• If running time does not depend on the size
of the input (n) for (int i = 0; i < n; i = i + 1) // i++
• O(any constant value), this is equivalent to Example: Linear search
being O(1). Even if there are loops, it doesn’t
depend on the size of n
8
Exponential time O(2n) Factorial time O(n!)
• function fib(n) • function fact(n) {
{ for(i=0; i<n; i++)
if(n <= 1) {
{ return n } fact(n-1);
else
}
return fib(n-2) + fib(n – 1)
}
}
Example: traversing all nodes in binary tree Example: generating permutations
9
Source: https://www.slideshare.net/introprogramming/19-algorithms-andcomplexity
56
10