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