source: trunk/source/visualization/XXX/include/tree/tree.cc @ 834

Last change on this file since 834 was 834, checked in by garnier, 16 years ago

import all except CVS

File size: 3.9 KB
Line 
1/*
2Copyright (c) 2003-2006, Troy Aaron Johnson
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
9  * Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14  * Neither the name of Troy Aaron Johnson nor the names of any
15    contributors may be used to endorse or promote products derived from
16    this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28POSSIBILITY OF SUCH DAMAGE.
29*/
30
31template <class N>
32template <class InputIterator>
33tree<N>::tree(InputIterator first, InputIterator last, unsigned int n)
34{
35  assert(n != 0);
36
37  createMaster();
38
39  if (first == last)
40    return;
41
42  setRoot(*first);
43
44  /* don't directly initialize i to first + 1 because not all STL
45     iterators support addition */
46  InputIterator i(first);
47 
48  if (++i == last)
49    return;
50
51  /* Make a list of leaves. Initially the list contains only the
52     root.  Pick a leaf and add items to it until it is full.
53     Added items become new leaves.  Continue until there
54     are no more items */
55
56  std::list<typename tree<N>::iterator> leaves;
57  leaves.push_back(getRoot());
58
59  for (;;)
60  {
61    typename tree<N>::iterator leaf(leaves.front());
62
63    for (unsigned int k = 0; k < n; ++k)
64    {
65      leaves.push_back(leaf.push_back(*i));
66
67      if (++i == last)
68        return; 
69    }
70
71    leaves.pop_front();
72  }
73}
74
75template <class N>
76void tree<N>::copy_subtree(tree<N>::const_iterator subtree)
77{
78  /* Copying replaces this tree. */
79  clear();
80
81  /* Original is empty, so leave it empty. */
82  if (subtree == NULL)
83    return;
84
85  /* Copy the root data. */
86  sibling_iterator root(setRoot(*subtree));
87
88  /* Recursively copy the children. */ 
89  for (const_sibling_iterator i(subtree.beginChildren());
90       i != subtree.endChildren(); ++i)
91    root.absorb_back(new tree<N>(i));
92}
93
94template <class N>
95void tree<N>::clear(void)
96{
97  if (empty())
98    return;
99
100  /* delete all nodes bottom-up */
101  for (postorder_iterator i(beginPost()); i != endPost(); )
102  {
103    postorder_iterator tmp(i);
104    ++i;
105    tmp.destroy();
106  }
107
108  /* Preserve master invariant. */
109  master->first_child  = master->last_child   = NULL;
110  master->prev_sibling = master->next_sibling = master; 
111
112  /* Leave the master for the destructor. */
113}
114
115template <class N>
116size_t tree<N>::height(void) const
117{
118  size_t h = 0;
119
120  for (const_preorder_iterator i(beginPre()); i != endPre(); ++i)
121  {
122    if (static_cast<size_t>(i.depth() + 1) > h)
123      h = i.depth() + 1;
124  }
125
126  return h;
127}
128
129template <class N>
130size_t tree<N>::height_leftmost(void) const
131{
132  size_t h = 0;
133
134  tree_node<N>* p = master->next_sibling;
135
136  if (p != master)
137  {
138    do {
139      ++h;
140      p = p->first_child;
141    } while (p != NULL);
142  } 
143
144  return h;
145}
146
147template<class N>
148size_t tree<N>::size(void) const
149{
150  size_t s = 0;
151
152  for (const_preorder_iterator i(beginPre()); i != endPre(); ++i)
153    ++s; 
154
155  return s;
156} 
Note: See TracBrowser for help on using the repository browser.