0% found this document useful (0 votes)
64 views7 pages

Dsa Lab 12 064

The document contains C++ code for implementing binary search trees (BSTs). It defines Node and BT classes for creating and traversing a BST. The BT class contains methods for inserting nodes, traversing the tree using inorder, preorder and postorder traversal, and deleting a node. The main function tests the BST implementation by inserting nodes, traversing the tree, and deleting a node.

Uploaded by

Warisha Malik
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)
64 views7 pages

Dsa Lab 12 064

The document contains C++ code for implementing binary search trees (BSTs). It defines Node and BT classes for creating and traversing a BST. The BT class contains methods for inserting nodes, traversing the tree using inorder, preorder and postorder traversal, and deleting a node. The main function tests the BST implementation by inserting nodes, traversing the tree, and deleting a node.

Uploaded by

Warisha Malik
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/ 7

DATA STRUCTURES AND

ALGORITHMS
LAB NO 12

TASK 1

INPUT

#include<iostream>
using namespace std;
class node{
public:
int data;
node* left = NULL;
node* right = NULL;
};
class bt
{ public:

node* Insert(node* root, int value)


{
if(root == NULL)
{
root = new node;
root->data = value;
return root;
}

// Insert data.
if(value > root->data)
{
root->right = Insert(root->right, value);
}
else
{
root->left = Insert(root->left, value);
}

return root;
}

void Inorder(node* root)


{
if(!root) {
return;
}
Inorder(root->left);
cout << root->data <<" ";
Inorder(root->right);
}

void preorder(node* root)


{
if (root == NULL) {
return ;
}
cout << root->data <<" ";
preorder(root->left);

preorder(root->right);
}

void postorder(node* root)


{
if (root == NULL) {
return ;
}
postorder(root->left);

postorder(root->right);
cout << root->data<<" ";
}

};
// Driver code
int main()
{
node *root = NULL;
bt b;
int num,value;
cout<<"\n Enter the number of nodes: ";
cin>>num;
cout<<"Enter the value of nodes : ";
cin>>value;
root = b.Insert(root,value);
for(int i =0;i<num-1;i++){
cout<<"\n Enter the value of nodes : ";
cin>>value;
b.Insert(root,value);
}
cout<<"\n Display by the preorder : ";
b.preorder(root);
cout<<"\n Display by the inorder : ";
b.Inorder(root);
cout<<endl;
cout<<"\n Displaying by the post order: ";
b.postorder(root);

return 0;
}

OUTPUT

TASK 2

INPUT

#include<iostream>
using namespace std;
class node{
public:
int data;
node* left = NULL;
node* right = NULL;
};
class bt
{ public:

node* Insert(node* root, int value)


{
if(root == NULL)
{
root = new node;
root->data = value;
return root;
}

// Insert data.
if(value > root->data)
{
root->right = Insert(root->right, value);
}
else
{
root->left = Insert(root->left, value);
}

return root;
}

void Inorder(node* root)


{
if(!root) {
return;
}
Inorder(root->left);
cout << root->data <<" ";
Inorder(root->right);
}

void preorder(node* root)


{
if (root == NULL) {
return ;
}
cout << root->data <<" ";
preorder(root->left);

preorder(root->right);
}

void postorder(node* root)


{
if (root == NULL) {
return ;
}
postorder(root->left);
postorder(root->right);
cout << root->data<<" ";
}
node* minValueNode(node* BT)
{
node* current = BT;

/* loop down to find the leftmost leaf */


while (current && current->left != NULL)
current = current->left;

return current;
}
node* deleteNode(node* root, int key)
{
// base case
if (root == NULL)
return root;

if (key < root->data)


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

else if (key > root->data)


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

else {
// node has no child
if (root->left == NULL and root->right == NULL)
{
delete root;
return NULL;
}

// node with only one child or no child


else if (root->left == NULL) {
node* temp = root->right;
delete root;
return temp;
}
else if (root->right == NULL) {
node* temp = root->left;
delete root;
return temp;
}

node* temp = minValueNode(root->right);


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

};
int main()
{
node *root = NULL;
bt b;
int num,value;
cout<<"\n Enter the number of nodes: ";
cin>>num;
cout<<"Enter the value of nodes : ";
cin>>value;
root = b.Insert(root,value);
for(int i =0;i<num-1;i++){
cout<<"\n Enter the value of nodes : ";
cin>>value;
b.Insert(root,value);
}
cout<<"\n Display by the preorder : ";
b.preorder(root);
cout<<"\n Display by the inorder : ";
b.Inorder(root);
cout<<endl;
cout<<"\n Displaying by the post order: ";
b.postorder(root);
cout<<"\n Enter number you want to delete : ";
int del;
cin>>del;
b.deleteNode(root,del);
cout<<"\n Displaying data after deletion : ";
b.preorder(root);
return 0;
}

OUTPUT

You might also like