Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

K S RANGASAMY COLLEGE OF

TECHNOLOGY,TIRUCHENGODE.
MINIPROJECT IN DATA STRUCTURES.
TOPIC:BINARY SEARCH TREE.

PRESENTED BY,
1.ABOORVA.S-1914102
2.ABSARA.V-1914103
3.NILANCHANA.T-1914143
CLASS : II ND YEAR CSE-A.
DATE : 13/11/2020.
BINARY SEARCH TREE FOR MOBILE PRICES
AIM:
To generate a code for a mobile shop to their
variety of mobile prices and also deleting a price from
the mobile price using BINARY SEARCH TREE.
PROBLEM DESCRIPTION :
a. To descript the variety of smart phones with
same features at varied prices.
b. To insert the mobile prices using Binary
search tree concept.
c. One of the Smart phone has been sold out. So
, the price of sold smart phone should be deleted
from the Binary search tree.
MODULE:
1. Creating the root node with the prices of a smart phones.
2. Insert the prices of the smart phones one by one.
3. Delete the prices of the smart phones after they sold out.

ALGORITHM:
STEP 1:
Initially we want to create a root node, so we insert the first
prices of the smart phones and consider it as a root node.
STEP 2:
After inserting the first price of the smart phones we
need to insert the other prices of the smart phones one by
one by using binary search tree concept.
STEP 3:
If one the smart phone has been sold we need to
delete the price of the smart phone by using deletion
concept in binary search tree.
FLOWCHART:
CREATING A ROOT NODE:

16,000

16,000
INSERT 5,000:

5,000
INSERT 8,000:

16,000

5,000

8,000
INSERT 7,500 AND 8,300:
16,000

5,000

8,000

8,300
7,500
INSERT6,600 AND 9,080 AND 7,777:
16,000

5,000

8,000

7,500 8,300

6,600 7,777 9,080


NOW ONE OF THE HAS BEEN SOLD OUT SO I NEED TO DELETE THE
PRICE OF THE RESPECTIVE MOBILE.
DELETE 5,000:
16,000

8,000

7,500 8,300

6,600 9,080
7,777
CODING:
#include <iostream>
using namespace std;
class BST
{
int data;
BST *left, *right;
public:
// Default constructor.
BST();
// Parameterized constructor.
BST(int);
// Insert function.
BST* Insert(BST*, int);
// Inorder traversal.
void Inorder(BST*);
};
// Default Constructor definition.
BST ::BST()
: data(0)
, left(NULL)
, right(NULL)
{
}
// Parameterized Constructor definition.
BST ::BST(int value)
{
data = value;
left = right = NULL;
}
// Insert function definition.
BST* BST ::Insert(BST* root, int value){
if (!root)
{
// Insert the first node, if root is NULL.
return new BST(value);
}
// Insert data.
if (value > root->data)
{
// Insert right node data, if the 'value’ // to be inserted is greater than 'root' node data.
// Process right nodes. // root->right = Insert(root->right, value);
}
else
{
// Insert left node data, if the 'value’ // to be inserted is greater than 'root' node data. // Process left
nodes.
root->left = Insert(root->left, value);
// Return 'root' node, after insertion.
return root;
}
// Inorder traversal function.// This gives data in sorted order.
void BST ::Inorder(BST* root)
{
if (!root)
{
return;
}
Inorder(root->left);
cout << root->data << endl;
Inorder(root->right);
}
// Driver code
int main()
{
BST b, *root = NULL;
root = b.Insert(root, 16000);
b.Insert(root, 5000);
b.Insert(root, 8000);
b.Insert(root, 7500);
b.Insert(root, 8300);
b.Insert(root, 6600);
b.Insert(root, 9080);
b.Insert(root, 7777 );
b.Inorder(root);
return 0;
}
SCREENSHOT OF THE OUTPUT
NOW ONE OF THE HAS BEEN SOLD OUT SO I NEED TO DELETE THE
PRICE OF THE RESPECTIVE MOBILE.
CODING:
#include <bits/stdc++.h>
using namespace std;
struct node {
int key;
struct node *left, *right;
};
// A utility function to create a new BST node
struct node* newNode(int item){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
/* return the (unchanged) node pointer */
return node;
}
// A utility function to do // inorder traversal of BST
void inorder(struct node* root)
{
if (root != NULL) {
inorder(root->left);
cout << root->key;
inorder(root->right);
}}
/* A utility function to insert a new node with given key in * BST */
struct node* insert(struct node* node, int key)
{ /* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
/* Given a non-empty binary search tree, return the nodewith minimum key value found in that
tree. Note that theentire tree does not need to be searched. */
struct node* minValueNode(struct node* node)
{
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;
return current;
 }
/* Given a binary search tree and a key, this functiondeletesthe key and returns the new root */
struct node* deleteNode(struct node* root, int key)
{
// base case
if (root == NULL)
return root;
// If the key to be deleted is // smaller than the root's // key, then it lies in left subtree
if (key < root->key)
root->left = deleteNode(root->left, key);
// If the key to be deleted is // greater than the root's // key, then it lies in right subtree
else if (key > root->key)
root->right = deleteNode(root->right, key);
// if key is same as root's key, then This is the node // to be deleted
else {
// node with only one child or no child
if (root->left == NULL) { struct node* temp = root->right;
free(root);
return temp;
else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
// node with two children: Get the inorder successor
// (smallest in the right subtree)
struct node* temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->key = temp->
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
// Driver Code
int main()
{ /* Let us create following BST
16000 /
5000 /
8000 / \
7500 8300/ \ \
6,600 7,777 8,300/ 9,080 */

struct node* root = NULL;


root = insert(root,16000);
root = insert(root, 5000); root = insert(root, 8000);root = insert(root, 7500); root =
insert(root, 8300);
root = insert(root, 6600); root = insert(root, 9080); root = insert(root, 7777);
cout << "Inorder traversal of the given tree \n";
inorder(root);
cout << "\nDelete 5000\n;
root = deleteNode(root, 5000);
cout << "Inorder traversal of the modified tree \n";
inorder(root);
return 0;}

SCREENSHOT OF THE OUTPUT:


RESULT:

Thus the prices of the varied mobile phones had


been generated by using BINARY SEARCH TREE
and also deleted while the mobile is sold out.

You might also like