TTK
Loading...
Searching...
No Matches
DynamicTree.cpp
Go to the documentation of this file.
1#include <DynamicTree.h>
2
3#include <sstream>
4
5// DynamicTree ----------------------------------
6
7std::string ttk::DynamicTree::print() const {
8 std::stringstream res;
9
10 for(const auto &node : nodes_) {
11 res << "id: " << &node - &nodes_[0];
12 if(node.parent_) {
13 res << ", parent: " << node.parent_ - &nodes_[0];
14 } else {
15 res << ", parent: X";
16 }
17 res << " find root: " << node.findRoot();
18 res << " root: " << node.findRoot() - &nodes_[0];
19 res << std::endl;
20 }
21 return res.str();
22}
23
24std::string ttk::DynamicTree::printNbCC() const {
25 std::stringstream res;
26 std::vector<DynTreeNode *> roots;
27 roots.reserve(nodes_.size());
28 for(const auto &n : nodes_) {
29 roots.emplace_back(n.findRoot());
30 }
31 std::sort(roots.begin(), roots.end());
32 const auto it = std::unique(roots.begin(), roots.end());
33 roots.erase(it, roots.end());
34 res << "nb nodes " << nodes_.size() << std::endl;
35 res << "nb cc " << roots.size() << std::endl;
36 return res.str();
37}
38
39// DynTreeNode ----------------------------------
40
42 if(!parent_)
43 return;
44
45 DynTreeNode *curNode = this;
46 DynTreeNode *parentNode = curNode->parent_;
47 DynTreeNode *gParentNode = parentNode->parent_;
48
49 curNode->parent_ = nullptr;
50
51 // Reverse all the node until the root
52 while(true) {
53 parentNode->parent_ = curNode;
54
55 curNode = parentNode;
56 parentNode = gParentNode;
57
58 if(gParentNode) {
59 gParentNode = gParentNode->parent_;
60 } else {
61 break;
62 }
63 }
64}
65
67 DynTreeNode *curNode = const_cast<DynTreeNode *>(this);
68 while(curNode->parent_) {
69 curNode = curNode->parent_;
70 }
71 return curNode;
72}
73
75 evert();
76 auto nRoot = n->findRoot();
77
78 if(nRoot != this) {
79 // The two nodes are in two different trees
80 parent_ = n;
81 return true;
82 }
83
84 // here the nodes are in the same tree
85 return false;
86}
std::string print() const
std::string printNbCC() const
class representing a node of a tree and the link to its parent if not the root
Definition DynamicTree.h:25
bool insertEdge(DynTreeNode *const n)
DynTreeNode * findRoot() const
Get representative node.
DynTreeNode * parent_
Definition DynamicTree.h:26
void evert()
Make this node the root of its tree.