diff options
| author | Fuwn <[email protected]> | 2024-07-08 09:47:43 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-07-08 09:47:43 -0700 |
| commit | fb2733897fcebb584086b2743ddbe12eb24a9c75 (patch) | |
| tree | fe1b659af654d4c756bb036308d0273734a152cb | |
| parent | feat: some (diff) | |
| download | bst-fb2733897fcebb584086b2743ddbe12eb24a9c75.tar.xz bst-fb2733897fcebb584086b2743ddbe12eb24a9c75.zip | |
| -rw-r--r-- | bst.cc | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -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; } |