diff --git a/6-1/main.cpp b/6-1/main.cpp index c9b9468..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,7 +40,7 @@ struct Node { Node(int key_) : key(key_) {} void add(int new_key); - void pre_order(); + void pre_order(Print print); }; @@ -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 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; } @@ -84,6 +90,8 @@ Node::~Node() { } } + + int main() { int size, key; cin >> size >> key; @@ -92,7 +100,10 @@ int main() { cin >> key; root->add(key); } - root->pre_order(); + Print print; + root->pre_order(print); cout << endl; + return 0; } +