52#ifndef DOXYGEN_SHOULD_SKIP_THIS
56 template <
typename Val >
59 GUM_CONSTRUCTOR(BinTreeNode);
62 children_[0] =
nullptr;
63 children_[1] =
nullptr;
66 template <
typename Val >
67 BinTreeNode< Val >::BinTreeNode(
const BinTreeNode< Val >& from) :
68 val_(from.val_), parent_(nullptr), parent_dir_(BinTreeDir::
NO_PARENT) {
69 GUM_CONS_CPY(BinTreeNode);
72 children_[0] =
nullptr;
73 children_[1] =
nullptr;
76 template <
typename Val >
77 BinTreeNode< Val >::~BinTreeNode() {
78 GUM_DESTRUCTOR(BinTreeNode);
81 if (parent_ !=
nullptr)
82 parent_->children_[
static_cast< int >(parent_dir_)]
85 if (children_[0] !=
nullptr) {
86 children_[0]->parent_ =
nullptr;
87 children_[0]->parent_dir_ = BinTreeDir::NO_PARENT;
90 if (children_[1] !=
nullptr) {
91 children_[1]->parent_ =
nullptr;
92 children_[1]->parent_dir_ = BinTreeDir::NO_PARENT;
99 template <
typename Val >
100 BinTreeNode< Val >& BinTreeNode< Val >::operator=(
const BinTreeNode< Val >& from) {
103 GUM_OP_CPY(BinTreeNode);
110 template <
typename Val >
111 Val& BinTreeNode< Val >::value() {
115 template <
typename Val >
116 Val& BinTreeNode< Val >::operator*() {
120 template <
typename Val >
121 BinTreeNode< Val >* BinTreeNode< Val >::child(BinTreeDir dir)
const {
122 return children_[
static_cast< int >(dir)];
125 template <
typename Val >
126 BinTreeNode< Val >* BinTreeNode< Val >::leftChild()
const {
127 return children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)];
130 template <
typename Val >
131 BinTreeNode< Val >* BinTreeNode< Val >::rightChild()
const {
132 return children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)];
135 template <
typename Val >
136 BinTreeNode< Val >* BinTreeNode< Val >::parent()
const {
140 template <
typename Val >
141 BinTreeDir BinTreeNode< Val >::parentDir()
const {
145 template <
typename Val >
146 void BinTreeNode< Val >::insertLeftChild(BinTreeNode< Val >& new_child) {
147 if (new_child.parent_ !=
nullptr) {
151 if (children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)] !=
nullptr) {
156 new_child.parent_ =
this;
157 new_child.parent_dir_ = BinTreeDir::LEFT_CHILD;
158 children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)] = &new_child;
161 template <
typename Val >
162 BinTreeNode< Val >* BinTreeNode< Val >::insertLeftChild(
const Val& val) {
163 if (children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)] !=
nullptr) {
167 BinTreeNode< Val >* new_child =
new BinTreeNode< Val >(val);
170 new_child->parent_ =
this;
171 new_child->parent_dir_ = BinTreeDir::LEFT_CHILD;
172 children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)] = new_child;
177 template <
typename Val >
178 void BinTreeNode< Val >::insertRightChild(BinTreeNode< Val >& new_child) {
179 if (new_child.parent_ !=
nullptr) {
183 if (children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)] !=
nullptr) {
188 new_child.parent_ =
this;
189 new_child.parent_dir_ = BinTreeDir::RIGHT_CHILD;
190 children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)] = &new_child;
193 template <
typename Val >
194 BinTreeNode< Val >* BinTreeNode< Val >::insertRightChild(
const Val& val) {
195 if (children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)] !=
nullptr) {
199 BinTreeNode< Val >* new_child =
new BinTreeNode< Val >(val);
202 new_child->parent_ =
this;
203 new_child->parent_dir_ = BinTreeDir::RIGHT_CHILD;
204 children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)] = new_child;
209 template <
typename Val >
210 void BinTreeNode< Val >::insertChild(BinTreeNode< Val >& new_child, BinTreeDir child_dir) {
211 if (new_child.parent_ !=
nullptr) {
215 if (children_[
static_cast< int >(child_dir)] !=
nullptr) {
220 new_child.parent_ =
this;
221 new_child.parent_dir_ = child_dir;
222 children_[
static_cast< int >(child_dir)] = &new_child;
225 template <
typename Val >
226 BinTreeNode< Val >* BinTreeNode< Val >::insertChild(
const Val& val, BinTreeDir child_dir) {
227 if (children_[
static_cast< int >(child_dir)] !=
nullptr) {
231 BinTreeNode< Val >* new_child =
new BinTreeNode< Val >(val);
234 new_child->parent_ =
this;
235 new_child->parent_dir_ = child_dir;
236 children_[
static_cast< int >(child_dir)] = new_child;
241 template <
typename Val >
242 void BinTreeNode< Val >::eraseLeftLink() {
243 if (children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)] !=
nullptr) {
244 children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)]->parent_ =
nullptr;
245 children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)]->parent_dir_ = BinTreeDir::NO_PARENT;
246 children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)] =
nullptr;
250 template <
typename Val >
251 void BinTreeNode< Val >::eraseRightLink() {
252 if (children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)] !=
nullptr) {
253 children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)]->parent_ =
nullptr;
254 children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)]->parent_dir_ = BinTreeDir::NO_PARENT;
255 children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)] =
nullptr;
259 template <
typename Val >
260 void BinTreeNode< Val >::eraseLink(BinTreeDir tree_dir) {
261 if (children_[
static_cast< int >(tree_dir)] !=
nullptr) {
262 children_[
static_cast< int >(tree_dir)]->parent_ =
nullptr;
263 children_[
static_cast< int >(tree_dir)]->parent_dir_ = BinTreeDir::NO_PARENT;
264 children_[
static_cast< int >(tree_dir)] =
nullptr;
268 template <
typename Val >
269 BinTreeNode< Val >* BinTreeNode< Val >::leftmostNode()
const {
270 BinTreeNode< Val >* node =
const_cast< BinTreeNode< Val >*
>(
this);
272 while (node->children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)] !=
nullptr)
273 node = node->children_[
static_cast< int >(BinTreeDir::LEFT_CHILD)];
278 template <
typename Val >
279 BinTreeNode< Val >* BinTreeNode< Val >::rightmostNode()
const {
280 BinTreeNode< Val >* node =
const_cast< BinTreeNode< Val >*
>(
this);
282 while (node->children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)] !=
nullptr)
283 node = node->children_[
static_cast< int >(BinTreeDir::RIGHT_CHILD)];
288 template <
typename Val >
289 BinTreeNode< Val >* BinTreeNode< Val >::root()
const {
290 BinTreeNode< Val >* node =
const_cast< BinTreeNode< Val >*
>(
this);
292 while (node->parent_ !=
nullptr)
293 node = node->parent_;
BinTreeNode(const Val &v)
Basic constructor: a node without parent nor children.
Exception : a similar element already exists.
#define GUM_ERROR(type, msg)
gum is the global namespace for all aGrUM entities
BinTreeDir
The direction of a given edge in a binary tree.