File: newtree.html

package info (click to toggle)
cadabra 1.46-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,244 kB
  • sloc: cpp: 33,188; ansic: 2,724; makefile: 329; yacc: 180; sh: 157; python: 45; lex: 38; lisp: 19
file content (254 lines) | stat: -rw-r--r-- 11,367 bytes parent folder | download | duplicates (6)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> 
<title>tree.hh: an STL-like C++ tree class</title>
<!-- <link rel="stylesheet" type="text/css" href="../software.css"> -->
<link rel="stylesheet" type="text/css" href="../../../cadabra/web/youngtab.css"> 
</head>
<body>
<div id="head">
<h1 class="shadow">tree.hh</h1>
<h1 class="intro">tree.hh</h1>
<h2 class="intro">An STL-like C++ tree class</h2>
<h3 class="intro">Copyright &copy; 2001-2007  Kasper Peeters</h3></div>
<div id="content">
<h2>Overview</h2> 

<div class="text">
The <code>tree.hh</code> library for C++ provides an STL-like container class
for <i>n</i>-ary trees, templated over the data stored at the nodes.  Various
types of iterators are provided (post-order, pre-order, and
others). Where possible the access methods are compatible with the STL
or alternative algorithms are available. The library is available
under the terms of the GNU General Public License.</div>

<div class="text">Documentation is available in the form of a <a
href="tree-VERSION/tree.ps">postscript</a> and a <a href="tree-VERSION/tree.pdf">pdf</a> file (also
available in the tarball as a LaTeX file). This documentation is still a
bit short and not entirely complete. See the test program (included in
the distribution) for an example of how to use
tree.hh. Also look at the <a href="#example">simple example</a>
below. There is also some <a href="doxygen/html">doxygen generated documentation</a>.</div>


<div class="text">The <code>tree.hh</code> code is available under the terms of the 
<a href="http://www.gnu.org/copyleft/gpl.html">GNU
General Public License</a>. If you use <code>tree.hh</code>,
please satisfy my curiosity and write me a small email with
a bit of explanation of your software and the role of my tree 
class in it.</div>

<div class="text">The <code>tree.hh</code> library is meant for generic <i>n</i>-ary
trees. If you are only interested in AVL binary search trees
(Adelson,Velskii & Landis), you may want to have a look at the
<a href="http://www.geocities.com/wkaras/gen_cpp/avl_tree.html">C++
AVL tree template</a> page. </div>

<h2>Download</h2> 

<div class="text">Everything (the header file, examples, documentation
and all other things referred to on this page) is contained in the
tarball 
<blockquote>
<a href="tree-VERSION.tar.gz">tree-VERSION.tar.gz</a></blockquote>  
Feel free to copy the
header <a href="tree-VERSION/tree.hh">tree.hh</a> (which is all you need code-wise) into your own
source directory as long as you respect the license (see above). 
The list of changes can be found in the <a href="ChangeLog">ChangeLog</a>.</div>

<div class="text">See the intro above for links to the documentation. There is a very
simple demonstration program available, <a
href="tree-VERSION/tree_example.cc">tree_example.cc</a> (also included in the tarball),
which is discussed <a href="#example">below</a>. There is also a small
test program, <a href="tree-VERSION/test_tree.cc">test_tree.cc</a>, which makes use
of the <a
href="tree-VERSION/tree_util.hh">tree_util.hh</a> utility functions by
Linda Buisman; the output
should be exactly identical to the <a
href="tree-VERSION/test_tree.output">test_tree.output</a> file.</div>

<div class="text">The current version works with GNU gcc 3.x and
higher, Borland&nbsp;C++&nbsp;builder and Microsoft Visual C++ 7.1 and
higher. It is compatible with STLport.</div>

<div class="text">Tony Cook has provided a version for the buggy Microsoft Visual C++
compilers up to version 7.0. You will have to use a special
version of the header file, <a href="tree-VERSION/tree_msvc.hh">tree_msvc.hh</a>
(currently based on the original tree.hh version
<strong>MSVC</strong>). The difference is that all members of the
iterator and sibling_iterator subclasses have been moved inside the
class definitions. If you get unresolved symbols in the linking phase,
or other weird compiler errors, you should use this header. Microsoft
users are urged to upgrade to version 7.1 which works with tree.hh out
of the box.</div>

<h2>Mailing list and update announcements</h2>

<div class="text">There is no mailing list, but I can send you an email whenever a new
version of tree.hh becomes available. Just send me an email at 
kasper.peeters&nbsp;(at)&nbsp;aei.mpg.de
and ask me to put you on the distribution list. </div>

<div class="text">I also announce major updates on <a
href="http://www.freshmeat.net">Freshmeat</a> though not as often as
by email.</div>

<h2>Projects using tree.hh</h2>

<div class="text">The <code>tree.hh</code> library is used in various projects:
<dl>
<dt><strong><a
href="../cadabra/">Cadabra</a></strong></dt>
<dd>A field-theory motivated approach to symbolic computer algebra.</dd>
<dt><strong><a href="http://www.echem.uni-tuebingen.de/~bs/echem/software/EChem++/echem++.shtml">EChem++</a></strong></dt>
<dd>A project realizing the idea of a Problem Solving Environment
(PSE) in the field of computational electrochemistry.  Computer
controlled experimental measurements, numerical simulation and
analysis of electrochemical processes will be combined under a common
user interface.</dd>
<dt><strong><a
href="http://www.infor.uva.es/~jadiego/">LZCS</a></strong></dt>
<dd>A semistructured document transformation tool. LZCS compresses
structured documents taking advantage of the redundant information
that can appear in the structure. The main idea is that frequently
repeated subtrees may exist and these can be replaced by a backward
reference to their first occurance. See the <a
href="http://www.dcc.uchile.cl/~gnavarro/ps/dcc04.1.ps.gz">accompanying
paper</a> for more details.</dd>
<dt><strong><a href="http://libofx.sourceforge.net/">libOFX</a></strong></dt>
<dd>A parser and an API designed to allow applications to very easily support OFX
command responses, usually provided by financial institutions for
statement downloads.</dd>
<dt><strong>A genetic programming project</strong></dt>
<dd>See this <a
href="http://www.cs.adfa.edu.au/~shanyin/publications/peel.pdf">paper</a> for
more information.</dd>
<dt><strong><a href="http://garraf.epsevg.upc.es/freeling/">FreeLing</a></strong></dt>
<dd> The FreeLing package consists of a library providing language analysis services (such as morfological analysis, date recognition, PoS tagging, etc.)</dd>
</dl>
Let me know about your project when you are using
<code>tree.hh</code>, so that I can add it to the list.</div>

<a name="example"></a>
<h2>Simple example</h2> 

<div class="text">The following program constructs a tree of std::string nodes, puts some content in
it and applies the <code>find</code> algorithm to find the node with content
"two". It then prints the content of all the children of this node.
You can download the source <a href="tree-VERSION/tree_example.cc">tree_example.cc</a> if you're too
lazy to type it in.
<blockquote><pre>
#include &lt;algorithm&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include "tree.hh"

using namespace std;

int main(int, char **)
   {
   tree&lt;string&gt; tr;
   tree&lt;string&gt;::iterator top, one, two, loc, banana;

   top=tr.begin();
   one=tr.insert(top, "one");
   two=tr.append_child(one, "two");
   tr.append_child(two, "apple");
   banana=tr.append_child(two, "banana");
   tr.append_child(banana,"cherry");
   tr.append_child(two, "peach");
   tr.append_child(one,"three");

   loc=find(tr.begin(), tr.end(), "two");
   if(loc!=tr.end()) {
      tree&lt;string&gt;::sibling_iterator sib=tr.begin(loc);
      while(sib!=tr.end(loc)) {
         cout &lt;&lt; (*sib) &lt;&lt; endl;
         ++sib;
         }
      cout &lt;&lt; endl;
      tree&lt;string&gt;::iterator sib2=tr.begin(loc);
      tree&lt;string&gt;::iterator end2=tr.end(loc);
      while(sib2!=end2) {
         for(int i=0; i&lt;tr.depth(sib2)-2; ++i) 
            cout &lt;&lt; " ";
         cout &lt;&lt; (*sib2) &lt;&lt; endl;
         ++sib2;
         }
      }
   }
</pre></blockquote>
The output of this program is
<blockquote><pre>
apple
banana
peach

apple
banana
 cherry
peach
</pre></blockquote>
Note that this example only has one element at the top of the tree (in
this case that is the node containing "one") but it is possible to
have an arbitary number of such elements (then the tree is more like a
"bush"). Observe the way in which the two types of iterators work. The
first block of output, obtained using the sibling_iterator, only
displays the children directly below "two". The second block iterates
over all children at any depth below "two". In the second output
block, the <code>depth</code> member has been used to determine the
distance of a given node to the root of the tree.  </div>

<h2>Data structure</h2> 

<div class="text">The data structure of the <code>tree</code> class is depicted
below (see the documentation for more detailed information). 
Each node contains a pointer to the first and last child element,
and each child contains pointers to its previous and next sibling:
<blockquote><small><pre>
           first_child        first_child 
 root_node-+----------node--+----->-------node
           |           |    |               |   
           |           |    |               V   next_sibling
           |           |    |               |
                       |    |             node
                       |    |               |
                       |    |               V   next_sibling
                       |    | last_child    |
                       |    +----->-------node
                       |                        
                       V next_sibling           
                       |                       
                       |     first_child                  
                      node--+----->-------node
                       |    |               |   
                       |    |               V   next_sibling
                       |    |               |
                       |    +-------------node
                       .
                       .</pre></small></blockquote>
Iterators come in two types. The normal <code>iterator</code> iterates depth-first
over all nodes. The beginning and end of the tree can be obtained by using the
<code>begin()</code> and <code>end()</code> members. The other type of iterator
only iterates over the nodes at one given depth (ie. over all siblings). One
typically uses these iterators to iterate over all children of a node, in which
case the [begin,end) range can be obtained by calling <code>begin(iterator)</code>
and <code>end(iterator)</code>.</div>

<div class="text">Iterators can be converted from one type to the other; this includes the `end' 
iterators (all intervals are as usual closed at the beginning and open
at the end).</div>

<hr noshade size=1>
<p align=right><!-- Begin Nedstat Basic code -->
<!-- Title: tree.hh stl-like container class -->
<!-- URL: http://www.aei.mpg.de/~peekas/tree/ -->
<a target="_blank" href="http://www.nedstatbasic.net/stats?ACQzpQ2TGcFmLelb10QwJ0nRa7UQ"><img
src="http://m1.nedstatbasic.net/n?id=ACQzpQ2TGcFmLelb10QwJ0nRa7UQ"
border="0" width="18" height="18"
alt="Webstats4U - Free web site statistics
Personal homepage website counter"></a>
<!-- End Nedstat Basic code --> $Id: newtree.html,v 1.1 2007/08/21 08:47:14 peekas Exp $
</body>
</html>