Guest User

Untitled

a guest
May 19th, 2019
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include "tree.hh"
  5. #include "tree_util.hh"
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12. tree<string> tr;
  13. tree<string>::iterator top, one, two, loc, banana;
  14.  
  15. top = tr.begin();
  16. one = tr.insert(top, "one");
  17. two = tr.append_child(one, "two");
  18. tr.append_child(two, "apple");
  19. banana = tr.append_child(two, "banana");
  20. tr.append_child(banana, "cherry");
  21. tr.append_child(two, "peach");
  22. tr.append_child(one, "three");
  23.  
  24. cout << "printing bracketed tree:" << endl;
  25. kptree::print_tree_bracketed(tr);
  26.  
  27. cout << endl << endl;
  28.  
  29.  
  30. cout << "printing leaf nodes and appending a child to each one:" << endl;
  31. tree<string>::leaf_iterator iter = tr.begin_leaf();
  32.  
  33. while (tr.is_valid(iter)) {
  34. cout << *(tr.parent(iter)) << " > " << (*iter) << endl;
  35.  
  36. // Capture the current iterator location for appending children
  37. tree<string>::leaf_iterator append_iter = iter;
  38.  
  39. // advance your real iterator to the next node before adding children
  40. iter++;
  41.  
  42. // Loop and append new children
  43. for (int i = 0; i < 3; i++) {
  44. tr.append_child(append_iter, "child" + to_string(i));
  45. }
  46.  
  47. }
  48.  
  49. cout << endl << "-------" << endl << endl << "printing tree with new children:" << endl;
  50. kptree::print_tree_bracketed(tr);
  51.  
  52. cout << endl << endl << "Printing leaf nodes" << endl << endl;
  53.  
  54. iter = tr.begin_leaf();
  55. while (tr.is_valid(iter)) {
  56. cout << *(tr.parent(iter)) << " > " << (*iter) << endl;
  57.  
  58. iter++;
  59. }
  60.  
  61. return 0;
  62. }
RAW Paste Data