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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
#include <cstddef>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <ostream>
class BinarySearchTree
{
struct Node
{
int data;
Node *parent;
Node *left_child;
Node *right_child;
explicit Node(int data, Node *parent = NULL):
data(data),
parent(parent),
left_child(NULL),
right_child(NULL)
{
}
~Node()
{
delete left_child;
delete right_child;
}
};
public:
BinarySearchTree():
root(NULL),
sz(0)
{
std::srand(std::time(NULL));
}
~BinarySearchTree()
{
delete root;
}
bool empty() const
{
return sz == 0;
}
std::size_t size() const
{
return sz;
}
void insert(int nd) // nd = Node Data
{
if (root != NULL)
{
Node *temp = root;
while (true)
if (nd < temp->data)
{
if (temp->left_child == NULL)
{
temp->left_child = new Node(nd, temp);
break;
}
else
temp = temp->left_child;
}
else
if (nd > temp->data)
{
if (temp->right_child == NULL)
{
temp->right_child = new Node(nd, temp);
break;
}
else
temp = temp->right_child;
}
else // nd == temp->data
return; // nothing to insert
}
else
root = new Node(nd);
++sz;
}
void remove(int nd) // nd = Node Data
{
if (root == NULL)
return;
Node *target = root;
bool found_nd = false;
while (true)
if (nd < target->data)
{
if (target->left_child != NULL)
target = target->left_child;
else
break;
}
else
if (nd > target->data)
{
if (target->right_child != NULL)
target = target->right_child;
else
break;
}
else // nd == target->data
{
found_nd = true;
break;
}
// target wasn't found, nothing to remove
if (!found_nd)
return;
kill_node(target);
--sz;
}
void display(std::ostream &os = std::clog) const
{
os << "BinarySearchTree @ " << this << ", size: " << sz << '\n';
recursive_display_on(os, root);
}
private:
void recursive_display_on(std::ostream &os, const Node *n) const
{
if (n == NULL)
return;
recursive_display_on(os, n->left_child);
os << n->data << ' ';
recursive_display_on(os, n->right_child);
}
void kill_node(Node *target)
{
// according to Wikipedia, we must now deal with three cases:
// (1) no children
// (2) one child
// (3) two children
// case (1)
if (target->left_child == NULL && target->right_child == NULL)
{
if (target->parent == NULL) // this can only be the root node
{
delete root;
root = NULL;
return;
}
else
// determine if `target' is a left child, or a right child
if (target->parent->data < target->data)
target->parent->right_child = NULL;
else
target->parent->left_child = NULL;
delete target;
}
else
// case (2)
if (target->left_child == NULL && target->right_child != NULL)
{
if (target->data > target->parent->data)
target->parent->right_child = target->right_child;
else
target->parent->left_child = target->right_child;
target->right_child->parent = target->parent;
target->right_child = NULL;
delete target;
}
else
if (target->left_child != NULL && target->right_child == NULL)
{
if (target->data > target->parent->data)
target->parent->right_child = target->left_child;
else
target->parent->left_child = target->left_child;
target->left_child->parent = target->parent;
target->left_child = NULL;
delete target;
}
else
// case (3)
{
// according to Wikipedia, we shouldn't be "consistent" in choosing
// between in-order predecessor and in-order successor
if (std::rand() % 2 == 0) // go for predecessor
{
Node *p = target->left_child;
while (p->right_child != NULL)
p = p->right_child;
target->data = p->data;
kill_node(p);
}
else // go for successor
{
Node *s = target->right_child;
while (s->left_child != NULL)
s = s->left_child;
target->data = s->data;
kill_node(s);
}
}
}
Node *root;
std::size_t sz;
};
int main()
{
BinarySearchTree bst;
bst.remove(27);
bst.insert(-1);
bst.insert(-5);
bst.insert(90);
bst.insert(0);
bst.insert(-90);
bst.insert(25);
bst.insert(2);
bst.insert(2);
bst.insert(2);
bst.insert(23);
bst.insert(24);
bst.remove(-90);
bst.remove(-1);
bst.remove(-90);
bst.remove(25);
bst.insert(-3);
bst.display();
}
|