-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtemplate_bst.cpp
190 lines (157 loc) · 3.93 KB
/
template_bst.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BST
{
BinaryTreeNode<int> *root;
private:
BinaryTreeNode<int> *insert(BinaryTreeNode<int> *node,int data)
{
if(node == NULL)
{
node = new BinaryTreeNode<int>(data);
return node;
}
if(node->data < data)
{
node->right = insert(node->right,data);
}
else if(node->data > data)
{
node->left = insert(node->left,data);
}
return node;
}
BinaryTreeNode<int> *deleteData(BinaryTreeNode<int> *node,int data)
{
if(node == NULL)
{
return NULL;
}
if(node->data == data)
{
if(node->left == NULL && node->right == NULL)
{
free(node);
return NULL;
}
else if(node->left == NULL && node->right != NULL)
{
BinaryTreeNode<int> *nextNode = node->right;
free(node);
return nextNode;
}
else if(node->left != NULL && node->right == NULL)
{
BinaryTreeNode<int> *nextNode = node->left;
free(node);
return nextNode;
}
else if(node->left != NULL && node->right != NULL)
{
BinaryTreeNode<int> * inOrdSuc = node->right;
while(inOrdSuc->left != NULL)
inOrdSuc = inOrdSuc->left;
node->data = inOrdSuc->data;
node->right = deleteData(node->right,inOrdSuc->data);
return node;
}
}
if(node->data < data)
node->right = deleteData(node->right,data);
else if(node->data > data)
node->left = deleteData(node->left,data);
return node;
}
bool hasData(int data,BinaryTreeNode<int> *root)
{
if(root == NULL)
return false;
if(root->data == data)
return true;
if(root->data < data)
{
return hasData(data,root->right);
}
else if(root->data > data)
{
return hasData(data,root->left);
}
}
void printTree(BinaryTreeNode<int> *node)
{
if(node == NULL)
return;
cout << node->data << ":";
if(node->left != NULL)
cout << "L:" << node->left->data << ",";
if(node->right != NULL)
cout << "R:" << node->right->data;
cout << endl;
printTree(node->left);
printTree(node->right);
}
public:
BST()
{
root = NULL;
}
void insert(int data)
{
root = insert(root,data);
}
void deleteData(int data)
{
root = deleteData(root,data);
}
bool hasData(int data)
{
return hasData(data,root);
}
void printTree()
{
printTree(root);
}
};
int main()
{
BST *tree = new BST();
int choice, input;
while(true){
cin>>choice;
switch(choice){
case 1:
cin >> input;
tree->insert(input);
break;
case 2:
cin >> input;
tree->deleteData(input);
break;
case 3:
cin >> input;
if(tree->hasData(input)) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
break;
default:
tree->printTree();
break;
}
}
return 0;
}