MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
binarytree.h
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#ifndef BINARYTREE_H
8#define BINARYTREE_H
9
10#include <queue>
11#include "INCLUDE/maiatypes.h"
12#include "IO/infoout.h"
13#include "globalvariables.h"
14
15template <typename T>
17 public:
19 m_object = nullptr;
20 m_father = nullptr;
21 m_leftChild = nullptr;
22 m_rightChild = nullptr;
23 }
24
25 explicit BinaryTreeNode<T>(T* obj) {
26 m_object = obj;
27 m_father = nullptr;
28 m_leftChild = nullptr;
29 m_rightChild = nullptr;
30 }
31
32 BinaryTreeNode<T>(T* obj, BinaryTreeNode<T>* father, BinaryTreeNode<T>* leftChild, BinaryTreeNode<T>* rightChild) {
33 m_object = obj;
34 m_father = father;
35 m_leftChild = leftChild;
36 m_rightChild = rightChild;
37 }
38
39
40 T* getObject() { return m_object; }
44
45 void setObject(T* obj) { m_object = obj; }
46 void setFather(BinaryTreeNode<T>* node) { m_father = node; }
49
50 protected:
55};
56
58
59template <typename T>
61 public:
62 BinaryTree<T>() { m_root = nullptr; }
63 explicit BinaryTree<T>(BinaryTreeNode<T>* root) { m_root = root; }
64
66
68 if(m_root == nullptr) {
69 return 0;
70 }
71
72 MInt lDepth = getNodeDepth(m_root->getLeftChild());
73 MInt rDepth = getNodeDepth(m_root->getRightChild());
74
75 if(lDepth > rDepth) {
76 return (lDepth + 1);
77 }
78 return (rDepth + 1);
79 }
80
81 protected:
83
85 if(node == nullptr) {
86 return (0);
87 }
88
89 MInt lDepth = getNodeDepth(m_root->getLeftChild());
90 MInt rDepth = getNodeDepth(m_root->getRightChild());
91
92 if(lDepth > rDepth) {
93 return (lDepth + 1);
94 }
95 return (rDepth + 1);
96 }
97};
98
100
101
103 public:
104 explicit BinaryPropertyMPITree(MInt num_procs) : m_myMPILocation(nullptr) {
105 MInt rank = globalDomainId();
106 m_queue = new std::queue<BinaryTreeIntNode*>;
107 for(MInt i = 0; i < num_procs; i++) {
108 auto* ins_int = new MInt();
109 *ins_int = i;
110 auto* ins = new BinaryTreeIntNode(ins_int);
111 if(i == 0) {
112 m_root = ins;
113 m_queue->push(m_root);
114 } else {
116 }
117
118 if(i == rank) {
119 m_myMPILocation = ins;
120 }
121 }
122 }
124 for(MUlong i = 0; i < m_queue->size(); i++) {
125 delete m_queue->front();
126 m_queue->pop();
127 }
128 delete m_queue;
129 m_queue = nullptr;
130 }
131
133
135 if(m_myMPILocation == nullptr) {
136 return nullptr;
137 }
138 return m_myMPILocation;
139 }
140
142 if(m_myMPILocation != nullptr && m_myMPILocation->getFather() != nullptr) {
143 return m_myMPILocation->getFather();
144 }
145 return nullptr;
146 }
147
149 if(m_myMPILocation != nullptr && m_myMPILocation->getLeftChild() != nullptr) {
151 }
152 return nullptr;
153 }
155 if(m_myMPILocation != nullptr && m_myMPILocation->getRightChild() != nullptr) {
157 }
158 return nullptr;
159 }
160
161 protected:
163 std::queue<BinaryTreeIntNode*>* m_queue;
164
165 static void printTreeRoot(BinaryTreeIntNode* node, MInt level) {
166 m_log << "L " << level << ":" << *(node->getObject()) << std::endl;
167 if(node->getLeftChild() != nullptr) {
168 printTreeRoot(node->getLeftChild(), level + 1);
169 }
170 if(node->getRightChild() != nullptr) {
171 printTreeRoot(node->getRightChild(), level + 1);
172 }
173 }
174
176 while(!m_queue->empty()) {
177 // full, descend
178 if(m_queue->front()->getLeftChild() != nullptr && m_queue->front()->getRightChild() != nullptr) {
179 m_queue->push(m_queue->front()->getLeftChild());
180 m_queue->push(m_queue->front()->getRightChild());
181 m_queue->pop();
182 }
183 // not full, fill up
184 else {
185 node->setFather(m_queue->front());
186 if(m_queue->front()->getLeftChild() == nullptr) {
187 m_queue->front()->setLeftChild(node);
188 } else {
189 m_queue->front()->setRightChild(node);
190 }
191
192 break;
193 }
194 }
195 }
196};
197
198#endif // BINARYTREE_H
BinaryTreeNode< MInt > BinaryTreeIntNode
Definition: binarytree.h:57
std::queue< BinaryTreeIntNode * > * m_queue
Definition: binarytree.h:163
void insertBalancedNode(BinaryTreeIntNode *node)
Definition: binarytree.h:175
static void printTreeRoot(BinaryTreeIntNode *node, MInt level)
Definition: binarytree.h:165
BinaryTreeIntNode * getMyMPILocation()
Definition: binarytree.h:134
BinaryPropertyMPITree(MInt num_procs)
Definition: binarytree.h:104
BinaryTreeIntNode * m_myMPILocation
Definition: binarytree.h:162
BinaryTreeIntNode * getMyMPIReceiver()
Definition: binarytree.h:141
BinaryTreeIntNode * getMyLeftMPISender()
Definition: binarytree.h:148
BinaryTreeIntNode * getMyRightMPISender()
Definition: binarytree.h:154
MInt getNodeDepth(BinaryTreeNode< T > *node)
Definition: binarytree.h:84
MInt getMaxDepth()
Definition: binarytree.h:67
BinaryTreeNode< T > * getRoot()
Definition: binarytree.h:65
BinaryTreeNode< T > * m_root
Definition: binarytree.h:82
BinaryTreeNode< T > * m_leftChild
Definition: binarytree.h:53
BinaryTreeNode< T > * m_father
Definition: binarytree.h:52
BinaryTreeNode< T > * getRightChild()
Definition: binarytree.h:43
BinaryTreeNode< T > * getLeftChild()
Definition: binarytree.h:42
BinaryTreeNode< T > * getFather()
Definition: binarytree.h:41
void setLeftChild(BinaryTreeNode< T > *node)
Definition: binarytree.h:47
void setRightChild(BinaryTreeNode< T > *node)
Definition: binarytree.h:48
BinaryTreeNode< T > * m_rightChild
Definition: binarytree.h:54
void setFather(BinaryTreeNode< T > *node)
Definition: binarytree.h:46
void setObject(T *obj)
Definition: binarytree.h:45
T * getObject()
Definition: binarytree.h:40
MInt globalDomainId()
Return global domain id.
InfoOutFile m_log
int32_t MInt
Definition: maiatypes.h:62
uint64_t MUlong
Definition: maiatypes.h:65