Lab Task-7
1. Write a C++ code to implement Binary Search Tree operations (insertion, traversal and
searching)
Do the following to write program for a BST:
1. To construct a binary search tree of integers (insert one by one).
2. To traverse the tree using all the methods i.e., in order, preorder and post order.
3. To search an element on the BST.
4. There are three cases when you delete a node.
• Case 1: Node with zero child (Leaf node)
• Case 2: Node with one child
• Case 3: Node with both children
Implement the logic of 3 cases one by one.
Hint: Your program should ask the user to input the choice what operation the user wants to perform.
1. Insert
2. Travers
3. Search
4. Delete
Class Node{ Class BST{
Data root
lptr , rptr BST(){
Node(){ Root = NULL
Everything null }
} insert(x){
Node(int a){ if(Root == null){
Data =a Node * nptr = new Node();
Everything else null Nptr->Data = x;
} Root = nptr;
} ……}// for inserting root node
else {………..}// for inserting rest of the nodes
}
Preorder(Node * tptr){
If(tptr !=Null){
Print(tptr->Data)
Preorder(tptr->lptr)
Preorder(tptr->rptr)
}
}
}
main(){
BST b
b.insert(10)
b.insert(20)
b.Preorder(b.root)
}
Your code here:
#include<iostream>
using namespace std;
class Node{
public:
int key;
Node *left;
Node *right;
Node(int key){
this->key=key;
left=right=NULL;
}
};
Node* insert(Node*root,int key)
{
if(root==NULL)
return new Node(key);
if(key<root->key){
root->left=insert(root->left,key);
}else{
root->right=insert(root->right,key);
}
return root;
}
void printInOrder(Node *root){
if (root==NULL)
return;
printInOrder(root->left);
cout<<root->key<<",";
printInOrder(root->right);
}
void printPreOrder(Node *root){
if (root==NULL)
return;
cout<<root->key<<",";
printPreOrder(root->left);
printPreOrder(root->right);
}
void printPostOrder(Node *root){
if (root==NULL)
return;
printPostOrder(root->left);
printPostOrder(root->right);
cout<<root->key<<",";
}
bool search(Node*root,int key){
if(root==NULL){
return false;
}
if(root->key==key){
return true;
}
if(key<root->key){
return search(root->left,key);
}
return search(root->right,key);
}
Node* findMid(Node* root){
while(root->left!=NULL){
root=root->left;
}
return root;
}
Node* remove(Node* root,int key){
if(root==NULL){
return NULL;
}
else if(key<root->key){
root->left=remove(root->left,key);
}else if(key>root->key){
root->right=remove(root->right,key);
}else{
if(root->left==NULL and root->right==NULL){
delete root;
root=NULL;
}
else if(root->left==NULL){
Node*temp=root;
root=root->right;
delete temp;
}else if(root->right==NULL){
Node*temp=root;
root=root->left;
delete temp;
}
else{
Node * temp=findMid(root->right);
root->key=temp->key;
root->right=remove(root->right,temp->key);
}
return root;
}
}
int main()
{
Node* root=NULL;
int arr[]={10,20,30,40,50,60,70,80,90};
for(int x:arr)
root=insert(root,x);
cout<<"InOrder :"<<endl;
printInOrder(root);
cout<<"\nPreOrder :"<<endl;
printPreOrder(root);
cout<<"\nPostOrder :"<<endl;
printPostOrder(root);
int key=70;
if(search(root,key))
cout<<"\nFound"<<endl;
else
cout<<"Not Found"<<endl;
root=remove(root,key);
cout<<"after deletion : "<<endl;
printInOrder(root);
cout<<endl;
return 0;
}
Your whole Screenshot here: (Console Output):
2. Implement a program using BST to store information of students with the following details such as id,
name and CGPA. You can use unique id of a student to search and insert different data.
Note: Each node will contain three information: id, name and CGPA but id will be used for searching data in
the BST
Your code here:
#include<iostream>
using namespace std;
struct BstNode{
string id;
string name;
double cgpa;
BstNode *left;
BstNode *right;
};
BstNode *root;
BstNode *GetNewNode(string id,
string name,
float cgpa){
BstNode *NewNode = new BstNode();
NewNode->id = id;
NewNode->name=name;
NewNode->cgpa=cgpa;
NewNode->left = NULL;
NewNode->right = NULL;
return NewNode;
}
BstNode *Insert(BstNode *root,
string id,
string name,
float cgpa){
if(root==NULL){
root = GetNewNode(id, name, cgpa);
cout << "Succesfully Inserted!" << "\n";
}
else if(id <= root->id){
root->left = Insert(root->left, id, name, cgpa);
}
else{
root->right = Insert(root->right, id, name, cgpa);
}
return root;
}
void inorderTraversal(struct BstNode* root) {
if(root == NULL){
return;
}
inorderTraversal(root->left);
cout << "ID: " << root->id << "\n";
cout << "NAME: "<< root->name << "\n";
cout << "CGPA: " << root->cgpa << "\n\n";
inorderTraversal(root->right);
}
bool Search(BstNode *root,
string id){
if(root==NULL){
cout << "No student details" << "\n";
return false;
}
else if(root->id == id){
return true;
}
else if(id <= root->id){
return Search(root->left, id);
}
else{
return Search(root->right, id);
}
}
int main()
{
cout << "\n";
root = NULL;
root = Insert(root, "1", "fahmida", 3.45);
root = Insert(root, "3", "Nasim", 3.52);
root = Insert(root, "6", "turna", 3.43);
cout << "\n";
cout << "Student Deatils" << "\n";
cout << "--------------------------------------------" << "\n";
inorderTraversal(root);
cout << "\n";
cout << "Enter search id: ";
string s;
cin >> s;
cout << "\n";
if(Search(root, s) == true){
cout << "Student id found!" << "\n";
}
else{
cout << "Student id not found" << "\n";
}
return 0;
}
Your whole Screenshot here: (Console Output):