update author list in license for (JAL-826)
[jalview.git] / src / jalview / datamodel / BinaryNode.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.datamodel;
19
20 /**
21  * DOCUMENT ME!
22  * 
23  * @author $author$
24  * @version $Revision$
25  */
26 public class BinaryNode
27 {
28   Object element;
29
30   String name;
31
32   BinaryNode left;
33
34   BinaryNode right;
35
36   BinaryNode parent;
37
38   /** DOCUMENT ME!! */
39   public int bootstrap;
40
41   /**
42    * Creates a new BinaryNode object.
43    */
44   public BinaryNode()
45   {
46     left = right = parent = null;
47     bootstrap = 0;
48   }
49
50   /**
51    * Creates a new BinaryNode object.
52    * 
53    * @param element
54    *          DOCUMENT ME!
55    * @param parent
56    *          DOCUMENT ME!
57    * @param name
58    *          DOCUMENT ME!
59    */
60   public BinaryNode(Object element, BinaryNode parent, String name)
61   {
62     this.element = element;
63     this.parent = parent;
64     this.name = name;
65
66     left = right = null;
67   }
68
69   /**
70    * DOCUMENT ME!
71    * 
72    * @return DOCUMENT ME!
73    */
74   public Object element()
75   {
76     return element;
77   }
78
79   /**
80    * DOCUMENT ME!
81    * 
82    * @param v
83    *          DOCUMENT ME!
84    * 
85    * @return DOCUMENT ME!
86    */
87   public Object setElement(Object v)
88   {
89     return element = v;
90   }
91
92   /**
93    * DOCUMENT ME!
94    * 
95    * @return DOCUMENT ME!
96    */
97   public BinaryNode left()
98   {
99     return left;
100   }
101
102   /**
103    * DOCUMENT ME!
104    * 
105    * @param n
106    *          DOCUMENT ME!
107    * 
108    * @return DOCUMENT ME!
109    */
110   public BinaryNode setLeft(BinaryNode n)
111   {
112     return left = n;
113   }
114
115   /**
116    * DOCUMENT ME!
117    * 
118    * @return DOCUMENT ME!
119    */
120   public BinaryNode right()
121   {
122     return right;
123   }
124
125   /**
126    * DOCUMENT ME!
127    * 
128    * @param n
129    *          DOCUMENT ME!
130    * 
131    * @return DOCUMENT ME!
132    */
133   public BinaryNode setRight(BinaryNode n)
134   {
135     return right = n;
136   }
137
138   /**
139    * DOCUMENT ME!
140    * 
141    * @return DOCUMENT ME!
142    */
143   public BinaryNode parent()
144   {
145     return parent;
146   }
147
148   /**
149    * DOCUMENT ME!
150    * 
151    * @param n
152    *          DOCUMENT ME!
153    * 
154    * @return DOCUMENT ME!
155    */
156   public BinaryNode setParent(BinaryNode n)
157   {
158     return parent = n;
159   }
160
161   /**
162    * DOCUMENT ME!
163    * 
164    * @return DOCUMENT ME!
165    */
166   public boolean isLeaf()
167   {
168     return (left == null) && (right == null);
169   }
170
171   /**
172    * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of
173    * this node (removing any old references) a null parameter DOES NOT mean that
174    * the pointer to the corresponding child node is set to NULL - you should use
175    * setChild(null), or detach() for this.
176    * 
177    */
178   public void SetChildren(BinaryNode leftchild, BinaryNode rightchild)
179   {
180     if (leftchild != null)
181     {
182       this.setLeft(leftchild);
183       leftchild.detach();
184       leftchild.setParent(this);
185     }
186
187     if (rightchild != null)
188     {
189       this.setRight(rightchild);
190       rightchild.detach();
191       rightchild.setParent(this);
192     }
193   }
194
195   /**
196    * Detaches the node from the binary tree, along with all its child nodes.
197    * 
198    * @return BinaryNode The detached node.
199    */
200   public BinaryNode detach()
201   {
202     if (this.parent != null)
203     {
204       if (this.parent.left == this)
205       {
206         this.parent.left = null;
207       }
208       else
209       {
210         if (this.parent.right == this)
211         {
212           this.parent.right = null;
213         }
214       }
215     }
216
217     this.parent = null;
218
219     return this;
220   }
221
222   /**
223    * Traverses up through the tree until a node with a free leftchild is
224    * discovered.
225    * 
226    * @return BinaryNode
227    */
228   public BinaryNode ascendLeft()
229   {
230     BinaryNode c = this;
231
232     do
233     {
234       c = c.parent();
235     } while ((c != null) && (c.left() != null) && !c.left().isLeaf());
236
237     return c;
238   }
239
240   /**
241    * Traverses up through the tree until a node with a free rightchild is
242    * discovered. Jalview builds trees by descent on the left, so this may be
243    * unused.
244    * 
245    * @return BinaryNode
246    */
247   public BinaryNode ascendRight()
248   {
249     BinaryNode c = this;
250
251     do
252     {
253       c = c.parent();
254     } while ((c != null) && (c.right() != null) && !c.right().isLeaf());
255
256     return c;
257   }
258
259   /**
260    * 
261    * set the display name
262    * 
263    * @param new name
264    */
265   public void setName(String name)
266   {
267     this.name = name;
268   }
269
270   /**
271    * 
272    * 
273    * @return the display name for this node
274    */
275   public String getName()
276   {
277     return this.name;
278   }
279
280   /**
281    * set integer bootstrap value
282    * 
283    * @param boot
284    */
285   public void setBootstrap(int boot)
286   {
287     this.bootstrap = boot;
288   }
289
290   /**
291    * get bootstrap
292    * 
293    * @return integer value
294    */
295   public int getBootstrap()
296   {
297     return bootstrap;
298   }
299 }