0% found this document useful (0 votes)
65 views25 pages

Data Structures Lab Manual

Uploaded by

momi bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views25 pages

Data Structures Lab Manual

Uploaded by

momi bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

DEPARTMENT OF COMPUTER SCIENCE

GOVERNMENT ISLAMIA GRADUATE COLLEGE FOR WOMEN, EIDGAH ROAD FAISALABAD

LAB MANUAL

Degree Computer Science

Semester 4th, BS (CS)

Session 2022-2026

Course Title Data Structure and Algorithms

Course Code CSI-402

Credit Hours 4(3-1)

Sr. # Content
1. Objectives
2. Outcomes
3. Recommended System / Software
Requirements
4. Text Books
5. References
6. Weekly Plan
7. Lab Work details

Prepared by:

Faiza Ameer Lodhi


Objectives:
 To write and execute programs in C++ to solve problems using data structures such as arrays, linked lists,
stacks, queues, trees, graphs, hash tables and search trees.
 To write and execute write programs in C++ to implement various sorting and searching methods.
Outcomes:
 Ability to identify the appropriate data structure for given problem.
 Graduate able to design and analyze the time and space complexity of algorithm or program.
 Ability to effectively use compilers includes library functions, debuggers and trouble shooting.

Recommended System / Software Requirements:


1. Intel based desktop PC of 166MHz or faster processor with at least 64 MB RAM and 100 MB free disk
space.
2. Turbo C++ compiler or GCC compilers (with visual studio code optional)

Text Book:
Data Structures using C++ by C M Aslam, Saeed Ahmad, Aqsa Aslam and Atif Mansoor.

Refereces :
1. Data Abstraction and Problem Solving with C++, 2nd ed, Frank M. Carrano, Paul Helman, Robert
Veroff, Addison-Wesley, 1998.
2. Data Structures and Algorithms (SAMS teach yourself), Lafore, Sams Publishing, 1999.
3. Fundamentals of Data Structures in C++, Horowitz, Sahni, and Mehta, Computer Science Press, 1995.
4. Data Structures in JAVA, Standish, Addison Wesley, 2000.

Weekly Plan
Week Program
1 Write C++ programs to implement i) Linear search ii) Binary search.
2 Write a C++ program to implement Hashing.
3 Write C++ programs to implement i) Selection Sort ii) Bubble Sort
4 Write C++ a program to implement Insertion Sort
5 Write C++ programs to implement i) Shell Sort. ii) Radix Sort.
6 Write C++ program to convert Decimal Number to Binary using stack.
7 Write C++ programs to convert infix expression to postfix and also prefix notation.
8 Write C++ program for tower of Hanoi using recursion.
9 Write C++ program to implement Queue
10 Write C++ program to implement circular Queue
11 Write C++ program to implement deque (double ended queue).
12 Write C++ programs to implement i) Merge Sort ii) Quick Sort. iii) Bucket Sort.
13 Write C++ program to implement list ADT to perform following operations a) Insert an element
into a linked list. b) Delete an element from list c) Search for a key element in list d)count number
of nodes in list.
14 Write C++ programs to implement doubly & circular linked list.
15 Write a C++ program to perform the following operations: a) Insert an element into a binary
search tree. b) Delete an element from a binary search tree. c) Search for a key element in a binary
search tree
16 Project

Week 1
Write C++ programs to implement i) Linear search ii) Binary search.

i) Linear Search:
// C++ code to linearly search
// x in arr[]. If x is present
// then return its location,
// otherwise return -1
#include <iostream>
using namespace std;

int search(int arr[],


int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}

// Driver code
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);

// Function call
int result = search(arr, n, x);
(result == -1) ?
cout << "Element is not present in array" :
cout << "Element is present at index " <<
result;
return 0;
}
ii) Binary Search

// C++ program to implement iterative Binary Search


#include <bits/stdc++.h>
using namespace std;

// An iterative binary search function.


int binarySearch(int arr[], int low, int high, int x)
{
while (low <= high) {
int mid = low + (high - low) / 2;

// Check if x is present at mid


if (arr[mid] == x)
return mid;

// If x greater, ignore left half


if (arr[mid] < x)
low = mid + 1;

// If x is smaller, ignore right half


else
high = mid - 1;
}

// If we reach here, then element was not present


return -1;
}

// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
if(result == -1) cout << "Element is not present in array";
else cout << "Element is present at index " << result;
return 0;
}

Week 2:
Write a C++ program to implement Hashing.

int HashTable::hash (string word)


// POST: the index of entry is returned
{ int sum = 0;
for (int k = 0; k < word.length(); k++)
sum = sum + int(word[k]);
return sum % SIZE;
}
Note: See Text book ( Program 8.6 )

Week 3:
Write C++ programs to implement i) Selection Sort ii) Bubble Sort.
i) Selection Sort
// C++ program for implementation of
// selection sort
#include <bits/stdc++.h>
using namespace std;

// Function for Selection sort


void selectionSort(int arr[], int n)
{
// One by one move boundary of
// unsorted subarray
for (int i = 0; i < n - 1; i++)
{
// Find the minimum element in
// unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
min_idx = j;
}

// Swap the found minimum element


// with the first element
if (min_idx != i)
swap(arr[min_idx], arr[i]);
}
}

// Function to print an array


void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}

// Driver program
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

ii) Bubble Sort:


// Optimized implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;

// An optimized version of Bubble Sort


void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}

// If no two elements were swapped


// by inner loop, then break
if (swapped == false)
break;
}
}
// Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << " " << arr[i];
}

// Driver program to test above functions


int main()
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}

Week 4

Write C++ a program to implement Insertion Sort.


// C++ program for implementation of Insertion Sort
#include <iostream>
using namespace std;

/* Function to sort array using insertion sort */


void insertionSort(int arr[], int n)
{
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

/* A utility function to print array of size n */


void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}
// Driver method
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
}

Week 5
Write C++ programs to implement i) Shell Sort. ii) Radix Sort.
i) Shell Sort:
// C++ implementation of Shell Sort
#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];

// put temp (the original a[i]) in its correct location


arr[j] = temp;
}
}
return 0;
}

void printArray(int arr[], int n)


{
for (int i=0; i<n; i++)
cout << arr[i] << " ";
}

int main()
{
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);

cout << "Array before sorting: \n";


printArray(arr, n);

shellSort(arr, n);

cout << "\nArray after sorting: \n";


printArray(arr, n);

return 0;
}

Radix Sort:
// C++ implementation of Radix Sort

#include <iostream>
using namespace std;

// A utility function to get maximum


// value in arr[]
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}

// A function to do counting sort of arr[]


// according to the digit
// represented by exp.
void countSort(int arr[], int n, int exp)
{

// Output array
int output[n];
int i, count[10] = { 0 };

// Store count of occurrences


// in count[]
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;

// Change count[i] so that count[i]


// now contains actual position
// of this digit in output[]
for (i = 1; i < 10; i++)
count[i] += count[i - 1];

// Build the output array


for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

// Copy the output array to arr[],


// so that arr[] now contains sorted
// numbers according to current digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}

// The main function to that sorts arr[]


// of size n using Radix Sort
void radixsort(int arr[], int n)
{

// Find the maximum number to


// know number of digits
int m = getMax(arr, n);

// Do counting sort for every digit.


// Note that instead of passing digit
// number, exp is passed. exp is 10^i
// where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}

// A utility function to print an array


void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

// Driver Code
int main()
{
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = sizeof(arr) / sizeof(arr[0]);

// Function Call
radixsort(arr, n);
print(arr, n);
return 0;
}
Week 6
Write C++ program to convert Decimal Number to Binary using stack.

#include <siostream.h>
#include <conio.h>
class stack
{
private: int top;
int stk (15];

public:
stack(void) {top = -1;}
// Prototypes of the functions of the class
void bin (int x):
void display(void);
}
Note: See text book and Implement program 5.2.

Week 7
Write C++ programs to convert infix expression to postfix and also prefix notation.
infix expression to postfix
#include <jostream.h>
#include <conio.h>
#Include <ctype.h>
int priority(char); // prototype of function to check precedence of operator
class infix postfix
{
private:
char stack|20]:
int top;

public:
infix_ postfix() { top = -1; }
void scan(char [] );
void push(char);
char pop(void);

}
Note: See text book and Implement program 5.4.
infix expression to prefix

#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

char * reverse(char*); // function prototype for reversing the infix expression


int priority(char); //prototype of function to cheek precedence of operator

class infix_prefix
{
private:
char stack[20];
int top;
public:
infix_prefix() { top = - 1;}
void scan(char []):
void push(char);
char pop(void):
};
void main(void)
infix prefix obj;
clrscr();
char exp[100], rev_exp[100];
cout<<"Enter infix expression without spaces: *;
cin>>exp:
//convert infix expression in reverse order

Strpy(rev_exp, reverse(exp));
obj scan(rev_oxp):
getch():
} // end of main()
Note: See text book and Implement program 5.5.

Week 8
Write C++ program for tower of Hanoi using recursion.

#include<iostream.h>
#include<conio.h>
//Tower of Hanoi function implementation
void TOH(int n, char Sour, char Aux, char Des)
{
if(n==1)
{
cout<<"Move Disk "<<n<<" from "<<Sour<<" to *<<Des<<endl;
return;
}
TOH(n-1,Sour,Des,Aux):
cout<<Move Disk "«<n<< from "<<Sour<<* to *<<Des<<endi;
TOH(n-1,Aux,Sour, Des);
}
void main(void)
{
int n;
clrscr();
coul<<"Enter no. of disks:";
cin>>n;
//calling the TOH
TOH(n,’A’,’B’,’C’);
getch();
}

Week 9
Write C++ program to implement Queue

// CPP program for array


// implementation of queue
#include <bits/stdc++.h>
using namespace std;

// A structure to represent a queue


class Queue {
public:
int front, rear, size;
unsigned capacity;
int* array;
};

// function to create a queue


// of given capacity.
// It initializes size of queue as 0
Queue* createQueue(unsigned capacity)
{
Queue* queue = new Queue();
queue->capacity = capacity;
queue->front = queue->size = 0;

// This is important, see the enqueue


queue->rear = capacity - 1;
queue->array = new int[queue->capacity];
return queue;
}

// Queue is full when size


// becomes equal to the capacity
int isFull(Queue* queue)
{
return (queue->size == queue->capacity);
}

// Queue is empty when size is 0


int isEmpty(Queue* queue)
{
return (queue->size == 0);
}

// Function to add an item to the queue.


// It changes rear and size
void enqueue(Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)
% queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
cout << item << " enqueued to queue\n";
}

// Function to remove an item from queue.


// It changes front and size
int dequeue(Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)
% queue->capacity;
queue->size = queue->size - 1;
return item;
}

// Function to get front of queue


int front(Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}

// Function to get rear of queue


int rear(Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}

// Driver code
int main()
{
Queue* queue = createQueue(1000);

enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);

cout << dequeue(queue)


<< " dequeued from queue\n";

cout << "Front item is "


<< front(queue) << endl;
cout << "Rear item is "
<< rear(queue) << endl;

return 0;
}
Note: See text book and Implement program 7.1.

Week 10
Write C++ program to implement circular Queue

/ C or C++ program for insertion and


// deletion in Circular Queue
#include<bits/stdc++.h>
using namespace std;

class Queue
{
// Initialize front and rear
int rear, front;

// Circular Queue
int size;
int *arr;
public:
Queue(int s)
{
front = rear = -1;
size = s;
arr = new int[s];
}

void enQueue(int value);


int deQueue();
void displayQueue();
};

/* Function to create Circular queue */


void Queue::enQueue(int value)
{
if ((front == 0 && rear == size-1) ||
((rear+1) % size == front))
{
printf("\nQueue is Full");
return;
}

else if (front == -1) /* Insert First Element */


{
front = rear = 0;
arr[rear] = value;
}

else if (rear == size-1 && front != 0)


{
rear = 0;
arr[rear] = value;
}

else
{
rear++;
arr[rear] = value;
}
}

// Function to delete element from Circular Queue


int Queue::deQueue()
{
if (front == -1)
{
printf("\nQueue is Empty");
return INT_MIN;
}

int data = arr[front];


arr[front] = -1;
if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == size-1)
front = 0;
else
front++;

return data;
}

// Function displaying the elements


// of Circular Queue
void Queue::displayQueue()
{
if (front == -1)
{
printf("\nQueue is Empty");
return;
}
printf("\nElements in Circular Queue are: ");
if (rear >= front)
{
for (int i = front; i <= rear; i++)
printf("%d ",arr[i]);
}
else
{
for (int i = front; i < size; i++)
printf("%d ", arr[i]);

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


printf("%d ", arr[i]);
}
}

/* Driver of the program */


int main()
{
Queue q(5);

// Inserting elements in Circular Queue


q.enQueue(14);
q.enQueue(22);
q.enQueue(13);
q.enQueue(-6);

// Display elements present in Circular Queue


q.displayQueue();

// Deleting elements from Circular Queue


printf("\nDeleted value = %d", q.deQueue());
printf("\nDeleted value = %d", q.deQueue());

q.displayQueue();

q.enQueue(9);
q.enQueue(20);
q.enQueue(5);

q.displayQueue();

q.enQueue(20);
return 0;
}
Note: See text book and Implement program 7.2.

Week 11
Write C++ program to implement deque (double ended queue).

// C++ implementation of De-queue using circular


// array
#include <iostream>
using namespace std;

// Maximum size of array or Dequeue


#define MAX 100

// A structure to represent a Deque


class Deque {
int arr[MAX];
int front;
int rear;
int size;

public:
Deque(int size)
{
front = -1;
rear = 0;
this->size = size;
}

// Operations on Deque:
void insertfront(int key);
void insertrear(int key);
void deletefront();
void deleterear();
bool isFull();
bool isEmpty();
int getFront();
int getRear();
};

// Checks whether Deque is full or not.


bool Deque::isFull()
{
return ((front == 0 && rear == size - 1)
|| front == rear + 1);
}

// Checks whether Deque is empty or not.


bool Deque::isEmpty() { return (front == -1); }
// Inserts an element at front
void Deque::insertfront(int key)
{
// check whether Deque if full or not
if (isFull()) {
cout << "Overflow\n" << endl;
return;
}

// If queue is initially empty


if (front == -1) {
front = 0;
rear = 0;
}

// front is at first position of queue


else if (front == 0)
front = size - 1;

else // decrement front end by '1'


front = front - 1;

// insert current element into Deque


arr[front] = key;
}

// function to inset element at rear end


// of Deque.
void Deque ::insertrear(int key)
{
if (isFull()) {
cout << " Overflow\n " << endl;
return;
}

// If queue is initially empty


if (front == -1) {
front = 0;
rear = 0;
}

// rear is at last position of queue


else if (rear == size - 1)
rear = 0;

// increment rear end by '1'


else
rear = rear + 1;
// insert current element into Deque
arr[rear] = key;
}

// Deletes element at front end of Deque


void Deque ::deletefront()
{
// check whether Deque is empty or not
if (isEmpty()) {
cout << "Queue Underflow\n" << endl;
return;
}

// Deque has only one element


if (front == rear) {
front = -1;
rear = -1;
}
else
// back to initial position
if (front == size - 1)
front = 0;

else // increment front by '1' to remove current


// front value from Deque
front = front + 1;
}

// Delete element at rear end of Deque


void Deque::deleterear()
{
if (isEmpty()) {
cout << " Underflow\n" << endl;
return;
}

// Deque has only one element


if (front == rear) {
front = -1;
rear = -1;
}
else if (rear == 0)
rear = size - 1;
else
rear = rear - 1;
}
// Returns front element of Deque
int Deque::getFront()
{
// check whether Deque is empty or not
if (isEmpty()) {
cout << " Underflow\n" << endl;
return -1;
}
return arr[front];
}

// function return rear element of Deque


int Deque::getRear()
{
// check whether Deque is empty or not
if (isEmpty() || rear < 0) {
cout << " Underflow\n" << endl;
return -1;
}
return arr[rear];
}

// Driver code
int main()
{
Deque dq(5);

// Function calls
cout << "Insert element at rear end : 5 \n";
dq.insertrear(5);

cout << "insert element at rear end : 10 \n";


dq.insertrear(10);

cout << "get rear element "


<< " " << dq.getRear() << endl;

dq.deleterear();
cout << "After delete rear element new rear"
<< " become " << dq.getRear() << endl;

cout << "inserting element at front end \n";


dq.insertfront(15);

cout << "get front element "


<< " " << dq.getFront() << endl;

dq.deletefront();
cout << "After delete front element new "
<< "front become " << dq.getFront() << endl;
return 0;
}
Note: See text book and Implement program 7.3.

Week 12
Write C++ programs to implement i) Merge Sort ii) Quick Sort. iii) Bucket Sort.
i) Merge Sort
#include siostream.h>
#include < conlo.h>
void MergeSort(Int 0, int, int);
void Merge(int 0, Int, int, int);
void main(void )
{
int i, arr[] = {12, 11, 13, 5, 6, 7};
clrscr();
cout<<"The given array before sorting”<<end;
for(i = 0; 1 < 6; |++)
cout<<arrij[];
coul<<endl;
MorgeSort(arr, 0, 5);
coul<<"Array after sorting" <<endl;
for(i = 0; 1 < 6; 1++)
cout<<arr[l];
getch():
} end of main()
Note: See text book and Implement program 8.12.
ii) Quick Sort
include <conio.h>
include <iostream.h>
quicksort(int 0, Int, Int );
int partition(int 0, int, int);
void main (void)
{
int i, arr[] = {2, 15, 1, 61, 11, 47, 8};
cout<<"Array before sorting"<<endl;
for(i = 0; i<=6; i++)
cout<<arr[i]<< "\t";
cout<<endl;
quicksort(arr, 0, 6);
cout<<"Array after sorting"<<endl;
for(i = 0; i<=6; i++)
couts<ar[i]<<"\t";
cout<<end;
getch();
} //end of main()
Note: See text book and Implement program 8.13.
iii) Bucket Sort

#include <conio.h>
#nclude <jostream.h>
#include <math.h>
class bucket_sort
{
private:
int ar[10];
int BO[10], B1[10], B2[10], B3[10], B4[10];
int counter[5];
public:
void input(void);
int largest(void);
vold distribute_sort(int);
void sort(int [], int);
void concatenate(void);
void display(void);
}
void main(void)
{

bucket_sort obj;
int large;
clrscr();
obj.input();
large = obj.largest();
obj.distribute_sort(large);
obj. concatenate();
obj.display();
getch();
} //end of main()
Note: See text book and Implement program 8.15.

Week 13
Write C++ program to implement list ADT to perform following operations a) Insert an element into a
linked list. b) Delete an element from list c) Search for a key element in list d) count number of nodes
in list.
#include <iostream.h>
#include <string.h>
#include <conio.h>
{
struct node
float data;
node *link;
};
class list
{
private:
node *start;
public:
list(void)
{
start = NULL;
}
void insert_begin(void):
void insert_end(void);
vold insert_location(vold):,
void delete_begin(vold);
void delete_end(vold);.
vold delete_location(void);
void search(void);
void display(void);
Int count(vold);
void bubble_sort(void);
};
Note: See text book and Implement program 9.2.

Week 14
Write C++ programs to implement doubly & circular linked list.
#include <iostream.h>
#include <conio.h>
struct node
{
Node *previous;
int data;
node *next;
};
Class list
{
private:
node *start, *current, *temp;

public:
list()
{ start = NULL; }
void add_item(int);
void display(void);
};
void main (void)
{
list obj;
int val;
clrscr();
cout<-"Enter five values \n";
for(int i = 1; I<=5; i++)
{
Cin>val;
Obj.add_item(val);
}
Obj.display();
getch();
}
Note: See text book and Implement program 9.3.
Circular linked list:
#include <iostream.h>
#include <conio.h>
struct node
{
int n;
node *link;
};
Class list
{
private:
Node *S, *C, *P;
public:
List(void)
{
S=NULL;
}
void insert(int);
void display(void);
};

Note: See text book and Implement program 9.5.

Week 15
Write a C++ program to perform the following operations: a) Insert an element into a binary search
tree. b) Delete an element from a binary search tree. c) Search for a key element in a binary search tree

a) Insert an element into a binary search tree.


#include<iostream.h>
#include<conio.h>
Struct node
{
int data;
node *right;
node *left;
};
class bst
{
private:
node *start, *cur, *temp, *parent;
public:
bst(void)
{
start = NULL;
}
void create(int);
void insert(int);
void inorder(void);
void inorder(node*);
};
Note: See text book and Implement program 10.8.
b) Delete an element from a binary search tree

#include<iostream.h>
#include<conio.h>
Struct node
{
int data;
node *right;
node *left;
};
class bst
{
private:
node *start, *cur, *temp, *parent;
public:
bst(void)
{
start = NULL;
}
void create(int);
void delete_node(void);
void delete_leaf_node(node*, int);
void delete_onechild_node(node*, int);
void delete_twochild_node(node*, int);
void preorder(void);
void preorder(node*);

};
Note: See text book and Implement program 10.9.
c) Search for a key element in a binary search tree
#include<iostream.h>
#include<conio.h>
Struct node
{
int data;
node *right;
node *left;
};

class bst
{
private:
node *start, *cur, *temp, *parent;
public:
bst(void)
{
start = NULL;
}
void create(int);
node* search(node*, int);
};
Note: See text book and Implement program 10.2.

SEMESTER PROJECT

Submission: Last Week of Semester

Security Lock
In the bank, a security lock is used to access some rooms. This lock accepts two inputs: the employee
identification number (16 bits) and his/her password (4 bits). If the bank has 20 employees, construct their
database and store it in the memory. Then write a program to access these rooms.

The inputs of the program are the employee identification and the password.

The output is one bit (0/1) that means (denied/allowed).

You might also like