Advertisement
rawyon

Untitled

Apr 25th, 2013
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Tree
  6. {
  7.  public:
  8.   Tree( const Tree & t )
  9.     : right_( t.get_right() ), left_ (t.get_left() ), data_(t.get_data() ){}
  10.  
  11.   Tree( int data )
  12.     : right_(nullptr), left_(nullptr), data_(data) {}
  13.  
  14.   Tree( Tree* right, Tree* left, int data )
  15.     : right_( right), left_(left), data_(data) {}
  16.  
  17.   Tree* get_right() const { return right_ ;}
  18.   Tree* get_left () const { return left_ ; }
  19.   int   get_data () const { return data_ ; }
  20.  
  21.   Tree clone() const { return { right_, left_, data_}; }
  22.  
  23.   void print() const
  24.   {
  25.     cout << "Right:" << right_ -> data_ << endl
  26.      << "Left :" << left_  -> data_ << endl
  27.      << "Data :" << data_           << endl
  28.      <<                                endl;
  29.   }
  30.  
  31.   void set_right( Tree* right) { right_ = right; }
  32.   void set_left ( Tree* left ) { left_  = left;  }
  33.   void set_data ( int data   ) { data_  = data;  }
  34.  
  35. private:
  36.   Tree* right_;
  37.   Tree* left_ ;
  38.   int   data_ ;
  39. };
  40.  
  41.  
  42.  
  43. int main()
  44. {
  45.  
  46.   Tree* tree1 = new Tree(1);
  47.   Tree* tree2 = new Tree(2);
  48.   Tree* tree3 = new Tree( tree1, tree2, 3 );
  49.  
  50.   Tree* tree4  = new Tree { tree3->clone() };
  51.  
  52.   cout << "Tree3: " << endl;
  53.   tree3->print();
  54.  
  55.   cout << "Tree4: " << endl;
  56.   tree4->print();
  57.  
  58.   tree3->set_right( tree2 );
  59.  
  60.  
  61.   cout << "Tree3: " << endl;
  62.   tree3->print();
  63.  
  64.   cout << "Tree4: " << endl;
  65.   tree4->print();
  66.  
  67.  
  68.   return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement