d5c2bb6d35132e4468225d39f32db1f58acc4b8f
[jalview.git] / forester / java / src / org / forester / test / examples / Example3.java
1 // $Id:
2 //
3 // forester -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2011 Christian M. Zmasek
7 // Copyright (C) 2008-2011 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library 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 GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.test.examples;
28
29 import org.forester.phylogeny.Phylogeny;
30 import org.forester.phylogeny.PhylogenyNode;
31 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
32
33 public class Example3 {
34
35     public static void main( final String[] args ) {
36         // Creating a new rooted tree with four external nodes.
37         final Phylogeny phy = new Phylogeny();
38         final PhylogenyNode root = new PhylogenyNode();
39         final PhylogenyNode d1 = new PhylogenyNode();
40         final PhylogenyNode d2 = new PhylogenyNode();
41         final PhylogenyNode d11 = new PhylogenyNode();
42         final PhylogenyNode d12 = new PhylogenyNode();
43         root.setName( "root" );
44         d1.setName( "1" );
45         d2.setName( "2" );
46         d11.setName( "1-1" );
47         d12.setName( "1-2" );
48         root.addAsChild( d1 );
49         root.addAsChild( d2 );
50         d2.addAsChild( d11 );
51         d2.addAsChild( d12 );
52         phy.setRoot( root );
53         phy.setRooted( true );
54         // Using a variety of iterators to visit the nodes of the newly created tree.
55         System.out.println( "post-order:" );
56         for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
57             System.out.println( it.next().getName() );
58         }
59         System.out.println( "pre-order:" );
60         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
61             System.out.println( it.next().getName() );
62         }
63         System.out.println( "level-order:" );
64         for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
65             System.out.println( it.next().getName() );
66         }
67         System.out.println( "external nodes only:" );
68         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
69             System.out.println( it.next().getName() );
70         }
71     }
72 }