1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.structures.tree;
25
26
27 import java.util.HashMap;
28
29
30 /***
31 * ItemsTree class.
32 *
33 * @author Luca Fossato
34 */
35 public abstract class ItemTree extends BinaryTree
36 {
37 /***
38 * Node hashMap.
39 * <br>
40 * Every map entry references a tree node.
41 */
42 protected HashMap map;
43
44 /***
45 * set to true to navigate all the nodes; to false to
46 * navigate only the open nodes;
47 */
48 protected boolean traverseAllNodes;
49
50
51 /***
52 * Constructor for the ItemsTree object
53 */
54 public ItemTree()
55 {
56 super();
57 map = new HashMap();
58 traverseAllNodes = false;
59 }
60
61
62 /***
63 * Sets the traverseAllNodes attribute of the ItemTree object
64 *
65 * @param traverseAllNodes The new traverseAllNodes value
66 */
67 public void setTraverseAllNodes(boolean traverseAllNodes)
68 {
69 this.traverseAllNodes = traverseAllNodes;
70 }
71
72
73 /***
74 * Gets the traverseAllNodes attribute of the ItemTree object
75 *
76 * @return The traverseAllNodes value
77 */
78 public boolean isTraverseAllNodes()
79 {
80 return traverseAllNodes;
81 }
82
83
84 /***
85 * Move the <code>c</code> node identified by the input <code>pId</code>
86 * indentifier over the new parent <code>p</code> node.
87 *
88 * @param pId the identifier of the parent <code>c</code> node
89 * @param id the identifier of the <code>c</code> node to move
90 * @param left true if the <code>c</code> node must be inserted
91 * as left node of <code>p</code>;
92 * false if <code>c</code> must be inserted as right
93 * node of <code>p</code>
94 */
95 public void move(String pId, String id, boolean left)
96 {
97 Node p = getNode(pId);
98 Node c = getNode(id);
99
100 move(p, c, left);
101 }
102
103
104 /***
105 * Remove the input node from the tree, rearranging
106 * the parent and the child nodes references.
107 *
108 * @param n the node to remove.
109 */
110 protected void remove(Node n)
111 {
112 synchronized (map)
113 {
114 map.remove(n.getId());
115 }
116
117 super.remove(n);
118 }
119
120
121 /***
122 * Get the node referenced by the input id.
123 *
124 * @param id the node id
125 * @return the node referenced by the input id, or null
126 * if that id doesn't match any node stored into
127 * the node map
128 */
129 public Node getNode(String id)
130 {
131 return (map.containsKey(id)) ? (Node) map.get(id) : null;
132 }
133
134
135 /***
136 * Traverse the tree, using pre-order algorithm,
137 * starting from the input node; visit only open nodes.
138 * <br>
139 * pre-order traversal: first looks at the data,
140 * then the left nodes, and then the right nodes.
141 *
142 * @param p the node where to start the tree traversing
143 * @exception Exception if any error occurs
144 */
145 protected void pretrav(Node p) throws Exception
146 {
147 if (p == null) return;
148
149 visit(p);
150 Node l = p.getLeft();
151
152
153 if (traverseAllNodes || p.isOpen())
154 pretrav(l);
155
156 pretrav(p.getRight());
157 }
158
159
160 /***
161 * Insert a new left node into the tree.
162 *
163 * @param p the parent node where to insert the new left node
164 * @param c the new left node to add to the parent node
165 * @return true if the insertion is successfull;
166 * false otherwise
167 */
168 public boolean insertLeft(Node p, Node c)
169 {
170 boolean inserted = false;
171
172 if ((inserted = super.insertLeft(p, c)))
173 map.put(c.getId(), c);
174
175 return inserted;
176 }
177
178
179 /***
180 * Insert a new right node into the tree.
181 *
182 * @param p the parent node where to insert the new right node
183 * @param c the new right node to add to the parent node
184 * @return true if the insertion is successfull;
185 * false otherwise
186 */
187 public boolean insertRight(Node p, Node c)
188 {
189 boolean inserted = false;
190
191 if ((inserted = super.insertRight(p, c)))
192 map.put(c.getId(), c);
193
194 return inserted;
195 }
196
197
198 /***
199 * Add a new left node to the parent node left node list.
200 *
201 * @param p the parent node
202 * @param c the left node to add to the parent node left node list
203 * @return true if the addition is successfull;
204 * false otherwise
205 */
206 public boolean addLeft(Node p, Node c)
207 {
208 boolean added = false;
209
210 if ((added = super.addLeft(p, c)))
211 map.put(c.getId(), c);
212
213 return added;
214 }
215
216
217 /***
218 * Add a new right node to the parent node right node list.
219 *
220 * @param p the parent node
221 * @param c the right node to add to the parent node right node list
222 * @return true if the addition is successfull;
223 * false otherwise
224 */
225 public boolean addRight(Node p, Node c)
226 {
227 boolean added = false;
228
229 if ((added = super.addRight(p, c)))
230 map.put(c.getId(), c);
231
232 return added;
233 }
234
235
236 /***
237 // * Collapse the tree.
238 // *
239 // * Use this method to set
240 // */
241
242
243
244
245
246
247 /***
248 // * Collapse the tree.
249 // *
250 // * @param n the input itemNode.
251 // */
252
253
254
255
256
257
258
259
260 }