CoMMA 1.3.2
A geometric agglomerator for unstructured meshes
Loading...
Searching...
No Matches
Tree.h
Go to the documentation of this file.
1#ifndef COMMA_PROJECT_TREE_H
2#define COMMA_PROJECT_TREE_H
3
4/*
5 * CoMMA
6 *
7 * Copyright © 2024 ONERA
8 *
9 * Authors: Nicolas Lantos, Alberto Remigi, and Riccardo Milani
10 * Contributors: Karim Anemiche
11 *
12 * This Source Code Form is subject to the terms of the Mozilla Public
13 * License, v. 2.0. If a copy of the MPL was not distributed with this
14 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
15 */
16
17#include <cassert>
18#include <cstddef>
19#include <iostream>
20#include <memory>
21#include <vector>
22
23namespace comma {
24
32template<
33 typename CoMMAIndexType,
34 typename CoMMAWeightType,
35 typename CoMMAIntType>
36class Node {
37public:
38 Node(CoMMAIndexType index, CoMMAWeightType volume) :
39 _index(index), _volume(volume){};
41 CoMMAIndexType _index;
43 CoMMAWeightType _volume;
45 CoMMAIntType _sonc = 0;
47 std::shared_ptr<Node> _father;
49 std::shared_ptr<Node> _left_idx;
51 std::shared_ptr<Node> _right_idx;
53 std::shared_ptr<Node> _left_son_idx;
54};
55
64template<
65 typename CoMMAIndexType,
66 typename CoMMAWeightType,
67 typename CoMMAIntType>
68class Tree {
69public:
72
76 explicit Tree(std::shared_ptr<NodeType> &root) :
77 _root(root) {}
78
80 ~Tree() = default;
81
83 std::shared_ptr<NodeType> _root;
84
92 const CoMMAIndexType &father_index,
93 const CoMMAIndexType &index,
94 const CoMMAWeightType &volume,
95 const CoMMAIntType &root
96 ) {
97 std::shared_ptr<NodeType> insertion =
98 std::make_shared<NodeType>(index, volume);
99 auto u_p_father =
100 root == 1 ? _root : search(_root->_left_son_idx, father_index);
101 assert(u_p_father != nullptr);
102 insertion->_father = u_p_father;
103 auto left_idx = transverse(u_p_father->_left_son_idx);
104 if (left_idx == nullptr) {
105 u_p_father->_left_son_idx = insertion;
106 } else {
107 insertion->_left_idx = left_idx;
108 left_idx->_right_idx = insertion;
109 }
110 u_p_father->_sonc = u_p_father->_sonc + 1;
111 // std::cout << u_p_father->_sonc << std::endl;
112 }
113
119 std::shared_ptr<NodeType> search(
120 std::shared_ptr<NodeType> &node, const CoMMAIndexType &value
121 ) {
122 if (node->_index == value && node->_father != nullptr) {
123 return node;
124 }
125 if (node == nullptr || node->_right_idx == nullptr) {
126 return nullptr;
127 }
128 return (search(node->_right_idx, value));
129 }
130
135 std::shared_ptr<NodeType> transverse(std::shared_ptr<NodeType> &node) {
136 if (node == nullptr || node->_right_idx == nullptr) {
137 return node;
138 }
139 return (transverse(node->_right_idx));
140 }
141
145 void deleteNode(const CoMMAIndexType &value) {
146 delete_node(_root->_left_son_idx, value);
147 }
148
154 std::shared_ptr<NodeType> &searched_node, const CoMMAIndexType &value
155 ) {
156 if (searched_node == nullptr) {
157 return;
158 }
159 if (searched_node->_index == value) {
160 // case 0: leftest node
161 if (searched_node->_left_idx == nullptr) {
162 searched_node->_father->_sonc--;
163 searched_node->_father->_left_son_idx = searched_node->_right_idx;
164 searched_node->_right_idx->_left_idx = nullptr;
165 }
166 // case 1: rightest node
167 else if (searched_node->_right_idx == nullptr) {
168 searched_node->_father->_sonc = searched_node->_father->_sonc - 1;
169 searched_node->_left_idx->_right_idx.reset();
170 } else {
171 searched_node->_father->_sonc--;
172 searched_node->_left_idx->_right_idx = searched_node->_right_idx;
173 searched_node->_right_idx->_left_idx = searched_node->_left_idx;
174 }
175 return;
176 }
177 delete_node(searched_node->_right_idx, value);
178 }
179
182
186 void print_nodes(std::shared_ptr<NodeType> &node) {
187 if (node == nullptr) {
188 return;
189 }
190 // std::cout << "node" << node->_index << std::endl;
191 print_nodes(node->_left_son_idx);
192 print_nodes(node->_right_idx);
193 }
194};
195
196} // end namespace comma
197
198#endif // COMMA_PROJECT_TREE_H
Node data structure that represent a node of the tree.
Definition: Tree.h:36
std::shared_ptr< Node > _right_idx
Shared pointer to the right element.
Definition: Tree.h:51
std::shared_ptr< Node > _left_son_idx
Shared pointer to the left element.
Definition: Tree.h:53
std::shared_ptr< Node > _father
Shared pointer to the father node.
Definition: Tree.h:47
CoMMAWeightType _volume
Volume.
Definition: Tree.h:43
std::shared_ptr< Node > _left_idx
Shared pointer to the left element.
Definition: Tree.h:49
CoMMAIntType _sonc
Number of son.
Definition: Tree.h:45
Node(CoMMAIndexType index, CoMMAWeightType volume)
Definition: Tree.h:38
CoMMAIndexType _index
Index of the cell.
Definition: Tree.h:41
Tree structure that represent a coarse cell, the fine cell and the neighbours to them.
Definition: Tree.h:68
void print_nodes(std::shared_ptr< NodeType > &node)
Print the branches starting from a given node.
Definition: Tree.h:186
std::shared_ptr< NodeType > _root
The Node at the root of the tree.
Definition: Tree.h:83
void deleteNode(const CoMMAIndexType &value)
Delete a node.
Definition: Tree.h:145
void insertSon(const CoMMAIndexType &father_index, const CoMMAIndexType &index, const CoMMAWeightType &volume, const CoMMAIntType &root)
Insert a node as child of a given node.
Definition: Tree.h:91
std::shared_ptr< NodeType > transverse(std::shared_ptr< NodeType > &node)
Traverse the tree.
Definition: Tree.h:135
void print()
Print the tree.
Definition: Tree.h:181
std::shared_ptr< NodeType > search(std::shared_ptr< NodeType > &node, const CoMMAIndexType &value)
Look for a node.
Definition: Tree.h:119
void delete_node(std::shared_ptr< NodeType > &searched_node, const CoMMAIndexType &value)
Delete a node.
Definition: Tree.h:153
~Tree()=default
Destructor.
Tree(std::shared_ptr< NodeType > &root)
Constructor.
Definition: Tree.h:76
Definition: Agglomerator.h:37