Skip to content

Commit

Permalink
Merge pull request #642 from ShreyasiDebnath/main
Browse files Browse the repository at this point in the history
Create InsertionInaBST.cpp
  • Loading branch information
gantavyamalviya authored Oct 5, 2022
2 parents de8e0b9 + 516287b commit b514f70
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions CPP/BST/InsertionInABST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <iostream>
#include <cstdlib>
using namespace std;

struct node {
int data;
struct node *left, *right;
};

// Create a node
struct node *createnode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
int search(struct node *root, int key){
struct node *prev = NULL;
while(root!=NULL){
prev = root;
if(key==root->data){
printf("Cannot insert %d, already in BST", key);
return 1;
}
else if(key<root->data){
root = root->left;
}
else{
root = root->right;
}
}
return 0;
}
struct node *insert(struct node *node, int key) {
if(search(node,key)==0){

if (node == NULL){
return createnode(key);
}
if (key < node->data){
node->left = insert(node->left, key);
}
else{
node->right = insert(node->right, key);
}
}

return node;
}


int main() {
struct node *root = NULL;
int ch;
int x;
cout<<"Press 0 to exit else enter 1: ";
cin>>ch;
while(ch!=0)
{
cout<<"\n"<<"1.Insertion"<<"\n"<<"2.View"<<"\n";
cout<<"\n"<<"Enter choice :";
cin>>ch;
switch (ch)
{
case 1:
cout<<"Enter the value you want to insert in binary search tree: ";
cin>>x;
root = insert(root, x);
break;
case 2:
cout << "Inorder traversal: ";
inorder(root);
break;

default:
printf("\nINVALID CHOICE !!");
break;
}
}
return 0;


}

0 comments on commit b514f70

Please sign in to comment.