Sie sind auf Seite 1von 6

DATA STRUCTURES

&
ALGORITHMS
CSL-221

LAB JOURNALS

Name: Obaid Awan

Enrollment No: 01-131182-043

Class: BSE-3A

Lab Instructor: Sir Aleem Ahmad

DEPARTMENT OF SOFTWARE ENGINEERING


BAHRIA UNIVERSITY
ISLAMABAD CAMPUS

1
DSA

LAB JOURNAL # 10

Title
TIME TAKEN TO SEARCH

Introduction:
In computer science, the time complexity is the computational complexity that describes the
amount of time it takes to run an algorithm.In this lab we found the total time taken to search
the number from a file.

Objectives:
 To make a program that performs the searching of number from a file.
 To write a program that show the time taken to search the given number .

Language Used: C++

Tools Used:
 Microsoft Visual Studio 2013

Submission Date: December 18th, 2019

Evaluation: Signature of Lab Engineer:

2
LAB TASKS

PROBLEM #1:
Write a program that search the number given by the user in the file having random 1000
functions and then it shows the time taken to search and also tells whether the number is found
or not.

PROGRAM:
//Implementation of Binary Search Trees
#include <fstream>
#include<iostream>
#include<ctime>
using namespace std;
template< class ItemType > struct TreeNode {
ItemType info; // Data member
TreeNode<ItemType>* left; // Pointer to left child
TreeNode<ItemType>* right; // Pointer to right child
};

template< class ItemType >

// BINARY SEARCH TREE SPECIFICATION


class TreeType {
public:
TreeType(); // constructor
~TreeType(); // destructor
bool IsEmpty() const; bool IsFull() const;
int NumberOfNodes() const; void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void RetrieveItem(ItemType& item, bool& found);
void PrintTree(ofstream& outFile);
void PrintHelper(TreeNode<ItemType>* ptr, ofstream& outFile);
void InsertHelper(TreeNode<ItemType>*& ptr, ItemType item);
void RetrieveHelper(TreeNode<ItemType>* ptr, ItemType& item, bool& found);
void DestroyHelper(TreeNode<ItemType>* ptr);

private:
TreeNode<ItemType>* root;
};

// BINARY SEARCH TREE IMPLEMENTATION


// OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
template< class ItemType >
TreeType<ItemType> ::TreeType() // constructor
{
root = NULL;
}

template< class ItemType >


bool TreeType<ItemType> ::IsEmpty() const
{
return (root == NULL);
}

3
template< class ItemType >
void TreeType<ItemType> ::RetrieveItem(ItemType& item, bool& found)
{
RetrieveHelper(root, item, found);
}

template< class ItemType >


void TreeType<ItemType> ::RetrieveHelper(TreeNode<ItemType>* ptr, ItemType& item,
bool& found)
{
if (ptr == NULL)
found = false;
else if (item < ptr->info) // GO LEFT
RetrieveHelper(ptr->left, item, found);
else if (item > ptr->info) // GO RIGHT
RetrieveHelper(ptr->right, item, found);
else
{
item = ptr->info; found = true;
}
}

template< class ItemType >


void TreeType<ItemType> ::InsertItem(ItemType item)
{
InsertHelper(root, item);
}

template< class ItemType >


void TreeType<ItemType> ::InsertHelper(TreeNode<ItemType>*& ptr, ItemType item)
{
if (ptr == NULL)
{ // INSERT item HERE AS LEAF
ptr = new TreeNode<ItemType>; ptr->right = NULL;
ptr->left = NULL; ptr->info = item;
}
else if (item < ptr->info) // GO LEFT
InsertHelper(ptr->left, item);
else if (item > ptr->info) // GO RIGHT
InsertHelper(ptr->right, item);
}

template< class ItemType >


void TreeType<ItemType> ::PrintTree(ofstream& outFile)
{
PrintHelper(root, outFile);
}

template< class ItemType >


void TreeType<ItemType> ::PrintHelper(TreeNode<ItemType>* ptr, ofstream& outFile)
{
if (ptr != NULL)
{
PrintHelper(ptr->left, outFile); // Print left subtree outFile << ptr-
>info ;
PrintHelper(ptr->right, outFile); // Print right subtree
}
}

template< class ItemType >


TreeType<ItemType> :: ~TreeType() // DESTRUCTOR
{

4
DestroyHelper(root);
}

template< class ItemType >


void TreeType<ItemType> ::DestroyHelper(TreeNode<ItemType>* ptr)
// Post: All nodes of the tree pointed to by ptr are deallocated.
{
if (ptr != NULL)
{
DestroyHelper(ptr->left); DestroyHelper(ptr->right); delete ptr;
}
}

// Driver Program

int main()
{
TreeType <int> ob;
int item = 1;
bool flag = false;
int input;
int items[10000];

for (int i = 0; i < 10000; i++)


{
items[i] = rand() % 50000;
}

ofstream myfile;
myfile.open("myfile.txt");
for (int j = 0; j < 10000; j++)
{
myfile << items[j];
myfile << endl;
}
myfile.close();

ifstream in("myfile.txt");

while (!in.eof())
{
in >> item;
ob.InsertItem(item);
}
cout<<"Obaid Awan"<<endl;
cout<<"01-131182-043"<<endl;
cout<<endl;
cout<<"|||||||||||| Binary Search
cout << "Enter item to search: ";
cin >> input;
const clock_t begin_time = clock();

ob.RetrieveItem(input, flag);

cout << "Time taken: " << float(clock() - begin_time/1000)<<" miliseconds" <<
endl;
if (flag == true)
{
cout << input << " Found!" << endl;
}
else

5
cout << "Not Found" << endl;

cout << "------------SIMPLE SEARCH----------" << endl;


cout << "Enter item to search: ";
cin >> input;
const clock_t begin_time1 = clock();
for (int i = 0; i < 10000; i++)
{
if (input == items[i])
{
cout << "Item found!" << endl;
break;
}

if (i == 9999)
{
cout << "Item Not Found!\n";
}
}
cout << "Time taken: " << float(clock() - begin_time1 / 1000)<<"miliseconds"
<< endl;

system("pause");
return 0;
}

Result
The output of the above program is shown below:

Conclusion
We are able to write the a program in which we search the number enter by the user from the
randomly filled file and then output the result and also the time taken to search the number in
the file.

Das könnte Ihnen auch gefallen