-
Notifications
You must be signed in to change notification settings - Fork 0
/
bstoperations.cpp
142 lines (126 loc) · 2.47 KB
/
bstoperations.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *left;
node *right;
node(int x)
{
data = x;
left = NULL;
right = NULL;
}
};
void preorder(node *root)
{
if(root==NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
node *insert(node* root,int x)
{
if(root == NULL)
root = new node(x);
node *temp = root;
while(temp!=NULL)
{
if(temp->data>x)
{
if(temp->left==NULL)
{
temp->left = new node(x);
break;
}
else
temp = temp->left;
}
else
{
if(temp->right==NULL)
{
temp->right = new node(x);
break;
}
else
temp = temp->right;
}
}
return root;
}
bool search(node* root,int x)
{
bool res;
node *temp1 = root;
while(temp1!=NULL)
{
if(temp1->data==x)
{
res = true;
break;
}
if(temp1->data>x)
temp1 =temp1->left;
else
temp1 = temp1->right;
}
if(temp1 == NULL)
res = false;
return res;
}
node* delete1(node* root, int key)
{
if (root == NULL)
return root;
if (key < root->data)
root->left = delete1(root->left, key);
else if (key > root->data)
root->right = delete1(root->right, key);
else
{
if(root->left == NULL && root->right == NULL)//node with 0 child
{
free(root);
return NULL;
}
else if(root->left == NULL && root->right != NULL) //node with one child
{
node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL && root->left != NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
// node with two children
node* temp = root->right;
while(temp->left!=NULL)
{
temp = temp->left;
}
root->data = temp->data;
root->right = delete1(root->right, temp->data);
}
return root;
}
int main()
{
node *head = new node(50);
head ->right = new node(70);
head->left = new node(30);
head->right->left = new node(60);
head->left->right = new node(40);
head = insert(head,80);
head = insert(head,65);
bool ans = search(head,50);
head = delete1(head,50);
cout<<ans<<endl;
preorder(head);
cout<<endl;
}