Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include "tree.hh"
- #include "tree_util.hh"
- using namespace std;
- int main()
- {
- tree<string> tr;
- tree<string>::iterator top, one, two, loc, banana;
- top = tr.begin();
- one = tr.insert(top, "one");
- two = tr.append_child(one, "two");
- tr.append_child(two, "apple");
- banana = tr.append_child(two, "banana");
- tr.append_child(banana, "cherry");
- tr.append_child(two, "peach");
- tr.append_child(one, "three");
- cout << "printing bracketed tree:" << endl;
- kptree::print_tree_bracketed(tr);
- cout << endl << endl;
- cout << "printing leaf nodes and appending a child to each one:" << endl;
- tree<string>::leaf_iterator iter = tr.begin_leaf();
- while (tr.is_valid(iter)) {
- cout << *(tr.parent(iter)) << " > " << (*iter) << endl;
- // Capture the current iterator location for appending children
- tree<string>::leaf_iterator append_iter = iter;
- // advance your real iterator to the next node before adding children
- iter++;
- // Loop and append new children
- for (int i = 0; i < 3; i++) {
- tr.append_child(append_iter, "child" + to_string(i));
- }
- }
- cout << endl << "-------" << endl << endl << "printing tree with new children:" << endl;
- kptree::print_tree_bracketed(tr);
- cout << endl << endl << "Printing leaf nodes" << endl << endl;
- iter = tr.begin_leaf();
- while (tr.is_valid(iter)) {
- cout << *(tr.parent(iter)) << " > " << (*iter) << endl;
- iter++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement