tree.hh
上传用户:l56789
上传日期:2022-02-25
资源大小:2422k
文件大小:2k
源码类别:

图形图像处理

开发平台:

Matlab

  1. //-----------------------------------------------------------------------------
  2. // tree.hh
  3. //-----------------------------------------------------------------------------
  4. //
  5. // Specification for perfect balance nChildren-ary tree structure where
  6. // every node, except the leaves, has exactly nChildren.
  7. // Data is stored in arrays, one for each level.  Levels are indexed
  8. // from top to bottom.
  9. // Parent-child relationships within the tree are implicitly contained in
  10. // the array indices.
  11. // More specific, the children for the node level[i][j] are:
  12. // level[i+1][j*nChildren+k] for k = 0,1,...,nChildren-1
  13. // Note that this allows us to treat multiple trees of same size as
  14. // one tree of multi roots.
  15. //
  16. //
  17. // Example Tree file format:
  18. // nRoots: 4 (the number of roots, or effectively totally number of trees)
  19. // nChildren: 4 (number of child nodes connected to each parent)
  20. // nLevels: 3 (number of tree levels)
  21. // (tree data from top level down encoded)
  22. //-----------------------------------------------------------------------------
  23. #ifndef TREE_H
  24. #define TREE_H
  25. #include <iostream>
  26. #include <vector>
  27. using std::vector;
  28. template <class T>
  29. class tree 
  30. {
  31.     public:
  32. tree() {}
  33. tree(int nr, int nc, int nl, const T& value = T());
  34. tree(const tree<T>& aTree); // copy constructor
  35. tree(char *filename); // tree from file
  36. tree<T>& operator = (const tree<T>& aTree);
  37. vector<T>& operator [] (int lind) { return data[lind]; }
  38. int nlev() { return data.size(); }
  39. int nrt() { return ((nlev() > 0) ? data[0].size() : 0); }
  40. int nch() { return ((nlev() > 1) ? data[1].size() / nrt() 
  41.     : 0); }
  42. void dump(char *filename);       // write to a binary file
  43.     private:
  44. vector< vector<T> > data;
  45. };
  46. #endif // TREE_H