Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- class Node{
- private:
- std::string value;
- Node * left, * right;
- public:
- Node(std::string value, Node * left = 0, Node * right = 0) :
- value(value), left(left), right(right)
- {};
- operator const char *() const{
- std::vector<const Node *> list(1, this);
- std::string output;
- while(!list.empty()){
- std::vector<const Node *> next;
- for (unsigned int i=0; i < list.size(); i++){
- output += list[i]->value + " ";
- if (list[i]->left)
- next.push_back(list[i]->left);
- if (list[i]->right)
- next.push_back(list[i]->right);
- }
- output += "\n";
- list.assign(next.begin(), next.end());
- }
- return output.c_str();
- };
- };
- int main(){
- std::cout << * new Node("R",
- new Node("A",
- new Node("C",
- new Node("G"),
- new Node("H")
- ),
- new Node("D",
- new Node("I"),
- new Node("J")
- )
- ), new Node("B",
- new Node("E",
- new Node("K"),
- new Node("L")
- ),
- new Node("F",
- new Node("M"),
- new Node("N")
- )
- )
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement