Advertisement
framp

Simple Trees

Aug 23rd, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Node{
  5. private:
  6.     std::string value;
  7.     Node * left, * right;
  8.    
  9. public:
  10.     Node(std::string value, Node * left = 0, Node * right = 0) :
  11.         value(value), left(left), right(right)
  12.     {};
  13.    
  14.     operator const char *() const{
  15.         std::vector<const Node *> list(1, this);
  16.         std::string output;
  17.         while(!list.empty()){
  18.             std::vector<const Node *> next;
  19.             for (unsigned int i=0; i < list.size(); i++){
  20.                 output += list[i]->value + " ";
  21.                 if (list[i]->left)
  22.                     next.push_back(list[i]->left);
  23.                 if (list[i]->right)
  24.                     next.push_back(list[i]->right);
  25.             }
  26.             output += "\n";
  27.             list.assign(next.begin(), next.end());
  28.         }
  29.         return output.c_str();
  30.     };
  31. };
  32.  
  33. int main(){
  34.     std::cout << * new Node("R",
  35.         new Node("A",
  36.             new Node("C",
  37.                 new Node("G"),
  38.                 new Node("H")
  39.             ),
  40.             new Node("D",
  41.                 new Node("I"),
  42.                 new Node("J")
  43.             )
  44.         ), new Node("B",
  45.             new Node("E",
  46.                 new Node("K"),
  47.                 new Node("L")
  48.             ),
  49.             new Node("F",
  50.                 new Node("M"),
  51.                 new Node("N")
  52.             )
  53.         )
  54.     );
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement