Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Tree
- {
- public:
- Tree( const Tree & t )
- : right_( t.get_right() ), left_ (t.get_left() ), data_(t.get_data() ){}
- Tree( int data )
- : right_(nullptr), left_(nullptr), data_(data) {}
- Tree( Tree* right, Tree* left, int data )
- : right_( right), left_(left), data_(data) {}
- Tree* get_right() const { return right_ ;}
- Tree* get_left () const { return left_ ; }
- int get_data () const { return data_ ; }
- Tree clone() const { return { right_, left_, data_}; }
- void print() const
- {
- cout << "Right:" << right_ -> data_ << endl
- << "Left :" << left_ -> data_ << endl
- << "Data :" << data_ << endl
- << endl;
- }
- void set_right( Tree* right) { right_ = right; }
- void set_left ( Tree* left ) { left_ = left; }
- void set_data ( int data ) { data_ = data; }
- private:
- Tree* right_;
- Tree* left_ ;
- int data_ ;
- };
- int main()
- {
- Tree* tree1 = new Tree(1);
- Tree* tree2 = new Tree(2);
- Tree* tree3 = new Tree( tree1, tree2, 3 );
- Tree* tree4 = new Tree { tree3->clone() };
- cout << "Tree3: " << endl;
- tree3->print();
- cout << "Tree4: " << endl;
- tree4->print();
- tree3->set_right( tree2 );
- cout << "Tree3: " << endl;
- tree3->print();
- cout << "Tree4: " << endl;
- tree4->print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement