Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Tree/BinarySearchTreePrakash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* left;
struct Node* right;
};
struct Node* Insert(struct Node* root,int data)
{
if(root == NULL)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
(*temp).data = data;
(*temp).left = NULL;
(*temp).right = NULL;
root = temp;
}
else if(data <= (*root).data)
{
(*root).left = Insert((*root).left,data);
}
else if(data >= (*root).data)
{
(*root).right = Insert((*root).right,data);
}
return root;
}
bool search(struct Node* root,int data)
{
if(root == NULL)
return false;
else if((*root).data == data)
return true;
else if(data <= (*root).data)
return search((*root).left,data);
else
return search((*root).right,data);
}
int main()
{
struct Node* root = NULL;
root = Insert(root,15);
root = Insert(root,10);
root = Insert(root,20);
root = Insert(root,25);
root = Insert(root,8);
root = Insert(root,12);
int x;
printf("Enter no. to search\n");
scanf("%d",&x);
if(search(root,x))
printf("found\n");
else
printf("not found\n");
return 0;
}