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