Report Title: Algorithms and Complexity
Report Title: Algorithms and Complexity
Algorithms
Algorithmsand
andcomplexity
complexity
1
University of Duhok Subject: Algorithms & Complexity
College of Science Date: 16 / 06 / 2020
Dept.: Computer Science Deadline: 1 / 07 / 2020
Class: 2nd year Questions: 5
Final Assignment
Full Name: Hussein Shamdeen Younus Mark
1 1 2 3 1
9 6 5 4 2
3 5 0 7
9 6 1 13 5 25 4 30 17 2
9 6 1 13 5 25 4 30 17 2
9 6 1 13 5 25 4 30 17 2
4 25 17 2
6 9 13 5
5 13 2 17
1 5 13 2 17 30
1 5 6 9 13 2 4 17 25 30
2
1 2 4 5 6 9 13 17 25 30
#include<iostream>
2
using namespace std ;
system("pause");
}
4
(10 Marks)
Q2) Writer a full program for Shell sort algorithm.
#include <iostream>
using namespace std;
/* function to sort arr using shellSort */
int shellSort(int arr[], int n)
{ // Start with a big gap, then reduce the gap
for (int gap = n/2; gap > 0; gap /= 2)
{ // Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
// keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < n; i += 1) {
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
int temp = arr[i];
// shift earlier gap-sorted elements up until the correct
// location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
5
(10 Marks)
Q3) Consider the following binary tree then answer the following questions: -
1- What is the pre order traversal? ----------- 1->2->4->8->5->9->3->6->10->7->11
2- What is the degree of the tree? ------------ 2
3- What is the height of node 2? ------------ 2
4- What are the descendants of node 3? --- 6 – 7 – 10 - 11
5- What are the ancestors of node 5? ------ 2 , 1
2 3
4 5 6 7
1 1
8 9
0 1
6
(10 Marks)
Q4) Answer the following questions: -
1- What is a Full Binary Tree then give an example that contains the maximum number of nodes?
What is a Complete Binary Tree then give an example that contain the minimum number of nodes in a -2
?complete binary tree
?What is the Perfect Binary Tree then give an example -3
-:Solution
A full binary tree (sometimes proper binary tree) is a tree in which every node other than the leaves has
.two children
The maximum number of nodes in a binary tree of height h is 2h+1-1, k>=0. (Ex: 24 –1 = 15)
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled,
.and all nodes are as far left as possible
Perfect Binary Tree A Binary tree is Perfect Binary Tree in which all internal nodes have two children
.and all leaves are at the same level
7
(10 Marks)
Q5) What is binary search tree then give an example: - in a binary search tree we have operations that us
to locate or print content of the tree (all nodes)
1-per-order traversal.
2- in-order traversal.
3- post-order traversal.
#include<iostream>
using namespace std;
int binarySearch ( int arr [ ] , int l , int h , int element )
{
while ( l <= h ) {
int m = ( l + h ) / 2 ; //both => m (2 l + r - 1) /
if ( arr [ m ] == element )
return m ;
if ( arr [ m ] > element )
h=m-1;
else
l=m+1;
}
return -1 ;
}
int main ( )
{
int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ;
int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ;
int num ;
cout << " Enter an Integer : " ;
cin >> num ;
int result = binarySearch ( arr , 0 , n - 1 , num ) ;
if ( result == -1 )
cout << " The Number : ( " << num << " ) Was Not Found : " <<endl ;
else
cout << " The Number : ( " << arr [ result ] << " ) Was Found At Index : (" << result <<endl ;
8
REFERENCES
[1] Reference 1 https://www.geeksforgeeks.org/shellsort/
[2] Reference 2 https://www.geeksforgeeks.org/binary-search-tree-data-structure/