GPL license added
[jalview.git] / src / jalview / datamodel / BinaryNode.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19
20 package jalview.datamodel;
21
22 public class BinaryNode
23 {
24
25   Object element;
26   String name;
27   BinaryNode left;
28   BinaryNode right;
29   BinaryNode parent;
30   public int bootstrap;
31
32   public BinaryNode()
33   {
34     left = right = parent = null;
35     bootstrap = 0;
36   }
37
38   public BinaryNode(Object element, BinaryNode parent, String name)
39   {
40     this.element = element;
41     this.parent = parent;
42     this.name = name;
43
44     left = right = null;
45   }
46
47   public Object element()
48   {
49     return element;
50   }
51
52   public Object setElement(Object v)
53   {
54     return element = v;
55   }
56
57   public BinaryNode left()
58   {
59     return left;
60   }
61
62   public BinaryNode setLeft(BinaryNode n)
63   {
64     return left = n;
65   }
66
67   public BinaryNode right()
68   {
69     return right;
70   }
71
72   public BinaryNode setRight(BinaryNode n)
73   {
74     return right = n;
75   }
76
77   public BinaryNode parent()
78   {
79     return parent;
80   }
81
82   public BinaryNode setParent(BinaryNode n)
83   {
84     return parent = n;
85   }
86
87   public boolean isLeaf()
88   {
89     return (left == null) && (right == null);
90   }
91
92   /**
93    * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of this node (removing any old references)
94    * a null parameter DOES NOT mean that the pointer to the corresponding child node is set to  NULL - you  should use
95    * setChild(null), or detach() for this.
96    *
97    */
98   public void SetChildren(BinaryNode leftchild, BinaryNode rightchild)
99   {
100     if (leftchild != null)
101     {
102       this.setLeft(leftchild);
103       leftchild.detach();
104       leftchild.setParent(this);
105
106     }
107     if (rightchild != null)
108     {
109       this.setRight(rightchild);
110       rightchild.detach();
111       rightchild.setParent(this);
112     }
113   }
114
115   /**
116    * Detaches the node from the binary tree, along with all its child nodes.
117    * @return BinaryNode The detached node.
118    */
119   public BinaryNode detach()
120   {
121     if (this.parent!=null) {
122       if (this.parent.left == this)
123       {
124         this.parent.left = null;
125       }
126       else
127       {
128         if (this.parent.right == this)
129         {
130           this.parent.right = null;
131         }
132       }
133     }
134     this.parent = null;
135     return this;
136   }
137   /**
138    * Traverses up through the tree until a node with a free leftchild is discovered.
139    * @return BinaryNode
140    */
141   public BinaryNode ascendLeft() {
142     BinaryNode c = this;
143     do {
144       c = c.parent();
145     }  while (c!=null && c.left()!=null && !c.left().isLeaf());
146     return c;
147   }
148   /**
149    * Traverses up through the tree until a node with a free rightchild is discovered.
150    * Jalview builds trees by descent on the left, so this may be unused.
151    * @return BinaryNode
152    */
153   public BinaryNode ascendRight() {
154     BinaryNode c = this;
155     do {
156       c = c.parent();
157     }  while (c!=null && c.right()!=null && !c.right().isLeaf());
158     return c;
159   }
160
161
162   public void setName(String name)
163   {
164     this.name = name;
165   }
166
167   public String getName()
168   {
169     return this.name;
170   }
171
172   public void setBootstrap(int boot)
173   {
174     this.bootstrap = boot;
175   }
176
177   public int getBootstrap()
178   {
179     return bootstrap;
180   }
181 }