summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/RB.cc
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-01-18 12:46:40 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-01-18 12:46:40 +0100
commitd703960951d838ce92687a4947581bc09a160f60 (patch)
tree1b3b67704cb84aea9ad1b04af1ee9de13b23fd83 /Year_2/Algorithms/RB.cc
parent8be9b7e84fbc723089412f62505fee5e6a4c879d (diff)
Add
Diffstat (limited to 'Year_2/Algorithms/RB.cc')
-rw-r--r--Year_2/Algorithms/RB.cc61
1 files changed, 50 insertions, 11 deletions
diff --git a/Year_2/Algorithms/RB.cc b/Year_2/Algorithms/RB.cc
index 9569fe0..eac3f07 100644
--- a/Year_2/Algorithms/RB.cc
+++ b/Year_2/Algorithms/RB.cc
@@ -97,6 +97,11 @@ public:
{
}
+ ~RBtree()
+ {
+ destructor(root_);
+ }
+
RBtree<H>*
insert(H key)
{
@@ -178,23 +183,57 @@ private:
Node<H>* root_ { nullptr };
Node<H>*
+ minimum(Node<H>* x)
+ {
+ if (!x)
+ return x;
+
+ while (x->left())
+ x = x->left();
+
+ return x;
+ }
+
+ Node<H>*
+ successor(Node<H>* x)
+ {
+ if (!x)
+ return x;
+
+ if (x->right()) {
+ return minimum(x->right());
+ }
+
+ auto y = x->parent();
+ while (y && x == y->right()) {
+ x = y;
+ y = y->parent();
+ }
+
+ return y;
+ }
+
+ void
+ destructor(Node<H>* x)
+ {
+ if (!x)
+ return;
+
+ destructor(x->left());
+ destructor(x->right());
+ delete x;
+ }
+
+ Node<H>*
search(H key)
{
auto t = this->root();
- while (t != nullptr && key != t->key()) {
+ while (t && key != t->key()) {
if (key < t->key()) {
- if (t->left()) {
- t = t->left();
- } else {
- break;
- }
+ t = t->left();
} else {
- if (t->right()) {
- t = t->right();
- } else {
- break;
- }
+ t = t->right();
}
}