0% found this document useful (0 votes)
2 views

binary lab no 8

The document outlines a lab session for BS Cyber Security and Digital Forensics students at the Islamia University of Bahawalpur, focusing on the implementation of binary search trees (BST). It details the structure and operations of BSTs, including insertion, deletion, searching, and finding minimum and maximum values, along with a sample code implementation in C++. The lab aims to enhance understanding of data structures and algorithms through practical coding exercises.

Uploaded by

alsamadlqp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

binary lab no 8

The document outlines a lab session for BS Cyber Security and Digital Forensics students at the Islamia University of Bahawalpur, focusing on the implementation of binary search trees (BST). It details the structure and operations of BSTs, including insertion, deletion, searching, and finding minimum and maximum values, along with a sample code implementation in C++. The lab aims to enhance understanding of data structures and algorithms through practical coding exercises.

Uploaded by

alsamadlqp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

The Islamia University of Bahawalpur

Faculty of Engineering

BS Cyber Security and Digital 3rd semester


Forensics

Lab # 8 Binary Search Trees

Course: Data Structures and Date:


Algorithms lab

Submitted by: Muhammad zameer Roll No. F23BINCE1M04084


malik

Objectives

The purpose of this lab session is to understand the implementation of operations on


binary trees.

Binary Search Trees:

A binary tree is a tree in which no node can have more than two children. In a binary
search tree ,for every node, X, in the tree, the values of all the keys in its left sub tree
are smaller than the key value of X, and the values of all the keys in its right sub tree
are larger than the key value of X. The basic operations on a binary search tree take
time proportional to the height of the tree.

In the linked list implementation of binary search trees: Each element is represented
by node with two link fields and a data field. Each connecting line (or edge) in a binary
tree drawing will be represented by a link field. A leaf node has a leftChild and
rightChild link of NULL. Root node will be pointed to by a pointer variable.

Algorithm:

1. Create a structure with an element (Element) and two pointers – Left and Right that
points to the left and right child node respectively in the tree.

2. Create a new node and assign the resultant pointer variable to the root node
pointer T and assign it to NULL.

3. To insert a new element X into the tree:

a. If the value of T is NULL, assign T->Element to X, and the left and right child
pointers to NULL and exit the insertion operation.

b. Otherwise if the element to be inserted is less than the root element T, repeat the
step 3 recursively, with the new value of T as T->Left.

c. Otherwise if the element to be inserted is more than the root element T, repeat the
step 3 recursively, with the new value of T as T->Right.
d. If the element is already present in the tree, do nothing.

4. To delete an element X from the tree:

a. Find the node where the element resides.

b. If the node has no left and right children, then the pointer to that node from the
parent is changed to NULL and the node is freed of its memory.

c. If the node has only one child, then the parent of the node is made to point to the
child of the node and the node is freed.

d. If the node has both left and right children:

i. Look at the right subtree of the node (subtree rooted at the right child of the node).

ii. Find the Minimum there.

iii. Replace the key of the node to be deleted by the minimum element.

iv. Delete the minimum element.

5. To find an element X in the tree with root node T:

a. If the root node T is initially NULL, then the tree is empty. So return NULL and exit.

b. Take the element X and compare it with the root node. If X is less than the element
found at the root node, then repeat step 5 recursively with the new value of T as T-
>Left.

c. Take the element X and compare it with the root node. If X is more than the element
found at the root node, then repeat step 5 recursively with the new value of T as T-
>Right.

6. To find the minimum element in a tree with root node T:

a. If T is NULL return NULL.

b. Otherwise slide the value of T to T->Left until T->Left becomes NULL.

c. Return the value of T.

7. To find the maximum element in a tree with root node T:

a. If T is NULL return NULL.

b. Otherwise slide the value of T to T->Right until T->Right becomes NULL.

c. Return the value of T.

Tasks:

1. Write the program to implement binary search tree.

a. Searching
b. Insertion

c. Deletion

d. travers the tree in Inorder.

Code :

#include <iostream>

using namespace std;

// Define a Node structure

struct Node {

int data;

Node* left;

Node* right;

};

// Function to create a new node

Node* newNode(int data) {

Node* node = new Node();

node->data = data;

node->left = nullptr;

node->right = nullptr;

return node;

// Function to insert a new node in the BST

Node* insert(Node* root, int data) {

if (root == nullptr) {

return newNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else {

root->right = insert(root->right, data);


}

return root;

// Function to search for a value in the BST

bool search(Node* root, int data) {

if (root == nullptr) {

return false;

if (root->data == data) {

return true;

if (data < root->data) {

return search(root->left, data);

} else {

return search(root->right, data);

// Function to find the minimum value node in the BST

Node* findMin(Node* root) {

while (root->left != nullptr) {

root = root->left;

return root;

//

Node* deleteNode(Node* root, int data) {

if (root == nullptr) {

return root;

}
if (data < root->data) {

root->left = deleteNode(root->left, data);

} else if (data > root->data) {

root->right = deleteNode(root->right, data);

} else {

if (root->left == nullptr) {

Node* temp = root->right;

delete root;

return temp;

} else if (root->right == nullptr) {

Node* temp = root->left;

delete root;

return temp;

Node* temp = findMin(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

// Function for Inorder traversal (Left, Root, Right)

void inorderTraversal(Node* root) {

if (root == nullptr) {

return;

inorderTraversal(root->left);

cout << root->data << " ";

inorderTraversal(root->right);
}

int main() {

Node* root = nullptr;

// Insert nodes into the BST

root = insert(root, 50);

root = insert(root, 30);

root = insert(root, 20);

root = insert(root, 40);

root = insert(root, 70);

root = insert(root, 60);

root = insert(root, 80);

cout << "Inorder traversal of the BST: ";

inorderTraversal(root);

cout << endl;

int value = 40;

if (search(root, value)) {

cout << "Value " << value << " found in the BST." << endl;

} else {

cout << "Value " << value << " not found in the BST." << endl;

// Delete a node from the BST

root = deleteNode(root, 20);

cout << "Inorder traversal after deleting 20: ";

inorderTraversal(root);

cout << endl;

root = deleteNode(root, 30);


cout << "Inorder traversal after deleting 30: ";

inorderTraversal(root);

cout << endl;

root = deleteNode(root, 50);

cout << "Inorder traversal after deleting 50: ";

inorderTraversal(root);

cout << endl;

return 0;

Output:

2 :
Add the code find to minimum and maximum element in the tree.

Code: #include <iostream>

using namespace std;

// Define a Node structure

struct Node {

int data;

Node* left;

Node* right;

};

// Function to create a new node

Node* newNode(int data) {


Node* node = new Node();

node->data = data;

node->left = nullptr;

node->right = nullptr;

return node;

Node* insert(Node* root, int data) {

if (root == nullptr) {

return newNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else {

root->right = insert(root->right, data);

return root;

bool search(Node* root, int data) {

if (root == nullptr) {

return false;

if (root->data == data) {

return true;

if (data < root->data) {

return search(root->left, data);

} else {

return search(root->right, data);

}
Node* findMin(Node* root) {

while (root->left != nullptr) {

root = root->left;

return root;

Node* findMax(Node* root) {

while (root->right != nullptr) {

root = root->right;

return root;

Node* deleteNode(Node* root, int data) {

if (root == nullptr) {

return root;

if (data < root->data) {

root->left = deleteNode(root->left, data);

} else if (data > root->data) {

root->right = deleteNode(root->right, data);

} else {

if (root->left == nullptr) {

Node* temp = root->right;

delete root;

return temp;

} else if (root->right == nullptr) {

Node* temp = root->left;

delete root;
return temp;

Node* temp = findMin(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

//

void inorderTraversal(Node* root) {

if (root == nullptr) {

return;

inorderTraversal(root->left);

cout << root->data << " ";

inorderTraversal(root->right);

int main() {

Node* root = nullptr;

// Insert nodes into the BST

root = insert(root, 50);

root = insert(root, 30);

root = insert(root, 20);

root = insert(root, 40);

root = insert(root, 70);

root = insert(root, 60);

root = insert(root, 80);

// Perform Inorder traversal


cout << "Inorder traversal of the BST: ";

inorderTraversal(root);

cout << endl;

int value = 40;

if (search(root, value)) {

cout << "Value " << value << " found in the BST." << endl;

} else {

cout << "Value " << value << " not found in the BST." << endl;

Node* minNode = findMin(root);

Node* maxNode = findMax(root);

cout << "Minimum value in the BST: " << minNode->data << endl;

cout << "Maximum value in the BST: " << maxNode->data << endl;

root = deleteNode(root, 20);

cout << "Inorder traversal after deleting 20: ";

inorderTraversal(root);

cout << endl;

root = deleteNode(root, 30);

cout << "Inorder traversal after deleting 30: ";

inorderTraversal(root);

cout << endl;

root = deleteNode(root, 50);

cout << "Inorder traversal after deleting 50: ";

inorderTraversal(root);

cout << endl;

return 0;
}

Output:

You might also like