-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary_search_tree.cpp
More file actions
219 lines (200 loc) · 5.69 KB
/
binary_search_tree.cpp
File metadata and controls
219 lines (200 loc) · 5.69 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#define DEBUG
#include <bits/stdc++.h>
using namespace std;
class BiTree;
class Node
{
friend class BiTree;
private :
Node *left;
int data;
int leftsize;
Node *right;
public :
Node (int t, Node *L = nullptr, Node *R = nullptr)
{
data = t;
left = L;
right = R;
leftsize = 0;
}
};
class BiTree
{
private :
Node *root;
void private_inorder (Node *temp_root)
{
if (temp_root) {
private_inorder(temp_root->left);
cout << temp_root->data << " ";
private_inorder(temp_root->right);
}
}
// the main point is
// i will insert the node under that node
Node *_insert(Node* that, int data)
{
if (that == nullptr) return new Node(data);
if (data < that->data) that->left = _insert(that->left, data);
else if (that->data < data) that->right = _insert(that->right, data);
return that;
}
int findMax(Node *subroot)
{
// left the max value of the given subroot tree
Node *temp = subroot;
while (temp->right) temp = temp->right;
return temp->data;
}
public:
BiTree ()
{
root = nullptr;
}
// recursive
void inorder_recursive();
// stack
void inorder_stack();
void level_order() ;
// binary tree insert
// using recursive
void insert(int data)
{
root = _insert(root, data) ;
}
bool search (int data)
{
Node *travel = root;
while (travel) {
if (data < travel->data) travel = travel->left;
else if (travel->data < data) travel = travel->right;
else return true;
}
return false;
}
// IMPORTANT!! if same value go to left
void remove (Node *r, Node *parent, int target_value)
{
if (r == nullptr) return;
if (target_value == r->data) {
// leaf
if (!r->left && !r->right) {
// if root
if (r == root) {root = nullptr; return;};
// parent link
if (target_value <= parent->data) parent->left = nullptr;
else parent->right = nullptr;
//delete r;
}
// degree one
else if (r->right == nullptr) {
// if root
if (r == root) {root = root->left; return;};
// only left
// why use <=
// = is rule for same to left tree !!
if (target_value <= parent->data) parent->left = r->left;
else parent->right = r->left;
// delete r;
}
else if (r->left == nullptr) {
// if root
if (r == root) {root = root->right; return;};
// only right
if (target_value <= parent->data) parent->left = r->right;
else parent->right = r->right;
// delete r;
}
// degree two
else {
// find the max in left subtree
int d = findMax(r->left);
// copy to the r
r->data = d;
// delete that leaf node
remove(r->left, r, d);
}
}
else if (target_value < r->data) {
remove(r->left, r, target_value);
}
else remove(r->right, r, target_value);
}
void remove_er (int target_value) {
if (!search(target_value)) return ;
remove (root, root, target_value);
}
void print_root() {
if (root) cout << "root is : " << root->data << endl;
else cout << "is empty!!\n";
}
};
int main ()
{
#ifdef DEBUG
freopen ("in.in", "r", stdin);
#endif
BiTree t;
t.insert(1);
t.insert(0);
t.insert(8);
t.insert(5);
t.insert(9);
t.insert(3);
t.insert(4);
cout << "recursive inorder\n";
t.inorder_recursive();
cout << endl;
cout << boolalpha;
cout << "search for 9: " << t.search(9);
cout << endl;
cout << "search for 0: " << t.search(0);
cout << endl;
// delete
cout << "test for delete\n";
t.remove_er(1);
t.inorder_recursive();
cout << endl;
t.print_root();
cout << endl;
return 0;
}
void BiTree::inorder_recursive()
{
private_inorder(root);
}
// stack
void BiTree::inorder_stack()
{
// left -> root -> right
// same as recursive method
stack<Node*> s;
Node *temp_root = root;
while (1) {
while (temp_root) { // go to left left node
s.push(temp_root);
temp_root = temp_root->left;
}
if (s.empty()) return ;
temp_root = s.top();
s.pop();
cout<< temp_root->data << " " ;
// think for the last 2 level node who have left nullptr and right node
temp_root = temp_root->right;
}
}
void BiTree::level_order()
{
queue<Node*> q;
q.push(root);
while (!q.empty()) {
auto temp_top = q.front();
q.pop();
// visit !
cout << temp_top->data << " ";
//expand
if (temp_top->left) q.push(temp_top->left);
if (temp_top->right) q.push(temp_top->right);
}
}