Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions 6-1/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/



#include <cassert>
#include <iostream>
#include <string>
Expand All @@ -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;
Expand All @@ -35,7 +40,7 @@ struct Node {

Node(int key_) : key(key_) {}
void add(int new_key);
void pre_order();
void pre_order(Print print);

};

Expand All @@ -55,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<Node *> 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;
}
Expand All @@ -84,6 +90,8 @@ Node::~Node() {
}
}



int main() {
int size, key;
cin >> size >> key;
Expand All @@ -92,7 +100,10 @@ int main() {
cin >> key;
root->add(key);
}
root->pre_order();
Print print;
root->pre_order(print);
cout << endl;
return 0;
}