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