fork(1) download
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4.  
  5. struct node
  6. {
  7. node( const std::string& d, node* lc, node* rc )
  8. : data(d), left_child(lc), right_child(rc) {}
  9.  
  10. ~node() { delete left_child ; delete right_child ; }
  11.  
  12. std::string data ;
  13. node* left_child ; // unique owning pointer
  14. node* right_child ; // unique owning pointer
  15.  
  16. private: node( const node& ) ; void operator= ( const node& ) ; // non-copyable
  17. };
  18.  
  19. std::ostream& write( std::ostream& stm, node* tree )
  20. {
  21. if(tree)
  22. {
  23. stm << tree->data << ' ' ;
  24. write( stm, tree->left_child ) ;
  25. return write( stm, tree->right_child ) ;
  26. }
  27. else return stm << "# " ;
  28. }
  29.  
  30. node* make_tree( std::istream& stm )
  31. {
  32. std::string data ;
  33. if( stm >> data && data != "#" )
  34. {
  35. /*auto*/ node* left_child = make_tree(stm) ;
  36. return new node( data, left_child, make_tree(stm) ) ;
  37. }
  38. else return 0 ;
  39. }
  40.  
  41. int main()
  42. {
  43. std::istringstream stm( "root aaa C # # D # # string 1231 # # #" ) ;
  44. /*auto*/node* tree = make_tree(stm) ;
  45. write( std::cout, tree ) << '\n' ;
  46. delete tree ;
  47. }
  48.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
root aaa C # # D # # string 1231 # # #