2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import java.awt.Color;
26 * Represent a node in a binary tree
28 * @author $mclamp (probably!)$
31 public class BinaryNode<T>
43 /** Bootstrap value */
59 public Color color = Color.black;
62 * if true, node is created to simulate polytomy between parent and its 3 or
65 public boolean dummy = false;
68 * Creates a new BinaryNode object.
72 left = right = parent = null;
78 * Creates a new BinaryNode object.
87 public BinaryNode(T element, BinaryNode<T> parent, String name,
91 this.element = element;
97 public BinaryNode(T element, BinaryNode<T> parent, String name,
98 double dist, int bootstrap)
100 this(element, parent, name, dist);
101 this.bootstrap = bootstrap;
104 public BinaryNode(T val, BinaryNode<T> parent, String name, double dist,
105 int bootstrap, boolean dummy)
107 this(val, parent, name, dist, bootstrap);
114 * @return DOCUMENT ME!
127 * @return DOCUMENT ME!
129 public T setElement(T v)
137 * @return DOCUMENT ME!
139 public BinaryNode<T> left()
150 * @return DOCUMENT ME!
152 public BinaryNode<T> setLeft(BinaryNode<T> n)
160 * @return DOCUMENT ME!
162 public BinaryNode<T> right()
173 * @return DOCUMENT ME!
175 public BinaryNode<T> setRight(BinaryNode<T> n)
183 * @return DOCUMENT ME!
185 public BinaryNode<T> parent()
196 * @return DOCUMENT ME!
198 public BinaryNode<T> setParent(BinaryNode<T> n)
206 * @return DOCUMENT ME!
208 public boolean isLeaf()
210 return (left == null) && (right == null);
214 * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of
215 * this node (removing any old references) a null parameter DOES NOT mean that
216 * the pointer to the corresponding child node is set to NULL - you should use
217 * setChild(null), or detach() for this.
220 public void SetChildren(BinaryNode<T> leftchild, BinaryNode<T> rightchild)
222 if (leftchild != null)
224 this.setLeft(leftchild);
226 leftchild.setParent(this);
229 if (rightchild != null)
231 this.setRight(rightchild);
233 rightchild.setParent(this);
238 * Detaches the node from the binary tree, along with all its child nodes.
240 * @return BinaryNode The detached node.
242 public BinaryNode<T> detach()
244 if (this.parent != null)
246 if (this.parent.left == this)
248 this.parent.left = null;
252 if (this.parent.right == this)
254 this.parent.right = null;
265 * Traverses up through the tree until a node with a free leftchild is
270 public BinaryNode<T> ascendLeft()
272 BinaryNode<T> c = this;
277 } while ((c != null) && (c.left() != null) && !c.left().isLeaf());
283 * Traverses up through the tree until a node with a free rightchild is
284 * discovered. Jalview builds trees by descent on the left, so this may be
289 public BinaryNode<T> ascendRight()
291 BinaryNode<T> c = this;
296 } while ((c != null) && (c.right() != null) && !c.right().isLeaf());
303 * set the display name
308 public void setName(String name)
316 * @return the display name for this node
318 public String getName()
324 * set integer bootstrap value
328 public void setBootstrap(int boot)
330 this.bootstrap = boot;
336 * @return integer value
338 public int getBootstrap()
345 * true if node is created for the representation of polytomous trees
347 public boolean isDummy()
358 * @return DOCUMENT ME!
360 public boolean setDummy(boolean newstate)
362 boolean oldstate = dummy;
369 * ascends the tree but doesn't stop until a non-dummy node is discovered.
372 public BinaryNode<T> AscendTree()
374 BinaryNode<T> c = this;
379 } while ((c != null) && c.dummy);