From 9b7b7a38b921e1849f5288a38f6908b54a7c66a2 Mon Sep 17 00:00:00 2001 From: Trinity-142 Date: Wed, 22 Apr 2026 22:57:57 +0700 Subject: [PATCH] solution --- 1.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/1.cpp b/1.cpp index 9c84ae0..8a91b7e 100644 --- a/1.cpp +++ b/1.cpp @@ -188,12 +188,63 @@ class AVLTree { _delete(root); root = nullptr; } + + class iterator { + vector path; + public: + iterator(Node* root) { + go_left(root); + } + + void go_left(Node* node) { + while (node) { + path.push_back(node); + node = node->left; + } + } + + bool operator!=(const iterator& other) const { + if (path.empty() && other.path.empty()) return false; + if (path.empty() != other.path.empty()) return true; + return path.back() != other.path.back(); + } + + iterator& operator++() { + Node* leaf = path.back(); + path.pop_back(); + go_left(leaf->right); + return *this; + } + + const T* operator->() { + return &path.back()->_value; + } + + const T& operator*() { + return path.back()->_value; + } + }; + + iterator begin() { + return iterator(root); + } + + iterator end() { + return iterator(nullptr); + } }; int main() { - // Move constructor AVLTree int_tree{}; - int_tree.insert(10); int_tree.insert(20); int_tree.insert(30); - AVLTree float_tree{}; - float_tree.insert(0.123); float_tree.insert(0.e-2); float_tree.insert(1.2); + int_tree.insert(100); int_tree.insert(50); int_tree.insert(25); int_tree.insert(75); int_tree.insert(65); + int_tree.insert(85); int_tree.insert(150); int_tree.insert(125); int_tree.insert(175); + auto it = int_tree.begin(); + auto end = int_tree.end(); + for (; it != end; ++it) { + cout << *it << " "; + } + cout << endl; + for (auto&& i : int_tree) { + cout << i << " "; + } }