OpenWalnut  1.5.0dev
WTreeNode.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 #include "../../common/WStringUtils.h"
30 #include "WTreeNode.h"
31 
32 WTreeNode::WTreeNode( size_t index, double level )
33  : std::enable_shared_from_this< WTreeNode >(),
34  m_level( level ),
35  m_index( index )
36 {
37 }
38 
39 WTreeNode::WTreeNode( const WDendrogram &dendrogram )
40  : std::enable_shared_from_this< WTreeNode >()
41 {
42  const std::vector< size_t >& nodes = dendrogram.getParents();
43  const std::vector< double >& heights = dendrogram.getHeights();
44 
45  size_t n = heights.size();
46 
47  std::map< size_t, WTreeNode::SPtr > map;
48 
49  for( size_t index = 0; index < nodes.size(); index++ )
50  {
51  double height = 0.0;
52 
53  if( index > n )
54  {
55  height = heights[ index - n - 1 ];
56  }
57  map[ index ] = WTreeNode::SPtr( new WTreeNode( index, height ) );
58  }
59 
60  for( size_t index = 0; index < nodes.size() - 1; index++ )
61  {
62  size_t parent = nodes[ index ];
63  map[ parent ]->addChild( map[ index ] );
64  }
65 
66  //Last node is the root node
67  WTreeNode::SPtr root = map.at( map.size() - 1 );
68 
69  m_index = root->index();
70  m_level = root->level();
71  m_children = root->getChildren();
72 }
73 
75 {
76 }
77 
79 {
80  return m_level;
81 }
82 
84 {
85  return m_index;
86 }
87 
89 {
90  m_children.push_back( child );
91  child->m_parent = shared_from_this();
92 }
93 
95 {
96  return m_parent;
97 }
98 
99 std::vector< WTreeNode::SPtr > WTreeNode::getChildren()
100 {
101  return m_children;
102 }
Hirachical binary tree datastructure with spatial layout information called dendrogram.
Definition: WDendrogram.h:63
const std::vector< size_t > & getParents() const
Returns const reference to the internal parents array.
const std::vector< double > & getHeights() const
Const reference to the heights array.
A node in a tree, holding an index, a level in the tree and pointers to its child nodes.
Definition: WTreeNode.h:39
void addChild(WTreeNode::SPtr child)
Adds a childnode to this node.
Definition: WTreeNode.cpp:88
WTreeNode(size_t index, double level)
Constructs a new TreeNode.
Definition: WTreeNode.cpp:32
double m_level
Stores the level of this node.
Definition: WTreeNode.h:110
WTreeNode::SPtr getParent()
Returns the parent node of this node.
Definition: WTreeNode.cpp:94
WTreeNode::SPtr m_parent
Stores the parent node.
Definition: WTreeNode.h:120
~WTreeNode()
Default destructor.
Definition: WTreeNode.cpp:74
double level()
Returns the level of the TreeNode.
Definition: WTreeNode.cpp:78
std::vector< WTreeNode::SPtr > getChildren()
Returns the child nodes of this node.
Definition: WTreeNode.cpp:99
size_t index()
Returns the index of the TreeNode.
Definition: WTreeNode.cpp:83
size_t m_index
Stores the index of this node.
Definition: WTreeNode.h:115
std::shared_ptr< WTreeNode > SPtr
Shared pointer abbreviation.
Definition: WTreeNode.h:44
std::vector< WTreeNode::SPtr > m_children
Stores the childnodes of this node.
Definition: WTreeNode.h:105