Tree io modifications, including support for polytomous trees by the
[jalview.git] / src / jalview / datamodel / BinaryNode.java
1 package jalview.datamodel;
2
3 public class BinaryNode
4 {
5
6   Object element;
7   String name;
8   BinaryNode left;
9   BinaryNode right;
10   BinaryNode parent;
11   public int bootstrap;
12
13   public BinaryNode()
14   {
15     left = right = parent = null;
16     bootstrap = 0;
17   }
18
19   public BinaryNode(Object element, BinaryNode parent, String name)
20   {
21     this.element = element;
22     this.parent = parent;
23     this.name = name;
24
25     left = right = null;
26   }
27
28   public Object element()
29   {
30     return element;
31   }
32
33   public Object setElement(Object v)
34   {
35     return element = v;
36   }
37
38   public BinaryNode left()
39   {
40     return left;
41   }
42
43   public BinaryNode setLeft(BinaryNode n)
44   {
45     return left = n;
46   }
47
48   public BinaryNode right()
49   {
50     return right;
51   }
52
53   public BinaryNode setRight(BinaryNode n)
54   {
55     return right = n;
56   }
57
58   public BinaryNode parent()
59   {
60     return parent;
61   }
62
63   public BinaryNode setParent(BinaryNode n)
64   {
65     return parent = n;
66   }
67
68   public boolean isLeaf()
69   {
70     return (left == null) && (right == null);
71   }
72
73   /**
74    * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of this node (removing any old references)
75    * a null parameter DOES NOT mean that the pointer to the corresponding child node is set to  NULL - you  should use
76    * setChild(null), or detach() for this.
77    *
78    */
79   public void SetChildren(BinaryNode leftchild, BinaryNode rightchild)
80   {
81     if (leftchild != null)
82     {
83       this.setLeft(leftchild);
84       leftchild.detach();
85       leftchild.setParent(this);
86
87     }
88     if (rightchild != null)
89     {
90       this.setRight(rightchild);
91       rightchild.detach();
92       rightchild.setParent(this);
93     }
94   }
95
96   /**
97    * Detaches the node from the binary tree, along with all its child nodes.
98    * @return BinaryNode The detached node.
99    */
100   public BinaryNode detach()
101   {
102     if (this.parent!=null) {
103       if (this.parent.left == this)
104       {
105         this.parent.left = null;
106       }
107       else
108       {
109         if (this.parent.right == this)
110         {
111           this.parent.right = null;
112         }
113       }
114     }
115     this.parent = null;
116     return this;
117   }
118   /**
119    * Traverses up through the tree until a node with a free leftchild is discovered.
120    * @return BinaryNode
121    */
122   public BinaryNode ascendLeft() {
123     BinaryNode c = this;
124     do {
125       c = c.parent();
126     }  while (c!=null && c.left()!=null && !c.left().isLeaf());
127     return c;
128   }
129   /**
130    * Traverses up through the tree until a node with a free rightchild is discovered.
131    * Jalview builds trees by descent on the left, so this may be unused.
132    * @return BinaryNode
133    */
134   public BinaryNode ascendRight() {
135     BinaryNode c = this;
136     do {
137       c = c.parent();
138     }  while (c!=null && c.right()!=null && !c.right().isLeaf());
139     return c;
140   }
141
142
143   public void setName(String name)
144   {
145     this.name = name;
146   }
147
148   public String getName()
149   {
150     return this.name;
151   }
152
153   public void setBootstrap(int boot)
154   {
155     this.bootstrap = boot;
156   }
157
158   public int getBootstrap()
159   {
160     return bootstrap;
161   }
162 }