-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.cpp
59 lines (50 loc) · 1.58 KB
/
TreeNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstring>
#include <iomanip>
#include "TreeNode.h"
void TreeNode::printFormatted(int aIndent) const {
// Indent the current entry
for (int k = 0; k < aIndent; ++k)
std::cout << ' ';
// Print the node info
if (mNodeMark.empty())
std::cout << '<' << mNodeName << "> " << mBranchLength;
else
std::cout << '<' << mNodeName << ">#" << mNodeMark << ' ' << mBranchLength;
// End line
std::cout << std::endl;
// Print the children (indented by 3 spaces)
std::vector<TreeNode *>::const_iterator in(mChildrenList.begin());
const std::vector<TreeNode *>::const_iterator end(mChildrenList.end());
for (; in != end; ++in)
(*in)->printFormatted(aIndent + 3);
}
void TreeNode::printNode(void) const {
// Print the node info
std::cout << mNodeName;
if (!mNodeMark.empty())
std::cout << '#' << mNodeMark;
std::cout << std::setprecision(6) << ":" << mBranchLength;
}
void TreeNode::printNodeWoutLen(void) const {
// Print the node info
std::cout << mNodeName;
if (!mNodeMark.empty())
std::cout << '#' << mNodeMark;
// std::cout << std::setprecision(6) << ":" << mBranchLength;
}
void TreeNode::clearNode(void) {
// Recursively remove the nodes
if (!mChildrenList.empty()) {
std::vector<TreeNode *>::iterator in(mChildrenList.begin());
const std::vector<TreeNode *>::iterator end(mChildrenList.end());
for (; in != end; ++in)
(*in)->clearNode();
for (in = mChildrenList.begin(); in != end; ++in)
delete (*in);
}
// Special case for the root node
if (!mParent) {
mChildrenList.clear();
}
}