yat  0.21pre
NodeBase.h
1 #ifndef theplu_yat_utility_detail_node_base
2 #define theplu_yat_utility_detail_node_base
3 
4 // $Id: NodeBase.h 4072 2021-08-20 05:40:18Z peter $
5 
6 /*
7  Copyright (C) 2021 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 // This is a private file used by yat/utility/Ranking.h
26 
27 #include <cstddef>
28 
29 namespace theplu {
30 namespace yat {
31 namespace utility {
32 
34 
35  // namespace for internal classes used in class Ranking
36  namespace ranking {
37 
38  class NodeBase
39  {
40  public:
41  NodeBase(void);
42  virtual ~NodeBase(void);
43  NodeBase* parent_;
44  NodeBase* left_;
45  NodeBase* right_;
46  unsigned int height_; // max distance to leaf (plus 1)
47  // number of total offsprings including myself
48  size_t size_;
49 
50  // return left height minus right height
51  int balance(void) const;
52 
53  // return 0 for root, 1 for its children, etc
54  int generation(void) const;
55 
56  // increase size of node and parent node, recursively.
57  void increment_size(void);
58  // decrease size of node and parent node, recursively.
59  void decrement_size(void);
60 
61  // set size to 1 + size of left + size of right
62  void update_size(void);
63  // set height to max of left height and right height plus 1
64  void update_height(void);
65  // set height as in update_height. If heigh is updated, call
66  // parent as well.
67  void update_height_recursively(void);
68 
69  // return true if head node
70  bool is_head_node(void) const;
71  // return true if having a parent and its left child is this
72  bool is_left_node(void) const;
73  // return true if having a parent and its right child is this
74  bool is_right_node(void) const;
75  // return true if having no parent
76  bool is_root_node(void) const;
77  // return traverse up in the tree following the left branch
78  // until a node has no left branch and we return that leaf node.
79  NodeBase* left_most(void);
80  const NodeBase* left_most(void) const;
81  // same as left_most but follow right branch
82  NodeBase* right_most(void);
83  const NodeBase* right_most(void) const;
84  bool validate(bool head=false) const;
85  };
86 
87  size_t height(const NodeBase* node);
88  size_t size(const NodeBase* node);
89 
90  } // end of namespace ranking
91 
93 
94 }}} // of namespace utility, yat, and theplu
95 #endif
The Department of Theoretical Physics namespace as we define it.

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14