Advertisement
zhangsongcui

Tree

Apr 17th, 2011
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <stack>
  5. #include <queue>
  6. #include <algorithm>
  7. #include <boost/ptr_container/ptr_vector.hpp>
  8. #include <boost/ptr_container/serialize_ptr_vector.hpp>
  9. #include <boost/archive/text_oarchive.hpp>
  10. #include <boost/archive/text_iarchive.hpp>
  11.  
  12. template <typename T>
  13. struct Tree
  14. {
  15.     Tree(): Data(), Childs() {};
  16.     Tree(const T& rhs): Data(rhs), Childs() {}
  17.     Tree(T&& rhs): Data(rhs), Childs() {}
  18.  
  19.     void addChild(const Tree<T>& rhs)
  20.     {
  21.         this->Childs.push_back(new Tree<T>(rhs));
  22.     }
  23.  
  24.     void addChild(Tree<T>&& rhs)
  25.     {
  26.         this->Childs.push_back(new Tree<T>(rhs));
  27.     }
  28.     /*
  29.     template <typename Fn>
  30.     void Traversal(Fn fn, unsigned depth=0)
  31.     {
  32.         fn(Data, depth);
  33.         std::for_each(Childs.begin(), Childs.end(), boost::bind(&Tree::Traversal<Fn>, _1, fn, depth+1));
  34.     }
  35.     */
  36.     template <typename Fn>
  37.     void DFS(Fn fn)
  38.     {
  39.         std::stack<std::pair<std::pair<boost::ptr_vector<Tree>::iterator, boost::ptr_vector<Tree>::iterator>, unsigned> > s;
  40.         fn(Data, 0);
  41.         s.push(std::make_pair(std::make_pair(Childs.begin(), Childs.end()), 1));
  42.         while (true)
  43.         {
  44.             if (s.top().first.first != s.top().first.second)
  45.             {
  46.                 fn(s.top().first.first->Data, s.top().second);
  47.                 s.push(std::make_pair(std::make_pair(s.top().first.first->Childs.begin(), s.top().first.first->Childs.end()), s.top().second+1));
  48.             }
  49.             else
  50.             {
  51.                 s.pop();
  52.                 if (s.empty())
  53.                     break;
  54.                 ++s.top().first.first;
  55.             }
  56.         }
  57.     }
  58.  
  59.     template <typename Fn>
  60.     void BFS(Fn fn)
  61.     {
  62.         std::queue<std::pair<std::pair<boost::ptr_vector<Tree>::iterator, boost::ptr_vector<Tree>::iterator>, unsigned> > q;
  63.         fn(Data, 0);
  64.         q.push(std::make_pair(std::make_pair(Childs.begin(), Childs.end()), 1));
  65.         do
  66.         {
  67.             for (boost::ptr_vector<Tree>::iterator it=q.front().first.first; it!=q.front().first.second; ++it)
  68.             {
  69.                 fn(it->Data, q.front().second);
  70.                 q.push(std::make_pair(std::make_pair(it->Childs.begin(), it->Childs.end()), q.front().second+1));
  71.             }
  72.         }
  73.         while (q.pop(), !q.empty());
  74.     }
  75.  
  76.     template <typename Archive>
  77.     void serialize(Archive& ar, const unsigned version)
  78.     {
  79.         ar & Data & Childs;
  80.     }
  81.  
  82.     T Data;
  83.     boost::ptr_vector<Tree> Childs;
  84. };
  85.  
  86. void print(const std::string& str, unsigned depth)
  87. {
  88.     while (depth--)
  89.         std::cout << '\t';
  90.     std::cout << str << std::endl;
  91. }
  92.  
  93. int main()
  94. {
  95.     typedef Tree<std::string> str_Tree;
  96.  
  97.     str_Tree root("Root");
  98.     {
  99.         str_Tree node1("Node 1");
  100.         node1.addChild(str_Tree("Leaf 1"));
  101.         node1.addChild(str_Tree("Leaf 2"));
  102.         node1.addChild(str_Tree("Leaf 3"));
  103.         root.addChild(std::move(node1));
  104.     }
  105.     root.addChild(str_Tree("Leaf 4"));
  106.     {
  107.         str_Tree node2("Node 2");
  108.         node2.addChild(str_Tree("Leaf 5"));
  109.         node2.addChild(str_Tree("Leaf 6"));
  110.         {
  111.             str_Tree node21("Node 2_1");
  112.             node21.addChild(str_Tree("Leaf 7"));
  113.             node21.addChild(str_Tree("Leaf 8"));
  114.             node2.addChild(std::move(node21));
  115.         }
  116.         node2.addChild(str_Tree("Leaf 9"));
  117.         root.addChild(std::move(node2));
  118.     }
  119.     root.addChild(str_Tree("Leaf 10"));
  120.  
  121.     root.DFS(print);
  122.     root.BFS(print);
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement