JAL-1551 spotlessApply
[jalview.git] / src / jalview / datamodel / BinaryNode.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import java.awt.Color;
24
25 /**
26  * DOCUMENT ME!
27  * 
28  * @author $author$
29  * @version $Revision$
30  */
31 public class BinaryNode
32 {
33   Object element;
34
35   String name;
36
37   BinaryNode left;
38
39   BinaryNode right;
40
41   BinaryNode parent;
42
43   /** Bootstrap value */
44   public int bootstrap;
45
46   /** DOCUMENT ME!! */
47   public double dist;
48
49   /** DOCUMENT ME!! */
50   public int count;
51
52   /** DOCUMENT ME!! */
53   public double height;
54
55   /** DOCUMENT ME!! */
56   public float ycount;
57
58   /** DOCUMENT ME!! */
59   public Color color = Color.black;
60
61   /** DOCUMENT ME!! */
62   public boolean dummy = false;
63
64   /**
65    * Creates a new BinaryNode object.
66    */
67   public BinaryNode()
68   {
69     left = right = parent = null;
70     bootstrap = 0;
71     dist = 0;
72   }
73
74   /**
75    * Creates a new BinaryNode object.
76    * 
77    * @param element
78    *          DOCUMENT ME!
79    * @param parent
80    *          DOCUMENT ME!
81    * @param name
82    *          DOCUMENT ME!
83    */
84   public BinaryNode(Object element, BinaryNode parent, String name,
85           double dist)
86   {
87     this();
88     this.element = element;
89     this.parent = parent;
90     this.name = name;
91     this.dist = dist;
92   }
93
94   public BinaryNode(Object element, BinaryNode parent, String name,
95           double dist, int bootstrap)
96   {
97     this(element, parent, name, dist);
98     this.bootstrap = bootstrap;
99   }
100
101   public BinaryNode(Object val, BinaryNode parent, String name, double dist,
102           int bootstrap, boolean dummy)
103   {
104     this(val, parent, name, dist, bootstrap);
105     this.dummy = dummy;
106   }
107
108   /**
109    * DOCUMENT ME!
110    * 
111    * @return DOCUMENT ME!
112    */
113   public Object element()
114   {
115     return element;
116   }
117
118   /**
119    * DOCUMENT ME!
120    * 
121    * @param v
122    *          DOCUMENT ME!
123    * 
124    * @return DOCUMENT ME!
125    */
126   public Object setElement(Object v)
127   {
128     return element = v;
129   }
130
131   /**
132    * DOCUMENT ME!
133    * 
134    * @return DOCUMENT ME!
135    */
136   public BinaryNode left()
137   {
138     return left;
139   }
140
141   /**
142    * DOCUMENT ME!
143    * 
144    * @param n
145    *          DOCUMENT ME!
146    * 
147    * @return DOCUMENT ME!
148    */
149   public BinaryNode setLeft(BinaryNode n)
150   {
151     return left = n;
152   }
153
154   /**
155    * DOCUMENT ME!
156    * 
157    * @return DOCUMENT ME!
158    */
159   public BinaryNode right()
160   {
161     return right;
162   }
163
164   /**
165    * DOCUMENT ME!
166    * 
167    * @param n
168    *          DOCUMENT ME!
169    * 
170    * @return DOCUMENT ME!
171    */
172   public BinaryNode setRight(BinaryNode n)
173   {
174     return right = n;
175   }
176
177   /**
178    * DOCUMENT ME!
179    * 
180    * @return DOCUMENT ME!
181    */
182   public BinaryNode parent()
183   {
184     return parent;
185   }
186
187   /**
188    * DOCUMENT ME!
189    * 
190    * @param n
191    *          DOCUMENT ME!
192    * 
193    * @return DOCUMENT ME!
194    */
195   public BinaryNode setParent(BinaryNode n)
196   {
197     return parent = n;
198   }
199
200   /**
201    * DOCUMENT ME!
202    * 
203    * @return DOCUMENT ME!
204    */
205   public boolean isLeaf()
206   {
207     return (left == null) && (right == null);
208   }
209
210   /**
211    * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of
212    * this node (removing any old references) a null parameter DOES NOT mean that
213    * the pointer to the corresponding child node is set to NULL - you should use
214    * setChild(null), or detach() for this.
215    * 
216    */
217   public void SetChildren(BinaryNode leftchild, BinaryNode rightchild)
218   {
219     if (leftchild != null)
220     {
221       this.setLeft(leftchild);
222       leftchild.detach();
223       leftchild.setParent(this);
224     }
225
226     if (rightchild != null)
227     {
228       this.setRight(rightchild);
229       rightchild.detach();
230       rightchild.setParent(this);
231     }
232   }
233
234   /**
235    * Detaches the node from the binary tree, along with all its child nodes.
236    * 
237    * @return BinaryNode The detached node.
238    */
239   public BinaryNode detach()
240   {
241     if (this.parent != null)
242     {
243       if (this.parent.left == this)
244       {
245         this.parent.left = null;
246       }
247       else
248       {
249         if (this.parent.right == this)
250         {
251           this.parent.right = null;
252         }
253       }
254     }
255
256     this.parent = null;
257
258     return this;
259   }
260
261   /**
262    * Traverses up through the tree until a node with a free leftchild is
263    * discovered.
264    * 
265    * @return BinaryNode
266    */
267   public BinaryNode ascendLeft()
268   {
269     BinaryNode c = this;
270
271     do
272     {
273       c = c.parent();
274     } while ((c != null) && (c.left() != null) && !c.left().isLeaf());
275
276     return c;
277   }
278
279   /**
280    * Traverses up through the tree until a node with a free rightchild is
281    * discovered. Jalview builds trees by descent on the left, so this may be
282    * unused.
283    * 
284    * @return BinaryNode
285    */
286   public BinaryNode ascendRight()
287   {
288     BinaryNode c = this;
289
290     do
291     {
292       c = c.parent();
293     } while ((c != null) && (c.right() != null) && !c.right().isLeaf());
294
295     return c;
296   }
297
298   /**
299    * 
300    * set the display name
301    * 
302    * @param new
303    *          name
304    */
305   public void setName(String name)
306   {
307     this.name = name;
308   }
309
310   /**
311    * 
312    * 
313    * @return the display name for this node
314    */
315   public String getName()
316   {
317     return this.name;
318   }
319
320   /**
321    * set integer bootstrap value
322    * 
323    * @param boot
324    */
325   public void setBootstrap(int boot)
326   {
327     this.bootstrap = boot;
328   }
329
330   /**
331    * get bootstrap
332    * 
333    * @return integer value
334    */
335   public int getBootstrap()
336   {
337     return bootstrap;
338   }
339
340   /**
341    * @param dummy
342    *          true if node is created for the representation of polytomous trees
343    */
344   public boolean isDummy()
345   {
346     return dummy;
347   }
348
349   /**
350    * DOCUMENT ME!
351    * 
352    * @param newstate
353    *          DOCUMENT ME!
354    * 
355    * @return DOCUMENT ME!
356    */
357   public boolean setDummy(boolean newstate)
358   {
359     boolean oldstate = dummy;
360     dummy = newstate;
361
362     return oldstate;
363   }
364
365   /**
366    * ascends the tree but doesn't stop until a non-dummy node is discovered.
367    * 
368    */
369   public BinaryNode AscendTree()
370   {
371     BinaryNode c = this;
372
373     do
374     {
375       c = c.parent();
376     } while ((c != null) && c.dummy);
377
378     return c;
379   }
380 }