-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
209 lines (171 loc) · 5.29 KB
/
Copy pathbst.cpp
File metadata and controls
209 lines (171 loc) · 5.29 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
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
//recursive definition
template<typename T> class BinarySearchTree {
public:
//current node has references to: root, left child, right child, parent
BinarySearchTree<T> *root, *left_son, *right_son, *parent;
//current node info
T *pinfo;
//constructor
BinarySearchTree() {
left_son = right_son = NULL;
root = this;
pinfo = NULL;
}
void setInfo(T info) {
pinfo = new T;
*pinfo = info;
}
void insert(T x) {
if (pinfo == NULL)
setInfo(x);
else
insert_rec(x);
}
///insert function depends on the value that we want to insert
///if it should be on the left / right subtree
void insert_rec(T x) {
int next_son;
if (x <= (*pinfo))
next_son = 0;
else
next_son = 1;
if (next_son == 0)
{
if (left_son == NULL)
{ ///left son
left_son = new BinarySearchTree<T>;
left_son->pinfo = new T;
*(left_son->pinfo) = x;
left_son->left_son = left_son->right_son = NULL;
left_son->parent = this;
left_son->root = root;
}
else
left_son->insert_rec(x);
}
else
{ /// right son
if (right_son == NULL)
{
right_son = new BinarySearchTree<T>;
right_son->pinfo = new T;
*(right_son->pinfo) = x;
right_son->left_son = right_son->right_son = NULL;
right_son->parent = this;
right_son->root = root;
}
else
right_son->insert_rec(x);
}
}
BinarySearchTree<T>* find(T x) {
BinarySearchTree<T> *rez;
if (pinfo == NULL)
return NULL;
if ((*pinfo) == x)
return this;
if (x <= (*pinfo))
{
if (left_son != NULL)
return left_son->find(x);
else
return NULL;
}
else
{
if (right_son != NULL)
return right_son->find(x);
else
return NULL;
}
}
void removeInfo(T x) {
BinarySearchTree<T> *t = find(x);
if (t != NULL)
t->remove();
}
void remove() {
BinarySearchTree<T> *p;
T *paux;
if (left_son == NULL && right_son == NULL)
{
if (parent == NULL)
{ /// this == root
delete this->pinfo;
root->pinfo = NULL;
}
else ///leaf
{
if (parent->left_son == this)
parent->left_son = NULL;
else
parent->right_son = NULL;
delete this->pinfo;
delete this;
}
}
else
{
if (left_son != NULL)
{
p = left_son;
while (p->right_son != NULL)
p = p->right_son;
}
else
{ /// right_son != NULL
p = right_son;
while (p->left_son != NULL)
p = p->left_son;
}
paux = p->pinfo;
p->pinfo = this->pinfo;
this->pinfo = paux;
p->remove();
}
}
void inOrderTraversal() {
if (left_son != NULL)
left_son->inOrderTraversal();
cout<<*pinfo<<" ";
if (right_son != NULL)
right_son->inOrderTraversal();
}
void preOrderTraversal(){
cout<<*pinfo<<" ";
if (left_son != NULL)
left_son->preOrderTraversal();
if (right_son != NULL)
right_son->preOrderTraversal();
}
void postOrderTraversal(){
if (left_son != NULL){
left_son->postOrderTraversal();}
if (right_son != NULL){
right_son->postOrderTraversal();}
cout<<*pinfo<<" ";
}
};
int main(){
BinarySearchTree<char> *r = new BinarySearchTree<char>();
r->insert('G');
r->insert('B');
r->insert('A');
r->insert('C');
r->insert('D');
r->insert('F');
r->insert('E');
cout<<"In order: ";
r->inOrderTraversal();
cout<<endl;
cout<<"Pre order: ";
r->preOrderTraversal();
cout<<endl;
cout<<"Post order: ";
r->postOrderTraversal();
cout<<endl;
}