vulms.vu.edu.pk€¦ · web viewwrite c++ program that will demonstrate how the value in a caller...
TRANSCRIPT
Week No. Lab Topic Page No.
1 Lab 1: Learn to implement linked list data structure 22 Lab 2: Learn to implement stack data structure using array 33 Lab 3: Learn to implement queue data structure using link list 44 Lab 4: Learn to draw binary search tree and implement in-order traversal 55 Lab 5: Learn to implement binary search tree for string type data 76 Lab 6: Learn to understand and implement function call by value, reference
and pointer14
7 Lab 7: Learn to understand and implement function call by value, reference and pointer
16
8 Lab 8: Learn to delete nodes from BST 19Midterm Exams
9 Learn to delete nodes from AVL tree 2010 Learn to build frequency table and Huffman encoding tree 2211 Lab 11: Learn to implement min heap using insert( ) method 2412 Lab 12: Learn to implement min heap using buildHeap( ) method 2913 Lab 13: Learn to build union tree 3014 Lab 14: Learn to implement binary search algorithm 3115 Lab 15: Build Hash table using linear probing collision resolution
technique33
16 Lab 16: Learn to sort array using bubble sort algorithm 34
1 | P a g e
Lab 1
Lab Title: Learn to implement linked list data structure
Objectives: Get the knowledge of implementing linked list data structure using C++ programming language.
Tool: Dev C++
Description:
Write the C++ code to implement the List class. You need to write only add( ). remove( ), and find( ) functions of the list class.
2 | P a g e
Lab 2
Lab Title: Learn to implement stack data structure using array
Objectives: Get the knowledge of implementing stack list data structure using array in C++ programming language
Tool: Dev C++
Description:
Write a C++ program to implement data structure Stack. You have to write all the stack functions and using those functions insert the numbers 1 to 10 into the stack. The stack should be able to show the error messages if the push( ) operation is initiated but the stack is full or the pop( ) operation is initiated but the stack is empty.
3 | P a g e
Lab 3
Lab Title: Learn to implement queue data structure using link list
Objectives: Get the knowledge of implementing queue data structure using link list in C++ programming language.
Tool: Dev C++
Description:
Write the C++ Code for the enqueue( ), dequeue( ), front( ) and the rear( ) functions of the class queue.
4 | P a g e
Lab 4
Lab Title: Learn to draw binary search tree and implement in-order traversal
Objectives: Learn to draw binary search tree and perform in-order traversal using C++ programming language.
Tool: Dev C++, MS Word, MS Visio
Description:
Task 1:
Draw BST from the given Data:
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Task 2:
Write C++ code that will perform inOrder traversal on the above given BST.
#include <iostream>
#include "TreeNode.cpp"
#include <queue>
using namespace std;
void inOrder(TreeNode<int>* );5 | P a g e
int main()
{
int Array[]={14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5,-1};
TreeNode <int> * root = new TreeNode<int>();
root->set(&Array[0]);
for (int i = 1; Array[i] > 0; i++)
{
insert(root, &Array[i]);
}
cout << "\ninorder: ";
inOrder( root );
} // end of main
void inOrder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
inOrder(treeNode->getLeft());
cout << *(treeNode->get())<<" ";
inOrder(treeNode->getRight());
}
} //End of inOrder.
6 | P a g e
Lab 5
Lab Title: Learn to implement binary search tree for string type data
Objectives: Get the knowledge of implementing binary search tree for string data type using C++ programming language.
Tool: Dev C++
Description:
Write C++ code that will draw BST using string type of data:
"babble", "fable", "jacket","backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease","debunk","feeder","genius","fetch","chain", NULL
Note: Template_BSTNode.cpp Template_Node_SLL.cpp and Template_Queue_LinkedList.cpp files will students search from CS301 handouts or may be provided by the tutor/Instructor.
#include <iostream>
#include <cstring>
#include "Template_BSTNode.cpp"
#include "Template_Queue_LinkedList.cpp"
using namespace std;
template <typename T>
void insert(BSTNode<T> * , T);
template <typename T>
void insertChar(BSTNode<T> * , T);
template <typename T>
void inOrderTraverse(BSTNode<T> * );
template <typename T>
void preOrderTraverse(BSTNode<T> * );
7 | P a g e
template <typename T>
void postOrderTraverse(BSTNode<T> * );
template <typename T>
void levelOrderTraverse(BSTNode<T> * );
int main()
{
static char * x[] = {"babble", "fable", "jacket","backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease","debunk","feeder","genius","fetch","chain", NULL};
BSTNode<char *> * Root = new BSTNode<char *>(x[0]);
for(int i = 0; x[i]; i++)
insertChar(Root, x[i]);
cout<< endl << endl<< "the inOrder BST is ... " << endl;
inOrderTraverse(Root);
cout<< endl << endl<< "the preOrder BST is ... " << endl;
preOrderTraverse(Root);
cout<< endl<< endl << "the postOrder BST is ... " << endl;
postOrderTraverse(Root);
cout<< endl << endl<< "the levelOrder BST is ... " << endl;
levelOrderTraverse(Root);
} // end of main.8 | P a g e
template <typename T>
void insert(BSTNode<T> * node, T x)
{
BSTNode<T> * newNode = new BSTNode<T>(x);
BSTNode<T> * tempRoot, * tempLeaf;
tempRoot = tempLeaf = node;
while(x !=tempRoot->getData() && tempLeaf != NULL)
{
tempRoot = tempLeaf;
if(x< tempRoot->getData())
tempLeaf = tempRoot->getLeft();
else
tempLeaf = tempRoot->getRight();
} // end of while , this loops only break when x = data hold by current node or it is leaf;
if(x == tempRoot->getData())
{
cout << "\nDuplicate element entry !!! "<< x << endl;
delete newNode;
}
else if(x< tempRoot->getData())
tempRoot->setLeft(newNode);
else
9 | P a g e
tempRoot->setRight(newNode);
}
template <typename T>
void insertChar(BSTNode<T> * node, T x) // OverLoaded Insert Funciton
{
BSTNode<T> * newNode = new BSTNode<T>(x);
BSTNode<T> * tempRoot, * tempLeaf;
tempRoot = tempLeaf = node;
while(strcmp(x, tempRoot->getData())!=0 && tempLeaf != NULL)
{
tempRoot = tempLeaf;
if(strcmp(x, tempRoot->getData())<0 )
tempLeaf = tempRoot->getLeft();
else
tempLeaf = tempRoot->getRight();
} // end of while , this loops only break when x = data hold by current node or it is leaf;
if(strcmp(x, tempRoot->getData())==0 )
{
cout << "\nDuplicate element entry !!! "<< x << endl;
delete newNode;
}
10 | P a g e
else if(strcmp(x, tempRoot->getData())< 0 )
tempRoot->setLeft(newNode);
else
tempRoot->setRight(newNode);
}
template <typename T>
void inOrderTraverse(BSTNode<T> * node )
{
if(node!=NULL)
{
inOrderTraverse(node->getLeft() ); // it will keep caling function till reaches node reach end
cout << endl<< node->getData();
inOrderTraverse(node->getRight() );
}
}
template <typename T>
void preOrderTraverse(BSTNode<T> * node )
{
if(node!=NULL)
{
cout << endl<< node->getData();
11 | P a g e
preOrderTraverse(node->getLeft() ); // it will keep caling function till reaches node reach end
preOrderTraverse(node->getRight() );
}
}
template <typename T>
void postOrderTraverse(BSTNode<T> * node )
{
if(node!=NULL)
{
postOrderTraverse(node->getLeft() ); // it will keep caling function till reaches node reach end
postOrderTraverse(node->getRight() );
cout << endl<<node->getData();
}
}
template <typename T>
void levelOrderTraverse(BSTNode<T> * node )
{ queue <BSTNode<T> *> myQueue;
if (node == NULL) return;
myQueue.push(node);
while (!myQueue.isEmpty())
{
node = myQueue.peak();
12 | P a g e
myQueue.pop();
cout << endl<< node->getData();
if(node->getLeft() != NULL)
myQueue.push(node->getLeft());
if(node->getRight() != NULL)
myQueue.push(node->getRight());
}
system("pause");
}
13 | P a g e
Lab 6
Lab Title: Learn to implement function call by value, reference and pointer
Objectives: Get the knowledge of implementing function calls for call by value, call by reference and call by pointer.
Tool: Dev C++
Description:
Write C++ program that will demonstrate how the value in a caller function is affected when it is passed to a function by using call by value, by using pointers and by using call by reference methods.
#include <iostream.h>//Function 1, call by valueint intMinus1( int oldVal){oldVal = oldVal – 1;return oldVal;}// Function 2, call by using pointersint intMinus2( int* oldVal){*oldVal = *oldVal – 2;return *oldVal;}// Function 3, call by referenceint intMinus3( int& oldVal){oldVal = oldVal – 3;return oldVal;}void main (){int myInt = 31;int retVal;retVal = intMinus1( myInt ); //call by valuecout << “After returning from the called function intMinus1” << endl ;cout << ”The value returned by the called function (retVal) is : ” << retVal ;cout << endl ;cout << ”The value of the calling function’s variable (myInt) is : ” << myInt ;cout << endl << endl;
14 | P a g e
// now pass the argument by using pointer, also initialize the value of myIntmyInt = 31 ;retVal = intMinus2( &myInt ); //call by passing a pointercout << “After returning from the called function intMinus2” << endl;cout << ”The value returned by the called function (retVal) is : ” << retVal ;cout << endl;cout << ”The value of the calling function’s variable (myInt) is : ” << myInt ;cout << endl << endl;// now pass the argument by as reference, also initialize the value of myIntmyInt = 31 ;retVal = intMinus3( myInt ); //call by passing a referencecout << “After returning from the called function intMinus3” << endl;cout << ”The value returned by the called function (retVal) is : ” << retVal ;cout << endl;cout << ”The value of the calling function’s variable (myInt) is : ” << myInt ;}
15 | P a g e
Lab 7
Lab Title: Learn to delete nodes from BST
Objectives: Understand to deletion process of nodes in BST
Tool: MS Word, MS Visio
Description:
Draw Binary Search Tree from the given data. Also draw Binary Search Tree after removal of value ‘9’.
9 4 6 17 2 8 4 15 41 47 29
16 | P a g e
Lab 8
Lab Title: Learn to draw AVL
Objectives: Learn to build/draw AVL tree and understand different types of rotations
Tool: MS Word, MS Visio
Description:
Build AVL tree from the given Data: 3 5 6 7 9 10 11 21 20 18 19
Note: You have to show only final AVL tree after insertion of each node value.
19 | P a g e
8
3 10
1 6 14
4 7
8
3 10
1 6
4 7
Lab 9
Lab Title: Learn to delete nodes from AVL tree
Objectives: Learn to delete nodes from AVL with the help of rotations
Tool: MS Word, MS Visio
Description:
Delete the node 14, 4 and 10 from given AVL three and perform necessary rotation to balance the tree after deletion of each node.
20 | P a g e
Lab 10
Lab Title: Learn to build frequency table and Huffman encoding tree
Objectives: Get the knowledge of building frequency table and Huffman encoding tree.
Tool: MS Word, MS Visio
Description:
Consider the message “the clouds are dark and its about to rain” and construct frequency table and Huffman encoding tree.
Frequency Table:
Character Frequency Character Frequency
a 5 n 2b 1 o 3c 1 r 3d 3 s 2e 2 t 4h 1 u 2i 2 SP 8k 1 NL 1l 1
22 | P a g e
s 2 n 2
b khc
14
7
4 o 4
7
42
u
28
13
a 8
t 4
15
SP 7
d 4
NLl
2i
r
e
0
1
0
1
0
0
0
0
00
0
0
0
0
0
0
0
0 1
1
1
1
1
1
1
1
1
1
1 1
1
1
23 | P a g e
Lab 11
Lab Title: Learn to implement min heap using insert( ) method
Objectives: Get the knowledge of implementing min heap using insert( ) method with the help of C++ programming language.
Tool: Dev C++
Description:
Consider the Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29 and write the C++ code to construct min heap using insert method.
#include <iostream>
using namespace std;
class Heap {
public:
Heap (int capacity);
bool insert ( const int & x);
bool isEmpty ();
bool isFull();
void traverse();
public:
int currentSize; // number of elements in heap
int * array; // the heap array
int capacity;
};
Heap::Heap (int capacity) {24 | P a g e
array = new int[capacity+1];
currentSize = 0;
}
bool Heap::insert ( const int & x) {
if(isFull()) {
cout <<endl<<"cannot insert Heap is full!!" << endl;
return 0;
}
int hole = ++currentSize;
for(/* declaration is above*/ ; hole>1 && x< array[hole/2]; hole /= 2) {
array[hole]= array[hole/2];
}
array[hole] = x;
}
void Heap::traverse() {
for (int i = 1; i<=currentSize; i++)
cout <<" "<< array[i] << " ";
}
bool Heap::isEmpty() {
return currentSize == 0;
}
bool Heap::isFull() {
return currentSize==capacity;
25 | P a g e
}
main() {
int size = 16, arr[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
Heap heap(size);
for (int i = 0; i< size; i++)
heap.insert(arr[i]); // insert number into heap one by one
cout << " Min Heap using insert method : "<<endl;
heap.traverse();
}
26 | P a g e
Lab 12
Lab Title: Learn to implement min heap using buildHeap( ) method
Objectives: Get the knowledge of implementing min heap using buildHeap( ) and perculateDown( ) methods with the help of C++ programming language.
Tool: Dev C++
Description:
Consider the Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29 and write the C++ code to construct min heap using buildHeap( ) method.
#include <iostream>
using namespace std;
class Heap {
public:
Heap (int capacity);
bool isEmpty ();
bool isFull();
void buildHeap(int * anArray, int n);
void traverse();
public:
int currentSize; // number of elements in heap
int * array; // the heap array
int capacity;
void percolateDown( int hole );
};27 | P a g e
Heap::Heap (int capacity) {
array = new int[capacity+1];
currentSize = 0;
}
void Heap::percolateDown(int hole) {
int child;
int temp = array[hole];
for ( /*nothing*/ ; hole * 2 <=currentSize; hole = child) {
child = hole*2;
if (child !=currentSize && array[child+1] < array[child] )
child++;
if (array[child]< temp)
array[hole] = array[child];
else break;
}
array[hole] = temp;
}
void Heap::buildHeap(int * anArray, int n) {
for (int i = 1; i <= n ; i++ )
array[i] = anArray[i-1];
currentSize = n;
28 | P a g e
for (int i =currentSize/2; i>0; i--)
percolateDown(i);
}
void Heap::traverse() {
for (int i = 1; i<=currentSize; i++)
cout <<" "<< array[i] << " ";
}
bool Heap::isEmpty() {
return currentSize == 0;
}
bool Heap::isFull() {
return currentSize==capacity;
}
main() {
int size = 16, arr[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
Heap heap(size);
heap.buildHeap(&arr[0],size);
cout << "\n Min Heap using build method: "<<endl;
heap.traverse();
}
29 | P a g e
4
2
3
1 5
67
83
Lab 13
Lab Title: Learn to build union tree
Objectives: Get the knowledge of building union tree of any given data.
Tool: MS Word, MS Visio
Description:
Consider the following sequence of union commands on the set of elements {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
union(3,1) union(5,6) union(3,5) union(4,2) union(3,4) union(1,7) union(7,8) union(2,3)
Show the resultant tree after performing union operations.
30 | P a g e
Lab 14
Lab Title: Learn to implement binary search algorithm
Objectives: Get the knowledge of implementing binary search algorithm using C++ programming language.
Tool: Dev C++
Description:
Write a program in C++ language to find a number (element) from an array using binary search algorithm.
You can use 18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97 as data of array.
#include <iostream>
using namespace std;
int binarySearch(int arr[], int l, int r, int num) {
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == num)
return mid;
if (arr[mid] > num)
return binarySearch(arr, l, mid-1, num);
return binarySearch(arr, mid+1, r, num); 31 | P a g e
}
return -1;
}
main() {
int size = 14, arr[size] = {18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97};
int number = 15;
int result = binarySearch(arr, 0, size-1, number);
if(result == -1){
cout<<"Element "<<number<<" is not present in array";
}
else{
cout<<"Element "<<number<<" is present at index %d"<<result;
}
}
32 | P a g e
Lab 15
Lab Title: Build hash table using linear probing collision resolution technique
Objectives: Learn to build Hash table using linear probing technique to resolve collision
Tool: MS Word
Description:
Consider the data given below and build a Hash table using linear probing technique to resolve collision.
Data 18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97
Use Hash function key % tablesize where tablesize is 20.
Index 0 1 2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
F(x) 20
97
42
3 82
85
47
11
51
94
96
37
18
79
33 | P a g e
Lab 16
Lab Title: Learn to sort array using bubble sort algorithm
Objectives: Get the knowledge of sorting array using bubble sort algorithm.
Tool: Dev C++
Description:
Consider the data given below as an array and sort by implementing bubble sort algorithm using C++ language.
Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29
#include <iostream>
using namespace std;
void bubbleSort(int *arr, int N) {
int i, temp, bound = N-1;
int swapped = 1;
while (swapped > 0 ) {
swapped = 0;
for(i=0; i < bound; i++)
if ( arr[i] > arr[i+1] ) {
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
swapped = i;
34 | P a g e
}
bound = swapped;
}
}
main() {
int size = 16, arr[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
bubbleSort(arr, size);
for(int i = 0; i < size; i++)
cout<<arr[i]<<"\t";
}
Mechanism to Conduct Lab:
Students and teacher communicate through Skype/Adobe Connect. Students perform the task using the recommended tool given in each lab.
35 | P a g e