#include using namespace std; class Tree { private: struct Node { int v; Node* r; Node* l; }; Node* root; void rec_add(int i, Node* current) { if ( i <= current->v ) //descend left { if ( current->l == nullptr ) { current->l = new Node{i, nullptr, nullptr}; } else { rec_add( i, current->l ); } } else //descend right { if ( current->r == nullptr ) { current->r = new Node{i, nullptr, nullptr}; } else { rec_add( i, current->r ); } } } void rec_print(Node* current) { if ( current ) { rec_print(current->l); cout << current->v << ", "; rec_print(current->r); } } void rec_delete(Node* current) { if ( current != nullptr ) { rec_delete( current -> l ); rec_delete( current -> r ); delete current; } } public: Tree() : root{} {}; ~Tree() { rec_delete( root ); } Tree( Tree const& ) = delete; Tree& operator=( Tree const& ) = delete; Tree( Tree && other ) : Tree{} { swap(root, other.root); } Tree& operator=( Tree && other ) { swap(root, other.root); return *this; } void add(int i) { if (root == nullptr) { root = new Node{i, nullptr, nullptr}; } else { rec_add(i, root); } } void print() { cout << "tree: "; if ( root ) { rec_print(root->l); cout << root->v << ", "; rec_print(root->r); } cout << endl; } }; int main() { Tree t1; t1.add(3); t1.add(5); t1.add(2); cout << "t1: "; t1.print(); Tree t2; t2 = move(t1); t2.add(1); cout << "t2: "; t2.print(); }