-
Notifications
You must be signed in to change notification settings - Fork 1
/
SEARCH_&_DELETION_IN_BST.cpp.cpp
94 lines (93 loc) · 1.45 KB
/
SEARCH_&_DELETION_IN_BST.cpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using namespace std;
#include<iostream>
struct node
{
int data;
struct node *left;
struct node *right;
}*root=NULL;
struct node *insertnode(struct node *root,struct node *t)
{
if(root==NULL)
{
root=new node;
root=t;
}
else if(root->data>t->data)
{
if(root->left==NULL)
{
root->left=t;
t->left=NULL;
t->right=NULL;
}
else
{
insertnode(root->left,t);
}
}
else if(root->data<t->data)
{
if(root->right==NULL)
{
root->right=t;
t->left=NULL;
t->right=NULL;
}
else
{
insertnode(root->right,t);
}
}
return root;
}
struct node *k=NULL;
void search(struct node *node1,int k)
{
if (node1->data == k)
cout<<"element found";
else if(k<node1->data&&node1->left!=NULL)
search(node1->left,k);
else if(k>node1->data&&node1->right!=NULL)
search(node1->right,k);
else
cout<<"elemnt not found";
}
void deletion(struct node* node1)
{
if(node1->left==NULL&&node1->right==NULL)
{
k->left=NULL;
}
else
{
k=node1;
deletion(node1->left);
}
}
void traverse(struct node* node1)
{
struct node*p=node1;
if (p == NULL)
return;
traverse(p->left);
cout << p->data << " ";
traverse(p->right);
}
int main()
{
struct node *p=NULL;
for(int i =0;i<6;i++)
{
struct node *temp=new struct node();
cin>>temp->data;
temp->left=NULL;
temp->right=NULL;
p=insertnode(p,temp);
}
int k;
cin>>k;
search(p,k);
deletion(p);
traverse(p);
}