From abf47eddbd6e8dd38b4e1d282c005f7615929b3f Mon Sep 17 00:00:00 2001 From: kindofman <43539680+kindofman@users.noreply.github.com> Date: Tue, 19 Nov 2019 07:49:51 +0300 Subject: [PATCH 1/2] Update main.cpp --- 6-1/main.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/6-1/main.cpp b/6-1/main.cpp index c9b9468..a8f7e00 100644 --- a/6-1/main.cpp +++ b/6-1/main.cpp @@ -36,6 +36,7 @@ struct Node { Node(int key_) : key(key_) {} void add(int new_key); void pre_order(); + Node* pre_order_step(); }; @@ -71,6 +72,22 @@ void Node::pre_order() { } } +Node* Node::pre_order_step() { + static vector stack; + Node* current = this; + stack.push_back(current); + current = current->left; + while (current || !stack.empty()) { + if (current) { + return current; + } else { + current = stack.back()->right; + stack.pop_back(); + } + } + return nullptr; +} + Node::~Node() { queue q; q.push(this); @@ -92,7 +109,12 @@ int main() { cin >> key; root->add(key); } - root->pre_order(); + Node* current = root; + while ( current ) { + cout << current->key << ' '; + current = current->pre_order_step(); + } cout << endl; + return 0; } From 3c2613a256c01d78c8e3022dde9a59478ffc2182 Mon Sep 17 00:00:00 2001 From: kindofman <43539680+kindofman@users.noreply.github.com> Date: Sat, 23 Nov 2019 10:13:39 +0300 Subject: [PATCH 2/2] Update main.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Сделал функтор для передачи в pre_order --- 6-1/main.cpp | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/6-1/main.cpp b/6-1/main.cpp index a8f7e00..1f62562 100644 --- a/6-1/main.cpp +++ b/6-1/main.cpp @@ -11,6 +11,7 @@ */ + #include #include #include @@ -22,6 +23,10 @@ using namespace std; +struct Print { + void operator() (int key) { cout << key << ' '; } +}; + struct Node { Node(const Node&) = delete; Node(const Node&&) = delete; @@ -35,8 +40,7 @@ struct Node { Node(int key_) : key(key_) {} void add(int new_key); - void pre_order(); - Node* pre_order_step(); + void pre_order(Print print); }; @@ -56,12 +60,13 @@ void Node::add(int new_key) { new_key < current->key ? current->left = new Node(new_key) : current->right = new Node(new_key); } -void Node::pre_order() { +void Node::pre_order(Print print) { vector stack; Node* current = this; while (current || !stack.empty()) { if (current) { - cout << current->key << ' '; + print(current->key); +// cout << current->key << ' '; stack.push_back(current); current = current->left; } @@ -72,22 +77,6 @@ void Node::pre_order() { } } -Node* Node::pre_order_step() { - static vector stack; - Node* current = this; - stack.push_back(current); - current = current->left; - while (current || !stack.empty()) { - if (current) { - return current; - } else { - current = stack.back()->right; - stack.pop_back(); - } - } - return nullptr; -} - Node::~Node() { queue q; q.push(this); @@ -101,6 +90,8 @@ Node::~Node() { } } + + int main() { int size, key; cin >> size >> key; @@ -109,12 +100,10 @@ int main() { cin >> key; root->add(key); } - Node* current = root; - while ( current ) { - cout << current->key << ' '; - current = current->pre_order_step(); - } + Print print; + root->pre_order(print); cout << endl; return 0; } +