labc==
labc==
Course Objectives:
● To understand the basic concepts of linear data structures like arrays and linked
lists.
● To grasp the concept of stacks and queues as a linear data structure and the
operations upon them.
● To understand the fundamental concepts of hierarchical Tree data structures.
● To explore optimization strategies for indexing structures and graph algorithms.
● To understand the concept of searching for quick data retrieval, sorting for
arranging data, hash functions strategies for optimized data storage.
Course Outcomes:
Co No.
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
CO1 3 2 1 - - - - - - - - - 2 1
CO2 3 2 1 - - - - - - - - - 2 1
CO3 3 2 1 - - - - - - - - - 2 1
CO4 3 2 1 - - - - - - - - - 2 1
C05 3 2 1 - - - - - - - - - 2 1
CONTENT
EXP. DATE
SIGN OF
NO OF TITLE CO PO MARKS
THE
THE Mapped Mapped
FACULTY
EXP
1. PO1,PO2,PO3
C++ program to implement CO1
singly linked list
2. PO1,PO2,PO3
C++ program to implement CO1
doubly linked list
3. PO1,PO2,PO3
C++ program to implement
polynomial addition using CO1
singly linked list
4. PO1,PO2,PO3
C++ program to implement
CO2
stack operations
5. PO1,PO2,PO3
C++ programs to
implement a queue
CO2
operations using a linked
list
6. PO1,PO2,PO3
Convert infix to postfix
CO2
expression using stack
7. C++ programs to PO1,PO2,PO3
implement a binary search
CO3
tree
Operations
8. PO1,PO2,PO3
Binary tree traversal CO3
9. PO1,PO2,PO3
Implement an AVL tree CO3
2
13. Binary search using a PO1,PO2,PO3
CO5
recursive function
14. C++ program to implement PO1,PO2,PO3
CO5
the Insertion Sort algorithm
15. C++ program to implement PO1,PO2,PO3
separate chaining technique CO5
in hashing
3
EX.NO: 1
C++ PROGRAM TO IMPLEMENT SINGLY LINKED LIST
DATE:
AIM:
To write a C++ program for implementing singly linked list.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class for the node.
Step 3: Create a node.
Step 4: Create a singly linked list
Step 5: Insert into the list
Step 6: Delete from the list.
Step 7: Search the list.
Step 8: Count the no of nodes in the list.
Step 9: Stop the program.
PROGRAM:
#include <iostream>
Node(int val) {
data = val;
next = NULL;
}
};
public:
LinkedList() {
head = NULL;
}
4
// Function to insert an element at the beginning of the list
void insertElement(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
} else {
newNode->next = head;
head = newNode;
}
cout<<val<< " inserted into the list." <<endl;
}
if (position == 1) {
newNode->next = head;
head = newNode;
cout<<val<< " inserted at position " << position << "." <<endl;
return;
}
if (!current) {
5
cout<< "Invalid position. Insertion failed." <<endl;
return;
}
newNode->next = current->next;
current->next = newNode;
cout<<val<< " inserted at position " << position << "." <<endl;
}
if (!current) {
cout<< key << " not found in the list. Insertion failed." <<endl;
return;
}
newNode->next = current->next;
current->next = newNode;
cout<<val<< " inserted after " << key << "." <<endl;
}
if (!current) {
cout<< key << " not found in the list. Insertion failed." <<endl;
return;
}
if (!prev) {
newNode->next = head;
head = newNode;
} else {
newNode->next = current;
6
prev->next = newNode;
}
cout<<val<< " inserted before " << key << "." <<endl;
}
delete temp;
cout<< "First element deleted from the list." <<endl;
}
while (current->next) {
prev = current;
current = current->next;
}
if (!prev) {
// If there is only one element in the list
head = nullptr;
} else {
prev->next = nullptr;
}
delete current;
cout<< "Last element deleted from the list." <<endl;
}
7
}
if (!current) {
cout<< "Invalid position. Deletion failed." <<endl;
return;
}
if (!prev) {
head = current->next;
} else {
prev->next = current->next;
}
delete current;
cout<< "Element at position " << position << " deleted from the list." <<endl;
}
if (!current) {
cout<<val<< " not found in the list. Deletion failed." <<endl;
return;
}
if (!prev) {
head = current->next;
} else {
prev->next = current->next;
8
}
delete current;
cout<<val<< " deleted from the list." <<endl;
}
while (current) {
if (current->data == val) {
cout<<val<< " found in the list." <<endl;
return true;
}
current = current->next;
}
while (current) {
count++;
current = current->next;
}
int main() {
LinkedList myList;
char choice;
9
int value, position, key;
do {
cout<< "\nMenu:\n";
cout<< "a) Insert an element at the beginning of the list.\n";
cout<< "b) Insert an element at the end of the list.\n";
cout<< "c) Insert an element at a given position in the list.\n";
cout<< "d) Insert an element after a given node in the list.\n";
cout<< "e) Insert an element before a given node in the list.\n";
cout<< "f) Delete an element from the beginning of the list.\n";
cout<< "g) Delete an element from the end of the list.\n";
cout<< "h) Delete an element at a given position in the list.\n";
cout<< "i) Delete an element by value from the list.\n";
cout<< "j) Search for a key element in the list.\n";
cout<< "k) Count the number of nodes in the list.\n";
cout<< "l) Traverse and display the list.\n";
cout<< "m) Exit.\n";
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 'a':
cout<< "Enter the value to insert: ";
cin>> value;
myList.insertElement(value);
break;
case 'b':
cout<< "Enter the value to insert at the end: ";
cin>> value;
myList.insertElementAtEnd(value);
break;
case 'c':
cout<< "Enter the value to insert: ";
cin>> value;
cout<< "Enter the position to insert: ";
cin>> position;
myList.insertElementAtPosition(value, position);
break;
case 'd':
cout<< "Enter the value to insert: ";
cin>> value;
cout<< "Enter the key after which to insert: ";
cin>> key;
myList.insertElementAfterNode(value, key);
break;
case 'e':
cout<< "Enter the value to insert: ";
cin>> value;
cout<< "Enter the key before which to insert: ";
cin>> key;
myList.insertElementBeforeNode(value, key);
10
break;
case 'f':
myList.deleteElementAtBeginning();
break;
case 'g':
myList.deleteElementAtEnd();
break;
case 'h':
cout<< "Enter the position to delete: ";
cin>> position;
myList.deleteElementAtPosition(position);
break;
case 'i':
cout<< "Enter the value to delete: ";
cin>> value;
myList.deleteElementByValue(value);
break;
case 'j':
cout<< "Enter the key element to search: ";
cin>> value;
myList.searchElement(value);
break;
case 'k':
myList.countNodes();
break;
case 'l':
myList.traverseList();
break;
case 'm':
cout<< "Exiting the program.\n";
break;
default:
cout<< "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 'm');
return 0;
}
OUTPUT:
11
INFERENCE:
A singly linked list is a unidirectional linked list. So, you can only traverse it in one direction,
i.e., from head node to tail node. Through this program we learnt how to implement some
operations on singly linked list.
RESULT:
The program was successfully executed and the output was got.
12
EX.NO:2
C++ PROGRAM TO IMPLEMENT DOUBLY LINKED LIST
DATE:
AIM:
To write a C++ program for implementing doubly linked list.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class for the node.
Step 3: Create a node.
Step 4: Create a doubly linked list
Step 5: Insert into the list
Step 6: Delete from the list.
Step 7: Print the list in reverse order.
Step 8: Stop the program.
PROGRAM:
#include <iostream>
Node(int val) {
data = val;
next = prev = NULL;
}
};
public:
DoublyLinkedList() {
head = NULL;
}
13
// Function to insert an element at the beginning of the list
void insertElement(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
cout<<val<< " inserted into the list." <<endl;
}
delete temp;
cout<< "First element deleted from the list." <<endl;
}
14
return;
}
if (!current) {
cout<< "Invalid position. Insertion failed." <<endl;
return;
}
newNode->next = current->next;
newNode->prev = current;
if (current->next) {
current->next->prev = newNode;
}
current->next = newNode;
cout<<val<< " inserted at position " << position << "." <<endl;
}
if (!current) {
cout<< key << " not found in the list. Insertion failed." <<endl;
return;
}
newNode->next = current->next;
newNode->prev = current;
if (current->next) {
current->next->prev = newNode;
}
current->next = newNode;
cout<<val<< " inserted after " << key << "." <<endl;
}
15
void insertElementBeforeNode(int val, int key) {
Node* newNode = new Node(val);
Node* current = head;
if (!current) {
cout<< key << " not found in the list. Insertion failed." <<endl;
return;
}
newNode->next = current;
newNode->prev = current->prev;
if (current->prev) {
current->prev->next = newNode;
} else {
head = newNode;
}
current->prev = newNode;
cout<<val<< " inserted before " << key << "." <<endl;
}
if (current->prev) {
current->prev->next = nullptr;
} else {
head = nullptr;
}
delete current;
cout<< "Last element deleted from the list." <<endl;
}
16
return;
}
if (!current) {
cout<< "Invalid position. Deletion failed." <<endl;
return;
}
if (current->prev) {
current->prev->next = current->next;
} else {
head = current->next;
}
if (current->next) {
current->next->prev = current->prev;
}
delete current;
cout<< "Element at position " << position << " deleted from the list." <<endl;
}
int main() {
DoublyLinkedListmyList;
char choice;
int value, position, key;
do {
cout<< "\nMenu:\n";
cout<< "a) Insert an element at the beginning of the list.\n";
cout<< "b) Insert an element at the end of the list.\n";
17
cout<< "c) Insert an element at a given position in the list.\n";
cout<< "d) Insert an element after a given node in the list.\n";
cout<< "e) Insert an element before a given node in the list.\n";
cout<< "f) Delete an element from the beginning of the list.\n";
cout<< "g) Delete an element from the end of the list.\n";
cout<< "h) Delete an element at a given position in the list.\n";
cout<< "i) Traverse and display the list.\n";
cout<< "j) Exit.\n";
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 'a':
cout<< "Enter the value to insert: ";
cin>> value;
myList.insertElement(value);
break;
case 'b':
cout<< "Enter the value to insert at the end: ";
cin>> value;
myList.insertElementAtEnd(value);
break;
case 'c':
cout<< "Enter the value to insert: ";
cin>> value;
cout<< "Enter the position to insert: ";
cin>> position;
myList.insertElementAtPosition(value, position);
break;
case 'd':
cout<< "Enter the value to insert: ";
cin>> value;
cout<< "Enter the key after which to insert: ";
cin>> key;
myList.insertElementAfterNode(value, key);
break;
case 'e':
cout<< "Enter the value to insert: ";
cin>> value;
cout<< "Enter the key before which to insert: ";
cin>> key;
myList.insertElementBeforeNode(value, key);
break;
case 'f':
myList.deleteElementAtBeginning();
break;
case 'g':
myList.deleteElementAtEnd();
break;
case 'h':
cout<< "Enter the position to delete: ";
18
cin>> position;
myList.deleteElementAtPosition(position);
break;
case 'i':
myList.traverseList();
break;
case 'j':
cout<< "Exiting the program.\n";
break;
default:
cout<< "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 'j');
return 0;
}
OUTPUT
INFERENCE:
A doubly linked list is a linked data structure that consists of a set of sequentially linked records
called nodes. Through this program we learnt how to implement some operations on doubly
linked list
RESULT
The program was successfully executed and the output was got.
19
EX.NO: 3
C++ PROGRAM TO PERFORM POLYNOMIAL ADDITION USING
SINGLY LINKED LIST
DATE:
AIM:
To write a C++ program to Perform Polynomial Addition using singly linked list.
ALGORITHM:
Step 1: Start the program.
Step 2: Insert a term into Polynomial 1.
Step 3: Insert a term into Polynomial 2
Step 4: Display Polynomial 1
Step 5: Display Polynomial 2
Step 6: Add Polynomials
Step 7: Print the result.
Step 8: Stop the program.
PROGRAM:
#include <iostream>
#include <cmath>
public:
Polynomial() : head(nullptr) {}
20
head = newTerm;
} else {
Term* current = head;
while (current->next) {
current = current->next;
}
current->next = newTerm;
}
}
21
result.insertTerm(term1->coefficient, term1->exponent);
term1 = term1->next;
}
while (term2) {
result.insertTerm(term2->coefficient, term2->exponent);
term2 = term2->next;
}
return result;
}
};
int main() {
Polynomial poly1, poly2, result;
char choice;
int coeff, exp;
do {
cout<< "\nMenu:\n";
cout<< "a) Insert a term into Polynomial 1.\n";
cout<< "b) Insert a term into Polynomial 2.\n";
cout<< "c) Display Polynomial 1.\n";
cout<< "d) Display Polynomial 2.\n";
cout<< "e) Add Polynomials.\n";
cout<< "f) Exit.\n";
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 'a':
cout<< "Enter coefficient and exponent for Polynomial 1 term: ";
cin>>coeff>> exp;
poly1.insertTerm(coeff, exp);
break;
case 'b':
cout<< "Enter coefficient and exponent for Polynomial 2 term: ";
cin>>coeff>> exp;
poly2.insertTerm(coeff, exp);
break;
case 'c':
cout<< "Polynomial 1: ";
poly1.displayPolynomial();
break;
case 'd':
cout<< "Polynomial 2: ";
poly2.displayPolynomial();
break;
case 'e':
result = poly1.addPolynomials(poly2);
cout<< "Resultant Polynomial (Polynomial 1 + Polynomial 2): ";
22
result.displayPolynomial();
break;
case 'f':
cout<< "Exiting the program.\n";
break;
default:
cout<< "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 'f');
return 0;
}
OUTPUT:
INFERENCE:
Polynomials are algebraic expressions that consist of constants and variables of different powers.
In this program we learnt to add two polynomials.
RESULT:
The program was successfully executed and the output was got.
23
EX.NO: 4
C++ PROGRAM TO IMPLEMENT STACK OPERATIONS
DATE:
AIM:
To write a C++ program to implement stack operations.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a node.
Step 3: Create a pointer to top of the stack.
Step 4: Do the push operation.
Step 5: Do the pop operation.
Step 6: Go to the top of the stack.
Step 7: Check if the stack is empty.
Step 8: Stop the program.
PROGRAM:
#include <iostream>
// Stack class
class Stack {
private:
Node* top; // Pointer to the top of the stack
public:
Stack(){
top=NULL;
}
24
void push(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = top;
top = newNode;
cout<< "Pushed: " << value <<endl;
}
int main() {
Stack stack;
int choice, value;
do {
cout<< "Stack Operations Menu:" <<endl;
cout<< "1. Push" <<endl;
cout<< "2. Pop" <<endl;
cout<< "3. Top" <<endl;
cout<< "4. Is Empty" <<endl;
25
cout<< "5. Exit" <<endl;
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 1:
cout<< "Enter the value to push: ";
cin>> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
value = stack.getTop();
if (value != -1) {
cout<< "Top element: " << value <<endl;
}
break;
case 4:
cout<< (stack.isEmpty() ? "Stack is empty." : "Stack is not empty.") <<endl;
break;
case 5:
cout<< "Exiting program." <<endl;
break;
default:
cout<< "Invalid choice. Please try again." <<endl;
}
} while (choice != 5);
return 0;
}
OUTPUT:
INFERENCE:
A Stack is a linear data structure that holds a linear, ordered sequence of elements. In this
program we learnt to implement stack operations.
RESULT:
The program was successfully executed and the output was got.
26
EX.NO: 5
C++ PROGRAMS TO IMPLEMENT A QUEUE OPERATIONS USING A
LINKED LIST
DATE:
AIM:
To write a C++ program to implement a queue operations using a linked list
ALGORITHM:
Step 1: Start the program.
Step 2: Create a node.
Step 3: Create a queue.
Step 4: Add an element to the queue.
Step 5: Delete an element from the queue.
Step 6: Get the front element in the queue.
Step 7: Check if the queue is empty.
Step 8: Stop the program.
PROGRAM:
#include <iostream>
// Queue class
class Queue {
private:
Node* front; // Pointer to the front of the queue
Node* rear; // Pointer to the rear of the queue
public:
Queue(){
front = NULL;
rear = NULL;
}
27
// Function to enqueue a new element into the queue
void enqueue(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = NULL;
if (isEmpty()) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
if (front == NULL) {
rear = NULL;
}
28
return front == NULL;
}
int main() {
Queue queue;
int choice, value;
do {
cout<< "\nQueue Operations Menu:" <<endl;
cout<< "1. Enqueue" <<endl;
cout<< "2. Dequeue" <<endl;
cout<< "3. Front" <<endl;
cout<< "4. Is Empty" <<endl;
cout<< "5. Display Queue" <<endl;
cout<< "6. Exit" <<endl;
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 1:
cout<< "Enter the value to enqueue: ";
cin>> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
29
value = queue.getFront();
if (value != -1) {
cout<< "Front element: " << value <<endl;
}
break;
case 4:
cout<< (queue.isEmpty() ? "Queue is empty." : "Queue is not empty.") <<endl;
break;
case 5:
queue.displayQueue();
break;
case 6:
cout<< "Exiting program." <<endl;
break;
default:
cout<< "Invalid choice. Please try again." <<endl;
}
} while (choice != 6);
return 0;
}
OUTPUT:
INFERENCE:
A queue follows the FIFO (First In First Out) method and is open at both of its ends. In this
program we learnt to implement queue operations.
RESULT:
The program was successfully executed and the output was got.
30
EX.NO: 6
CONVERT INFIX TO POSTFIX EXPRESSION USING STACK
DATE:
AIM:
To implement a C++ program to convert Infix to Postfix Expression using Stack
ALGORITHM:
Step 1: Start the program.
Step 2: Read the infix expression.
Step 3: Convert to postfix notation.
Step 4: Print the postfix expression.
Step 5: Stop the program.
PROGRAM:
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
if (op == '^')
return 3;
return 2;
31
return 1;
else
stack<char> operators;
string postfix;
if (isalnum(ch)) {
postfix += ch;
operators.push(ch);
// Pop operators from the stack and add them to the postfix expression until '(' is encountered
postfix += operators.top();
operators.pop();
operators.pop();
32
} else if (isOperator(ch)) {
postfix += operators.top();
operators.pop();
operators.push(ch);
// Pop any remaining operators from the stack and add them to the postfix expression
while (!operators.empty()) {
postfix += operators.top();
operators.pop();
return postfix;
int main() {
string infixExpression;
getline(cin, infixExpression);
return 0;}
33
OUTPUT:
INFERENCE:
Postfix expression can be defined as an expression in which all the operators are present after the
operands. In this program we learnt to implement the conversion of infix to postfix notation.
RESULT:
The program was successfully executed and the output was got.
34
EX.NO: 7
C++ PROGRAMS TO IMPLEMENT BINARY SEARCH TREE
OPERATIONS
DATE:
AIM:
. Write C++ programs to implement binary search tree operations.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a node.
Step 3: Insert an element into a binary search tree.
Step 4: Delete an element from a binary search tree.
Step 5: Search for a key element in a binary search tree.
Step 6: Print the results.
Step 6: Stop the program.
PROGRAM:
#include <iostream>
// BinarySearchTree class
class BinarySearchTree {
private:
TreeNode* root;
public:
BinarySearchTree(){
root=NULL;
}
35
if (node == NULL) {
TreeNode* newNode = new TreeNode;
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
return node;
}
// Function to find the node with the minimum value in a binary search tree
TreeNode* findMin(TreeNode* node) {
while (node->left != NULL) {
node = node->left;
}
return node;
}
36
delete node;
return temp;
} else if (node->right == NULL) {
TreeNode* temp = node->left;
delete node;
return temp;
}
// Case 2: Node with two children, get the inorder successor (smallest in the right
subtree)
TreeNode* temp = findMin(node->right);
return node;
}
if (key == node->data) {
return true;
} else if (key < node->data) {
return search(node->left, key);
} else {
return search(node->right, key);
}
}
37
bool search(int key) {
return search(root, key);
}
void displayInOrder() {
cout<< "In-Order Traversal: ";
displayInOrder(root);
cout<<endl;
}
};
int main() {
BinarySearchTreebst;
int choice, value;
do {
cout<< "Binary Search Tree Operations Menu:" <<endl;
cout<< "1. Insert" <<endl;
cout<< "2. Delete" <<endl;
cout<< "3. Search" <<endl;
cout<< "4. Display In-Order" <<endl;
cout<< "5. Exit" <<endl;
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 1:
cout<< "Enter the value to insert: ";
cin>> value;
bst.insert(value);
break;
case 2:
cout<< "Enter the value to delete: ";
cin>> value;
bst.remove(value);
38
break;
case 3:
cout<< "Enter the value to search: ";
cin>> value;
cout<< (bst.search(value) ? "Element found." : "Element not found.") <<endl;
break;
case 4:
bst.displayInOrder();
break;
case 5:
cout<< "Exiting program." <<endl;
break;
default:
cout<< "Invalid choice. Please try again." <<endl;
}
return 0;
}
OUTPUT:
INFERENCE:
A binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree
data structure with the key of each internal node being greater than all the keys in the respective
node's left subtree and less than the ones in its right subtree. In this program we learnt to
implement binary search tree operations.
RESULT:
The program was successfully executed and the output was got.
39
EX.NO: 8
BINARY TREE TRAVERSAL
DATE:
AIM:
To implement C++ program that use recursive functions to traverse the given binary tree.
ALGORITHM:
Step 1: Start the program.
Step 2: Node structure for binary tree.
Step 3: Create a structure for binary tree.
Step 4: Create a binary tree.
Step 5: Do the tree traversal-inorder.
Step 6: Do the tree traversal-preorder.
Step 7: Do the tree traversal-postorder.
Step 8: Print the results.
Step 9: Stop the program.
PROGRAM:
#include <iostream>
// BinaryTree class
class BinaryTree {
private:
TreeNode* root;
public:
BinaryTree(){
root=NULL;
}
40
// Function to create a new node
TreeNode* createNode(int value) {
TreeNode* newNode = new TreeNode;
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
return node;
}
41
void inorderTraversal(TreeNode* node) {
if (node != NULL) {
inorderTraversal(node->left);
cout<< node->data << " ";
inorderTraversal(node->right);
}
}
int main() {
BinaryTreebinaryTree;
int choice, value;
do {
cout<< "Binary Tree Traversal Menu:" <<endl;
cout<< "1. Insert Element" <<endl;
cout<< "2. Preorder Traversal" <<endl;
cout<< "3. Inorder Traversal" <<endl;
cout<< "4. Postorder Traversal" <<endl;
cout<< "5. Exit" <<endl;
cout<< "Enter your choice: ";
cin>> choice;
42
switch (choice) {
case 1:
cout<< "Enter the value to insert: ";
cin>> value;
binaryTree.insert(value);
break;
case 2:
binaryTree.preorderTraversal();
break;
case 3:
binaryTree.inorderTraversal();
break;
case 4:
binaryTree.postorderTraversal();
break;
case 5:
cout<< "Exiting program." <<endl;
break;
default:
cout<< "Invalid choice. Please try again." <<endl;
}
return 0;
}
OUTPUT:
INFERENCE:
A binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree
data structure with the key of each internal node being greater than all the keys in the respective
node's left subtree and less than the ones in its right subtree. In this program we learnt to
implement binary tree traversal.
RESULT:
The program was successfully executed and the output was got.
43
EX.NO: 9
IMPLEMENT AN AVL TREE
DATE:
AIM:
Write C++ programs to implement an AVL Tree
ALGORITHM:
Step 1: Start the program.
Step 2: Create a Node for AVL Tree node.
Step 3: Create a class for AVL tree.
Step 4: Create a new node.
Step 5: Get the height of the tree.
Step 6: Do the left rotation.
Step 7: Do the right rotation.
Step 8: Get the balance factor of a node.
Step 9: Balance the tree through rotations of left and right.
Step 10: Do the tree traversals.
Step 11: Print the results.
Step 12: Stop the program.
PROGRAM:
#include <iostream>
#include <algorithm>
// AVLTree class
class AVLTree {
private:
AVLNode* root;
44
public:
AVLTree(){
root=NULL;
}
x->right = y;
y->left = T2;
updateHeight(y);
updateHeight(x);
return x;
}
y->left = x;
x->right = T2;
45
updateHeight(x);
updateHeight(y);
return y;
}
46
node->left = leftRotate(node->left);
return rightRotate(node);
}
return node;
}
int main() {
AVLTreeavlTree;
int choice, value;
do {
cout<< "AVL Tree Operations Menu:" <<endl;
cout<< "1. Insert Element" <<endl;
cout<< "2. In-Order Traversal" <<endl;
cout<< "3. Exit" <<endl;
cout<< "Enter your choice: ";
47
cin>> choice;
switch (choice) {
case 1:
cout<< "Enter the value to insert: ";
cin>> value;
avlTree.insert(value);
break;
case 2:
avlTree.inOrderTraversal();
break;
case 3:
cout<< "Exiting program." <<endl;
break;
default:
cout<< "Invalid choice. Please try again." <<endl;
}
return 0;
}
OUTPUT:
INFERENCE:
AVL tree in data structures is a popular self-balancing binary search tree where the difference
between the heights of left and right subtrees for any node does not exceed unity. In this program
we learnt to implement AVL trees.
RESULT:
The program was successfully executed and the output was got.
48
EX.NO: 10
C++ PROGRAM THAT DEMONSTRATES B-TREE OPERATION
DATE:
AIM:
Write a C++ program that demonstrates B-Tree operation insertion, search, and display.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a btree node.
Step 3: Create a function for btree node.
Step 4: Create a function to split the current node.
Step 5: Create a function to insert a key into Btree.
Step 6: Create a function to search a key in btree.
Step 7: Create a function to display the display the tree.
Step 8: Stop the program.
PROGRAM:
#include <iostream>
using namespace std;
49
BTreeNode* newNode = createNode(child->leaf, degree);
newNode->n = degree - 1;
if (!child->leaf) {
for (int i = 0; i< degree; i++)
newNode->children[i] = child->children[i + degree];
}
child->n = degree - 1;
parentNode->children[index + 1] = newNode;
50
root->n++;
}
}
}
int i = 0;
while (i< root->n && key > root->keys[i])
i++;
if (root->leaf)
return false;
int main() {
BTreeNode* root = nullptr;
int degree;
while (true) {
cout<< "\nB-Tree Operations:\n";
cout<< "1. Insert Key\n";
cout<< "2. Search Key\n";
cout<< "3. Display B-Tree\n";
51
cout<< "4. Exit\n";
int choice;
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 1: {
int key;
cout<< "Enter key to insert: ";
cin>> key;
insert(root, key, degree);
cout<< "Key inserted successfully!\n";
break;
}
case 2: {
int key;
cout<< "Enter key to search: ";
cin>> key;
if (search(root, key))
cout<< "Key found in the B-Tree.\n";
else
cout<< "Key not found in the B-Tree.\n";
break;
}
case 3:
cout<< "B-Tree:\n";
display(root, 0);
break;
case 4:
cout<< "Exiting program.\n";
return 0;
default:
cout<< "Invalid choice. Try again.\n";
}
}
return 0;
}
OUTPUT:
52
INFERENCE:
B-tree is a self-balancing data structure commonly used in computer science for efficient storage
and retrieval of large amounts of data. In this program we learnt to implement B-Tree operation
insertion, search, and display.
RESULT:
The program was successfully executed and the output was got.
53
EX.NO: 11 DIJKSTRA'S SINGLE SOURCE SHORTEST PATH
ALGORITHM
DATE:
AIM:
Write a C++ program for Dijkstra's single source shortest path algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Set the number of vertices to 9.
Step 3: Find the vertex with minimum distance value, from the set of vertices not yet included in
shortest path tree.
Step 4: Create a function to print the constructed distance.
Step5: Function that implements Dijkstra's single source shortest path algorithm for a graph
represented using adjacency matrix representation.
Step 6: The output array. dist[i] will hold the shortest distance from src to i.
Step 7: sptSet[i] will be true if vertex i is included in shortest path tree or shortest distance from
src to i is finalized.
Step 8: Initialize all distances as INFINITE and stpSet[] as false.
Step 9: Find shortest path for all vertices.
Step 10: Pick the minimum distance vertex from the set of vertices not yet processed. u is always
equal to src in the first iteration.
Step 11: Mark the picked vertex as processed
Step 12: Update dist value of the adjacent vertices of the picked vertex.
Step 13: Update dist[v] only if is not in sptSet, there is an edge from u to v, and total weight of
path from src to v through u is smaller than current value of dist[v]
Step 14: Print the constructed distance array
Step 15: Stop the program.
PROGRAM:
#include <iostream>
#include <limits.h>
#define V 9
54
return min_index;
}
dist[src] = 0;
sptSet[u] = true;
printSolution(dist, V);
}
int main() {
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}};
dijkstra(graph, 0);
return 0;
}
55
OUTPUT:
INFERENCE:
The Dijkstra Shortest Path algorithm computes the shortest path between nodes. The algorithm
supports weighted graphs with positive relationship weights. In this program we learnt how to
implement Dijkstra Shortest Path algorithm.
RESULT:
The program was successfully executed and the output was got.
56
EX.NO: 12
PRIM'S MINIMUM SPANNING TREE
DATE:
AIM:
To Write a C++ program for Prim's Minimum Spanning Tree (MST) algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of nodes. Here it is 6.
Step 3: Create a node.
Step 4: Get the source node.
Step 5: Get the destination node.
Step 6: Get the weight of the node.
Step 7: Get the minimum for each time.
Step 8: Stop the program.
PROGRAM:
#include <iostream>
57
else if (b[i][j] < min)
{
min = b[i][j];
temp = i;
temp1 = j;
}
}
}
}
a[temp1] = 1;
p[c].fr = temp;
p[c].to = temp1;
p[c].cost = min;
c++;
b[temp][temp1] = b[temp1][temp]=1000;
}
for (int k = 0; k < 6; k++)
{
cout<<"source node:"<<p[k].fr<<endl;
cout<<"destination node:"<<p[k].to<<endl;
cout<<"weight of node"<<p[k].cost<<endl;
}
}
int main()
{
int a[7];
for (int i = 0; i< 7; i++)
{
a[i] = 0;
}
int b[7][7];
for (int i = 0; i< 7; i++)
{
cout<<"enter values for "<<(i+1)<<" row"<<endl;
for (int j = 0; j < 7; j++)
{
cin>>b[i][j];
}
}
prims(a, b, 0, 0);
OUTPUT:
58
INFERENCE:
The algorithm starts with an empty spanning tree. The idea is to maintain two sets of vertices.
The first set contains the vertices already included in the MST, and the other set contains the
vertices not yet included. At every step, it considers all the edges that connect the two sets and
picks the minimum weight edge from these edges. After picking the edge, it moves the other
endpoint of the edge to the set containing MST. Through this program we have learnt how to
implement Prim's Minimum Spanning Tree (MST) algorithm.
RESULT:
The program was successfully executed and the output was got.
59
EX.NO: 13
BINARY SEARCH USING A RECURSIVE FUNCTION
DATE:
AIM:
To Write a C++ programs to implement binary search using a recursive function.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of elements in the array.
Step 3: Get the elements in the array.
Step 4: Get the element to be searched.
Step 5: Sort the array.
Step 6: Do the binary search.
Step 7: Binary search is done in recursion.
Step 8: Print the results.
Step 9: Stop the program.
PROGRAM:
#include <iostream>
using namespace std;
if (arr[mid] == key)
return mid;
else if (arr[mid] > key)
return binarySearchRecursive(arr, low, mid - 1, key);
else
return binarySearchRecursive(arr, mid + 1, high, key);
}
int main() {
const int MAX_SIZE = 100;
60
int arr[MAX_SIZE];
int n, key, result;
if (result != -1)
cout<< "Element found at index " << result <<endl;
else
cout<< "Element not found in the array." <<endl;
return 0;
}
OUTPUT:
INFERENCE:
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing
the search interval in half. The idea of binary search is to use the information that the array is
sorted. In this program we learnt how to implement binary search using recursion.
RESULT:
The program was successfully executed and the output was got.
61
EX.NO: 14
INSERTION SORT ALGORITHM
DATE:
AIM:
To Write a C++ program to implement the Insertion Sort algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of elements in the array.
Step 3: Get the elements in the array.
Step 4: Sort the array using insertion sort.
Step 5: Display the array after sorting.
Step 6: Stop the program.
PROGRAM:
#include <iostream>
using namespace std;
arr[j + 1] = key;
}
}
62
int main() {
const int MAX_SIZE = 100;
int arr[MAX_SIZE];
int n;
insertionSort(arr, n);
displayArray(arr, n);
return 0;
}
OUTPUT:
INFERENCE:
Insertion sort is a simple sorting algorithm that works similar to the way we sort playing cards in
our hands. The array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted part. In this program we
learnt to implement the Insertion Sort algorithm.
RESULT:
The program was successfully executed and the output was got.
63
EX.NO: 15
SEPARATE CHAINING TECHNIQUE IN HASHING
DATE:
AIM:
To Write a C++ program to implement the separate chaining technique in hashing
ALGORITHM:
Step 1: Start the program.
Step 2: Set Hash table size to 10.
Step 3: Insert the key in the hash table.
Step 4: Search a key in the hash table.
Step 5: Display the hash table.
Step 6: Stop the program.
PROGRAM:
#include <iostream>
#include <list>
#include <iterator>
class HashTable {
private:
static const int tableSize = 10;
list<int> table[tableSize];
public:
void insertKey(int key) {
int index = hashFunction(key);
table[index].push_back(key);
}
void displayTable() {
for (int i = 0; i<tableSize; ++i) {
cout<< "Bucket " <<i<< ": ";
copy(table[i].begin(), table[i].end(), ostream_iterator<int>(cout, " "));
cout<<endl;
}
64
}
int main() {
HashTablehashTable;
int choice, key;
do {
cout<< "\n1. Insert Key\n";
cout<< "2. Display Hash Table\n";
cout<< "3. Search Key\n";
cout<< "4. Exit\n";
cout<< "Enter your choice: ";
cin>> choice;
switch (choice) {
case 1:
cout<< "Enter key to insert: ";
cin>> key;
hashTable.insertKey(key);
break;
case 2:
cout<< "Hash Table:\n";
hashTable.displayTable();
break;
case 3:
cout<< "Enter key to search: ";
cin>> key;
if (hashTable.searchKey(key))
cout<< "Key " << key << " found in the hash table.\n";
else
cout<< "Key " << key << " not found in the hash table.\n";
break;
case 4:
65
cout<< "Exiting the program.\n";
break;
default:
cout<< "Invalid choice! Please enter a valid option.\n";
}
return 0;
}
OUTPUT:
INFERENCE:
Hashing is a technique or process of mapping keys, and values into the hash table by using a
hash function. It is done for faster access to elements. The efficiency of mapping depends on the
efficiency of the hash function used. In this program we learnt to implement separate chaining
technique in hashing.
RESULT:
The program was successfully executed and the output was got.
66