-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.cpp
193 lines (164 loc) · 5.27 KB
/
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
191
192
193
#include "BST.h"
#include <iostream>
#include <queue>
using namespace std;
BST::BST():root(nullptr) {}
inline bool BST::empty() const {
return root == nullptr;
}
bool BST::searchAux(BST::NodePointer subtree, const DataType &item) const {
if (subtree == nullptr)
return false; //empty tree
if (item < subtree->data)
return searchAux(subtree->left,
item);//searches recursively in the left subtree if the item is less than the root
else if (item > subtree->data)
return searchAux(subtree->right,
item);//searches recursively in the right subtree if the item is greater than the root
else
return true;//item is found
}
bool BST::search(const DataType &item) const {
return searchAux(root, item);
}
void BST::insertAux(BST::NodePointer &subtree, const DataType &item) {
if (subtree == nullptr)
subtree = new BST::BSTNode(item);
else if (item < subtree->data)
insertAux(subtree->left, item);
else if (item > subtree->data)
insertAux(subtree->right, item);
else
cerr << "The item is already in the tree \n" << endl;//no duplication
}
void BST::insert(const DataType &item) {
insertAux(root, item);
}
void BST::search2(const DataType &item,
bool &found, BST::NodePointer &locptr,
BST::NodePointer &parent) const {
found = false;
locptr = root;//locptr is the node i want to delete later
parent = nullptr;
while (!found && locptr != nullptr) {
if (item < locptr->data) {
parent = locptr;
locptr = locptr->left;
} else if (item > locptr->data) {
parent = locptr;
locptr = locptr->right;
} else
found = true;
}
}
void BST::remove(const DataType &item) {
bool found;
BST::NodePointer x, parent;
search2(item, found, x, parent);//searches for the item existence in the binary tree
if (!found) {
cerr << "The item you want to delete isn't in the tree\n" << endl;
exit(1);
}
// item is in the tree
if (x->left != 0 && x->right != 0) {//Node has two children
/*
In case the node has two children:
we take the value of our successor and delete the successor
successor is the leftmost node of our right subtree
*/
BST::NodePointer xsuccesor = x->right;
parent = x;
while (xsuccesor->left !=
NULL)//the xsuccesor searches to the left of the tree till the left of the tree equals null
{
parent = xsuccesor;//at each iteration the parent takes the xsuccesor place and the xsuccesor moves to the left
xsuccesor = xsuccesor->left;
}
x->data = xsuccesor->data;//Move the data of leftmost child of the rightchild of parent to node to be deleted
x = xsuccesor;//now the x points at the location of the xsuccesor and next we check on the cases of the x which will have only 0 or 1 nodes
}
/*after the while loop ends we garantee that the child has only 0 or 1 child
case node has one or zero child
*/
BST::NodePointer subtree = x->left;
if (subtree == 0)//the child is in the right
subtree = x->right;
if (parent == 0)//we want to delete the root node
root = subtree;
else if (parent->left == x)//the left of the parent is the node i want to delete
parent->left = subtree;//make the parent's next points at the subtree which is either the right or the left node of the node i want to delete
else
parent->right = subtree;
delete x;
}
void BST::InOrderAux(BST::NodePointer root, vector<DataType> &v) {
//traverses in order of left->root->right
if (root == nullptr)
return;
InOrderAux(root->left, v);
v.push_back(root->data);
InOrderAux(root->right, v);
}
void BST::PostOrderAux(BST::NodePointer root) {
//traverses in order of left->right->node
if (root == nullptr)
return;
PostOrderAux(root->left);
PostOrderAux(root->right);
cout << root->data << "\t";
}
void BST::PreOrderAux(BST::NodePointer root) {//traverses the tree in order of root->left->right
if (root == nullptr)
return;
cout << root->data << "\t";
PreOrderAux(root->left);
PreOrderAux(root->right);
}
void BST::InOrder(vector<DataType> &v) {
InOrderAux(root, v);
}
void BST::clearAux(NodePointer &root) {
if (root == nullptr)
return;
if (root->left)
clearAux(root->left);
if (root->right)
clearAux(root->right);
delete root;
root = nullptr;
}
void BST::clear() {
clearAux(root);
}
BST::~BST() {
clear();
}
void BST::PreOrder() {
PreOrderAux(root);
}
void BST::PostOrder() {
PostOrderAux(root);
}
void BST::levelOrderPrint() {
if (empty()) {
cout << "empty tree";
return;
}
queue<NodePointer> q;
NodePointer cur = root;
q.push(root);
while (!q.empty()) {
int size = (int) q.size();
while (size--) {
cur = q.front();
q.pop();
cout << cur->data << ' ';
if (cur->left != nullptr)
q.push(cur->left);
if (cur->right != nullptr)
q.push(cur->right);
}
cout << '\n';
}
cout << '\n';
}