package jalview.datamodel; public class BinaryNode { Object element; String name; BinaryNode left; BinaryNode right; BinaryNode parent; public int bootstrap; public BinaryNode() { left = right = parent = null; bootstrap = 0; } public BinaryNode(Object element, BinaryNode parent, String name) { this.element = element; this.parent = parent; this.name = name; left = right = null; } public Object element() { return element; } public Object setElement(Object v) { return element = v; } public BinaryNode left() { return left; } public BinaryNode setLeft(BinaryNode n) { return left = n; } public BinaryNode right() { return right; } public BinaryNode setRight(BinaryNode n) { return right = n; } public BinaryNode parent() { return parent; } public BinaryNode setParent(BinaryNode n) { return parent = n; } public boolean isLeaf() { return (left == null) && (right == null); } /** * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of this node (removing any old references) * a null parameter DOES NOT mean that the pointer to the corresponding child node is set to NULL - you should use * setChild(null), or detach() for this. * */ public void SetChildren(BinaryNode leftchild, BinaryNode rightchild) { if (leftchild != null) { this.setLeft(leftchild); leftchild.detach(); leftchild.setParent(this); } if (rightchild != null) { this.setRight(rightchild); rightchild.detach(); rightchild.setParent(this); } } /** * Detaches the node from the binary tree, along with all its child nodes. * @return BinaryNode The detached node. */ public BinaryNode detach() { if (this.parent!=null) { if (this.parent.left == this) { this.parent.left = null; } else { if (this.parent.right == this) { this.parent.right = null; } } } this.parent = null; return this; } /** * Traverses up through the tree until a node with a free leftchild is discovered. * @return BinaryNode */ public BinaryNode ascendLeft() { BinaryNode c = this; do { c = c.parent(); } while (c!=null && c.left()!=null && !c.left().isLeaf()); return c; } /** * Traverses up through the tree until a node with a free rightchild is discovered. * Jalview builds trees by descent on the left, so this may be unused. * @return BinaryNode */ public BinaryNode ascendRight() { BinaryNode c = this; do { c = c.parent(); } while (c!=null && c.right()!=null && !c.right().isLeaf()); return c; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setBootstrap(int boot) { this.bootstrap = boot; } public int getBootstrap() { return bootstrap; } }