0% found this document useful (0 votes)
27 views16 pages

Syed Mohamid Raza Bcs221098

The document defines a binary search tree (BST) class with methods to insert nodes, perform traversals like inorder, preorder and postorder, search for a node, find the number of nodes, find successor and predecessor of a node, delete a node, and find the lowest common ancestor (LCA) of two nodes. It includes a main function that tests the BST class by inserting nodes, finding the LCA of different node pairs, and printing outputs.

Uploaded by

Ak with you
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views16 pages

Syed Mohamid Raza Bcs221098

The document defines a binary search tree (BST) class with methods to insert nodes, perform traversals like inorder, preorder and postorder, search for a node, find the number of nodes, find successor and predecessor of a node, delete a node, and find the lowest common ancestor (LCA) of two nodes. It includes a main function that tests the BST class by inserting nodes, finding the LCA of different node pairs, and printing outputs.

Uploaded by

Ak with you
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Name: Syed Mohamid Raza Nadvi

Reg no: Bcs221098


DS LAB
Question 1:
#include <iostream>

using namespace std;

class node
{
public:

int data;
node* left;
node* right;

node(int x)

{
data = x;
left = NULL;
right = NULL;

}
};

class BST
{
public:

node* root;

BST()
{
root = NULL;
}

node* insertrec(int x, node* r)


{
if (r == NULL)
{

node* newNode = new node(x);


return newNode;
}

if (r->data > x)
{

r->left = insertrec(x, r->left);


}
else if (r->data < x)
{

r->right = insertrec(x, r->right);


}
return r;
}

void insert(int val)


{
root = insertrec(val, root);
}
void in_order(node* r)
{
if (r == NULL)

{
return;
}
in_order(r->left);

cout << r->data << " ";


in_order(r->right);
}

void pre_order(node* r)
{

if (r == NULL)
{
return;
}

cout << r->data << " ";


pre_order(r->left);
pre_order(r->right);
}

void post_order(node* r)
{
if (r == NULL)
{
return;
}
pre_order(r->left);

pre_order(r->right);
cout << r->data << " ";
}

void search(node* r, int val)

{
if (r == NULL)
{
cout << "Not Found" << endl;

return;
}
else if (val == r->data)
{
cout << r->data << " Value Found" << endl;

}
else if (val < r->data)
{
search(r->left, val);

}
else
{
search(r->right, val);

}
}
int count_nodes(node* r)
{
static int count = 0;

if (r == NULL)
{
return 0;
}

else
{
count++;
count_nodes(r->left);

count_nodes(r->right);
}
return count;
}

void successor(int val, node* r)

{
node* successor = NULL;

while (r != NULL)
{

if (r->data <= val)


{
r = r->right;
}
else
{
successor = r;

r = r->left;
}
}

int x;

if (successor == NULL)
{
x = 0;
}

else
{
x = successor->data;
}

cout << "Successor: " << x << endl;

void predecessor(int val, node* r)


{
node* predecessor = NULL;

while (r != NULL)
{
if (r->data >= val)
{
r = r->left;
}

else
{
predecessor = r;
r = r->right;

}
}

int x;
if (predecessor == NULL)

{
x = 0;
}
else
{

x = predecessor->data;
}

cout << "Predecessor: " << x << endl;


}

node* deleterec(node* r, int val)


{
if (r == NULL)
{
return r;
}
if (val < r->data)

{
r->left = deleterec(r->left, val);
}
else if (val > r->data)

{
r->right = deleterec(r->right, val);
}
else

{
if (r->left == NULL)
{
node* temp = r->right;
delete r;

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

node* temp = findMinValueNode(r->right);


r->data = temp->data;
r->right = deleterec(r->right, temp->data);
}
return r;

void deleteNode(int val)


{
root = deleterec(root, val);

private:
node* findMinValueNode(node* r)
{
node* current = r;

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


{
current = current->left;
}

return current;
}
};

int main()

{
BST b1;
b1.insert(2);
b1.insert(7);
b1.insert(3);
b1.insert(1);
b1.insert(15);

cout << "Inorder: ";


b1.in_order(b1.root);
cout << endl;

cout << "Preorder: ";

b1.pre_order(b1.root);
cout << endl;

cout << "Postorder: ";


b1.post_order(b1.root);
cout << endl;

b1.search(b1.root, 10);
b1.search(b1.root, 2);

cout << "No of Nodes: " << b1.count_nodes(b1.root) << endl;

b1.successor(1, b1.root);
b1.predecessor(7, b1.root);

b1.deleteNode(3);
cout << "Inorder after deleting 3: ";
b1.in_order(b1.root);
cout << endl;

return 0;
}

Output:

Question :2
#include <iostream>
using namespace std;

class node
{
public:
int data;
node* left;
node* right;
node(int x)
{
data = x;
left = NULL;
right = NULL;
}

private:

};

class BST
{
public:
node* root;

BST()
{
root = NULL;
}
node* insertrec(int x,node *r)
{

if (r==NULL)
{
node* newNode = new node(x);
return newNode;
}

if (r->data > x)
{
r->left = insertrec(x, r->left);
}
else if (r->data < x)
{
r->right = insertrec(x, r->right);
}
return r;
}
void insert(int val)
{
root=insertrec(val, root);
}
node* findLowestCommonAncestor(node* root, int x, int y) {
while (root != NULL) {
if (root->data > x && root->data > y) {
root = root->left;
}
else if (root->data < x && root->data < y) {
root = root->right;
}
else {
return root;
}
}
return NULL;
}

private:
};

int main()
{
BST b1;
b1.insert(2);

b1.insert(15);
b1.insert(10);
b1.insert(25);
b1.insert(15);
b1.insert(30);
b1.insert(20);
b1.insert(8);
b1.insert(12);
b1.insert(22);
b1.insert(18);
b1.insert(9);
b1.insert(6);

node* lowest = b1.findLowestCommonAncestor(b1.root, 12, 6);


if (lowest != NULL)
{
cout << "Lowest Common Ancestor: " << lowest->data << endl;
}
else
{
cout << "Lowest Common Ancestor not found." << endl;
}
lowest = b1.findLowestCommonAncestor(b1.root, 20, 6);
if (lowest != NULL)
{
cout << "Lowest Common Ancestor: " << lowest->data << endl;
}
else
{
cout << "Lowest Common Ancestor not found." << endl;
}
return 0;
}
Output:

You might also like