-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-search-tree.cpp
More file actions
196 lines (169 loc) · 4.77 KB
/
binary-search-tree.cpp
File metadata and controls
196 lines (169 loc) · 4.77 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
#include "binary-search-tree.h"
#include <iostream>
BinarySearchTree::Node::Node(DataType newval) : val(newval), left(nullptr), right(nullptr), avlBalance(0) {
}
int BinarySearchTree::getNodeDepth(Node* n) const {
if (n == nullptr) {
return -1;
}
int leftDepth = getNodeDepth(n->left);
int rightDepth = getNodeDepth(n->right);
return 1 + std::max(leftDepth, rightDepth);
}
BinarySearchTree::BinarySearchTree() : root_(nullptr), size_(0) {
}
BinarySearchTree::~BinarySearchTree() {
while (size_) {
remove(root_->val);
}
}
unsigned int BinarySearchTree::size() const {
return size_;
}
BinarySearchTree::DataType BinarySearchTree::max() const {
Node* temp = root_;
while (temp->right != nullptr) {
temp = temp->right;
}
return temp->val;
}
BinarySearchTree::DataType BinarySearchTree::min() const {
Node* temp = root_;
while (temp->left != nullptr) {
temp = temp->left;
}
return temp->val;
}
unsigned int BinarySearchTree::depth() const {
return getNodeDepth(root_);
}
void BinarySearchTree::print(Node* n) const {
// In Order Printing
if (n == nullptr)
return;
print(n->left);
std::cout << (n->val) << ",";
print(n->right);
}
bool BinarySearchTree::exists(DataType val) const {
Node* temp = root_;
while (temp != nullptr) {
if (temp->val == val)
return true;
if (temp->val < val)
temp = temp->right;
else
temp = temp->left;
}
return false;
}
BinarySearchTree::Node* BinarySearchTree::getRootNode() {
return root_;
}
BinarySearchTree::Node** BinarySearchTree::getRootNodeAddress() {
return &root_;
}
bool BinarySearchTree::insert(DataType val) {
Node** pT = getRootNodeAddress();
Node* T = *pT;
if (T == nullptr) {
*pT = new Node(val);
} else {
Node* temp = T;
while (true) {
if (val <= temp->val) {
if (temp->left == nullptr) {
temp->left = new Node(val);
break;
} else {
temp = temp->left;
}
} else {
if (temp->right == nullptr) {
temp->right = new Node(val);
break;
} else {
temp = temp->right;
}
}
}
}
size_++;
return true;
}
bool BinarySearchTree::remove(DataType val) {
Node** pT = getRootNodeAddress();
Node* T = *pT;
if (T == nullptr) {
return false;
}
Node* current_node = T;
Node* parent_node = nullptr;
bool isLeftChild = false;
bool found = false;
while (current_node != nullptr) {
if (current_node->val == val) {
found = true;
break;
} else if (val < current_node->val) {
parent_node = current_node;
current_node = current_node->left;
isLeftChild = true;
} else {
parent_node = current_node;
current_node = current_node->right;
isLeftChild = false;
}
}
if (!found)
return false;
if (current_node->left == nullptr && current_node->right == nullptr) {
if (current_node == T) {
delete *pT;
*pT = nullptr;
} else {
if (isLeftChild)
parent_node->left = nullptr;
else
parent_node->right = nullptr;
delete current_node;
}
} else if (current_node->left != nullptr && current_node->right == nullptr) {
if (current_node == T) {
*pT = current_node->left;
} else {
if (isLeftChild)
parent_node->left = current_node->left;
else
parent_node->right = current_node->left;
}
delete current_node;
} else if (current_node->left == nullptr && current_node->right != nullptr) {
if (current_node == T) {
*pT = current_node->right;
} else {
if (isLeftChild)
parent_node->left = current_node->right;
else
parent_node->right = current_node->right;
}
delete current_node;
} else {
Node* successor = current_node->right;
Node* successor_parent = current_node;
isLeftChild = false;
while (successor->left != nullptr) {
successor_parent = successor;
successor = successor->left;
isLeftChild = true;
}
current_node->val = successor->val;
if (isLeftChild)
successor_parent->left = successor->right;
else
successor_parent->right = successor->right;
delete successor;
}
size_--;
return true;
}