summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-07-08 09:47:43 -0700
committerFuwn <[email protected]>2024-07-08 09:47:43 -0700
commitfb2733897fcebb584086b2743ddbe12eb24a9c75 (patch)
treefe1b659af654d4c756bb036308d0273734a152cb
parentfeat: some (diff)
downloadbst-fb2733897fcebb584086b2743ddbe12eb24a9c75.tar.xz
bst-fb2733897fcebb584086b2743ddbe12eb24a9c75.zip
feat: invertHEADmain
-rw-r--r--bst.cc25
1 files changed, 25 insertions, 0 deletions
diff --git a/bst.cc b/bst.cc
index f5f4f07..7101a63 100644
--- a/bst.cc
+++ b/bst.cc
@@ -200,6 +200,25 @@ public:
std::cout << '\n';
}
+
+ static auto invert(std::optional<std::shared_ptr<bst>> bst)
+ -> std::optional<std::shared_ptr<class bst>> {
+ if (bst.has_value()) {
+ if (bst.value()->_left.has_value() || bst.value()->_right.has_value()) {
+ auto left = invert(bst.value()->_left);
+ auto right = invert(bst.value()->_right);
+
+ bst.value()->_left = right;
+ bst.value()->_right = left;
+ }
+
+ return bst;
+ }
+
+ return std::nullopt;
+ }
+
+ auto invert() -> void { invert(std::make_shared<bst<T>>(*this)); }
};
auto main() -> int {
@@ -227,5 +246,11 @@ auto main() -> int {
std::cout << "size: " << bst.size() << '\n';
+ std::cout << "inverted: ";
+
+ bst.invert();
+ bst.invert();
+ bst.print_from_largest_to_smallest();
+
return 0;
}