Data structure and Algorithm lab reports, Lab Reports of Data Structures and Algorithms

The creation of a binary search tree (BST) and the different operations that can be performed on it, such as insertion, deletion, and searching. It also defines important terms related to trees, such as path, root, parent, child, leaf, and subtree. code examples for each operation and a menu-based program for BST. This document can be useful for computer science students studying data structures and algorithms.

Typology: Lab Reports

2020/2021

Available from 01/15/2022

syed-afaq-hussain-shah
syed-afaq-hussain-shah 🇺🇸

14 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18-EE-21
Lab # 10
Title:
Creation of Binary Search Tree and insertion, deletion and searching operations on BST
Objective:
How to create BST
To perform different operation on BST
Equipment Required:
PC/ Laptop
Dev C++ software
Binary Search Tree:
Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree
specifically. Binary Tree is a special data structure used for data storage purposes. A binary tree has a
special condition that each node can have a maximum of two children. A binary tree has the benefits
of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or
deletion operation are as fast as in linked list.
Important Terms:
Following are the important terms with respect to tree.
oPath − Path refers to the sequence of nodes along the edges of a tree.
oRoot − The node at the top of the tree is
called root. There is only one root per tree
and one path from the root node to any
node.
oParent − Any node except the root node has
one edge upward to a node called parent.
oChild − The node below a given node
connected by its edge downward is called
its child node.
oLeaf − The node which does not have any
child node is called the leaf node.
oSubtree − Subtree represents the descendants of a node.
oVisiting − Visiting refers to checking the value of a node when control is on the node.
oTraversing − Traversing means passing through nodes in a specific order.
oLevels − Level of a node represents the generation of a node. If the root node is at level 0, then its
next child node is at level 1, its grandchild is at level 2, and so on.
okeys − Key represents a value of a node based on which a search operation is to be carried out for
a node.
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Data structure and Algorithm lab reports and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 10

 Title:

Creation of Binary Search Tree and insertion, deletion and searching operations on BST

 Objective:

 How to create BST

 To perform different operation on BST

 Equipment Required:

 PC/ Laptop

 Dev C++ software

 Binary Search Tree:

Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree

specifically. Binary Tree is a special data structure used for data storage purposes. A binary tree has a

special condition that each node can have a maximum of two children. A binary tree has the benefits

of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or

deletion operation are as fast as in linked list.

 Important Terms:

Following are the important terms with respect to tree.

o Path − Path refers to the sequence of nodes along the edges of a tree.

o Root − The node at the top of the tree is

called root. There is only one root per tree

and one pat h from the root node to any

node.

o Parent − Any node except the root node has

one edge upward to a node called parent.

o Child − The node below a given node

connected by its edge downward is called

its child node.

o Leaf − The node which does not have any

child node is called the leaf node.

o Subtree − Subtree represents the descendants of a node.

o Visiting − Visiting refers to checking the value of a node when control is on the node.

o Traversing − Traversing means passing through nodes in a specific order.

o Levels − Level of a node represents the generation of a node. If the root node is at level 0, then its

next child node is at level 1, its grandchild is at level 2, and so on.

o keys − Key represents a value of a node based on which a search operation is to be carried out for

a node.

Binary Search Tree Representation:

Binary Search tree exhibits a special behavior. A node's left child must have a value less than its

parent's value and the node's right child must have a value greater than its parent value.

We're going to implement tree using node object and connecting them through references.

BST Basic Operations:

The basic operations that can be performed on a binary search tree data structure are the following −

o Insert − Inserts an element in a tree/create a tree.

o Search − Searches an element in a tree.

o Preorder Traversal − Traverses a tree in a pre-order manner.

o Inorder Traversal − Traverses a tree in an in-order manner.

o Postorder Traversal − Traverses a tree in a post-order manner.

Insert Operation:

The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first locate

its proper location. Start searching from the root node, then if the data is less than the key value,

search for the empty location in the left subtree and insert the data. Otherwise, search for the empty

location in the right subtree and insert the data.

Search Operation:

Whenever an element is to be searched, start searching from the root node, then if the data is less

than the key value, search for the element in the left subtree. Otherwise, search for the element in the

right subtree. Follow the same algorithm for each node.

Tree Node:

The code to write a tree node would be similar to what is given below. It has a data part and

references to its left and right child nodes.

struct node {

int data;

struct node *leftChild;

struct node *rightChild;

return; } else { find(item, &parent, &location); cout<<"\n\n\t\tNode is present in BST"; } }

2. Write a program that insert a node in Binary Search Tree?

Function to Insert a Node in BST:

void BST::insert(node *tree, node *newnode) { if (root == NULL) { root = new node; root->info = newnode->info; root->left = NULL; root->right = NULL; cout<<"Root Node is Added"<<endl; return; } if (tree->info == newnode->info) { cout<<"Element already in the tree"<<endl; return; } if (tree->info > newnode->info) { if (tree->left != NULL) { insert(tree->left, newnode); } else { tree->left = newnode; (tree->left)->left = NULL; (tree->left)->right = NULL; cout<<"Node Added To Left"<<endl; return; } } else { if (tree->right != NULL) { insert(tree->right, newnode); } else { tree->right = newnode; (tree->right)->left = NULL; (tree->right)->right = NULL; cout<<"Node Added To Right"<<endl; return; } }

3. Write a program that delete a node in Binary Search Tree? Write a

program for leaf, one child and both children deletion?

Function to delete a Node in BST:

void BST::del(int item)

node *parent, *location;

if (root == NULL)

cout<<"Tree empty"<<endl;

return;

find(item, &parent, &location);

if (location == NULL)

cout<<"Item not present in tree"<<endl;

return;

if (location->left == NULL && location->right == NULL)

case_a(parent, location);

if (location->left != NULL && location->right == NULL)

case_b(parent, location);

if (location->left == NULL && location->right != NULL)

case_b(parent, location);

if (location->left != NULL && location->right != NULL)

case_c(parent, location);

free(location);

Function to delete a Leaf in BST:

void BST::case_a(node *par, node *loc ) { if (par == NULL) { root = NULL; } else { if (loc == par->left) par->left = NULL; else par->right = NULL; } }

Function to delete a Child in BST:

void BST::case_b(node *par, node *loc) { node *child; if (loc->left != NULL) child = loc->left; else child = loc->right; if (par == NULL) {

public: void find(int, node **, node **); void search(); void insert(node *, node *); void del(int); void case_a(node *,node *); void case_b(node *,node *); void case_c(node *,node *); void display(node *, int); BST() { root = NULL; } }; int main () { int ch, num; BST bst; node *temp; while (1) { cout<<"\n\t >>>>>>>> Menu of Binary Search Tree (BST) <<<<<<<<"; cout<<"\n"; cout<<"\n\t-------------------------------------------------------------"; cout<<"\n\tENTER 1: Insert element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 2: Displaying BST"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 3: Search Element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 4: Delete element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 5: To Exit"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\n\tPlease Enter Your Choice: "; cin>>ch; switch(ch){ case 1: system("cls"); temp = new node; cout<<"\n\n\tEnter the number to be inserted : "; cin>>temp->info; bst.insert(root, temp);Sleep(2000);system("cls"); break; case 2: system("cls"); cout<<"\n\n\t\tDisplay BST:\n"<<endl; bst.display(root,1); cout<<endl;Sleep(5000);system("cls"); break; case 3: system("cls"); bst.search(); Sleep(2000);system("cls"); break; case 4: system("cls"); if (root == NULL)

cout<<"\n\n\tTree is empty, nothing to delete"<<endl; continue; } cout<<"\n\n\tEnter the number to be deleted : "; cin>>num; bst.del(num); Sleep(2000);system("cls"); break; case 5: exit(0); default: cout<<"\nWrong choice!!"; } } return 0; } void BST::insert(node *tree, node *newnode) { if (root == NULL) { root = new node; root->info = newnode->info; root->left = NULL; root->right = NULL; cout<<"\n\tRoot Node is Added"<<endl; return; } if (tree->info == newnode->info) { cout<<"\n\tElement already in the tree"<<endl; return; } if (tree->info > newnode->info) { if (tree->left != NULL) { insert(tree->left, newnode); } else { tree->left = newnode; (tree->left)->left = NULL; (tree->left)->right = NULL; cout<<"\n\tNode Added To Left"<<endl; return; } } else { if (tree->right != NULL) { insert(tree->right, newnode); } else { tree->right = newnode; (tree->right)->left = NULL; (tree->right)->right = NULL; cout<<"\n\tNode Added To Right"<<endl; return;

void BST::search() { int item; node ptr, ptrsave,loc,**par; node *parent, *location; if (root == NULL) { cout<<"\n\n\t\tTree empty"<<endl; return; } find(item, &parent, &location); cout<<"\n\n\t\tEnter Element to be searched: "; cin>>item; find(item, &parent, &location); if (location == NULL) { cout<<"\n\n\t\tItem not present in tree"<<endl; return; } else { find(item, &parent, &location); cout<<"\n\n\t\tNode is present in BST"; } } void BST::del(int item) { node *parent, *location; if (root == NULL) { cout<<"\n\n\t\tTree empty"<<endl; return; } find(item, &parent, &location); if (location == NULL) { cout<<"\n\n\t\tItem not present in tree"<<endl; return; } if (location->left == NULL && location->right == NULL) case_a(parent, location); if (location->left != NULL && location->right == NULL) case_b(parent, location); if (location->left == NULL && location->right != NULL) case_b(parent, location); if (location->left != NULL && location->right != NULL) case_c(parent, location); free(location); } void BST::case_a(node *par, node *loc ) { if (par == NULL) { root = NULL; } else {

if (loc == par->left) par->left = NULL; else par->right = NULL; } } void BST::case_b(node *par, node *loc) { node *child; if (loc->left != NULL) child = loc->left; else child = loc->right; if (par == NULL) { root = child; } else { if (loc == par->left) par->left = child; else par->right = child; } } void BST::case_c(node *par, node *loc) { node *ptr, *ptrsave, *suc, *parsuc; ptrsave = loc; ptr = loc->right; while (ptr->left != NULL) { ptrsave = ptr; ptr = ptr->left; } suc = ptr; parsuc = ptrsave; if (suc->left == NULL && suc->right == NULL) case_a(parsuc, suc); else case_b(parsuc, suc); if (par == NULL) { root = suc; } else { if (loc == par->left) par->left = suc; else par->right = suc; } suc->left = loc->left; suc->right = loc->right; }

Option#2 is used to Display element of BST as shown in fig Option#3 is used to find binary node in a BST as shown in figs

When tree is empty no element present

When Element found in BST

When Searched element not present in BST

Option#4 is used to delete element of BST as shown in figs

After Deletion

Option#5 is used to Exit from BST program