tree_node.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * \file tree_node.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 12-10-2010
  5. * \brief This file contains the definition of a virtual base class for a tree
  6. * node.
  7. */
  8. #include <pvfmm_common.hpp>
  9. #ifndef _PVFMM_TREE_NODE_HPP_
  10. #define _PVFMM_TREE_NODE_HPP_
  11. namespace pvfmm{
  12. /**
  13. * \brief Virtual base class for tree node.
  14. */
  15. class TreeNode{
  16. public:
  17. /**
  18. * \brief Base class for node data. Contains initialization data for the node.
  19. */
  20. class NodeData{
  21. public:
  22. virtual ~NodeData(){};
  23. virtual void Clear(){}
  24. int max_depth;
  25. int dim;
  26. };
  27. /**
  28. * \brief Initialize pointers to NULL
  29. */
  30. TreeNode(): dim(0), depth(0), max_depth(MAX_DEPTH), parent(NULL), child(NULL), status(1) { }
  31. /**
  32. * \brief Virtual destructor
  33. */
  34. virtual ~TreeNode();
  35. /**
  36. * \brief Initialize the node by passing the relevant data.
  37. */
  38. virtual void Initialize(TreeNode* parent_, int path2node_, NodeData* data_) ;
  39. /**
  40. * \brief Clear node data.
  41. */
  42. virtual void ClearData(){}
  43. /**
  44. * \brief Returns the dimension of the tree.
  45. */
  46. int Dim(){return dim;}
  47. /**
  48. * \brief Returns the depth of this node. (Root has depth 0)
  49. */
  50. int Depth(){return depth;}
  51. /**
  52. * \brief Returns 'true' if this is a leaf node.
  53. */
  54. bool IsLeaf(){return child == NULL;}
  55. /**
  56. * \brief Returns the child corresponding to the input parameter.
  57. */
  58. TreeNode* Child(int id);
  59. /**
  60. * \brief Returns a pointer to the parent node.
  61. */
  62. TreeNode* Parent();
  63. /**
  64. * \brief Returns the index which corresponds to this node among its
  65. * siblings (parent's children).
  66. * this->Parent()->Child(this->Path2Node())==this
  67. */
  68. int Path2Node();
  69. /**
  70. * \brief Allocate a new object of the same type (as the derived class) and
  71. * return a pointer to it type cast as (TreeNode*).
  72. */
  73. virtual TreeNode* NewNode(TreeNode* n_=NULL);
  74. /**
  75. * \brief Evaluates and returns the subdivision condition for this node.
  76. * 'true' if node requires further subdivision.
  77. */
  78. virtual bool SubdivCond();
  79. /**
  80. * \brief Create child nodes and Initialize them.
  81. */
  82. virtual void Subdivide() ;
  83. /**
  84. * \brief Truncates the tree i.e. makes this a leaf node.
  85. */
  86. virtual void Truncate() ;
  87. /**
  88. * \brief Set the parent for this node.
  89. */
  90. void SetParent(TreeNode* p, int path2node_) ;
  91. /**
  92. * \brief Set a child for this node.
  93. */
  94. void SetChild(TreeNode* c, int id) ;
  95. /**
  96. * \brief Returns status.
  97. */
  98. int& GetStatus();
  99. /**
  100. * \brief Update status for all nodes up to the root node.
  101. */
  102. void SetStatus(int flag);
  103. protected:
  104. int dim; //Dimension of the tree
  105. int depth; //Depth of the node (root -> 0)
  106. int max_depth; //Maximum depth
  107. int path2node; //Identity among siblings
  108. TreeNode* parent; //Pointer to parent node
  109. TreeNode** child; //Pointer child nodes
  110. int status;
  111. };
  112. }//end namespace
  113. #endif //_PVFMM_TREE_NODE_HPP_