-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.pde
More file actions
63 lines (56 loc) · 1.59 KB
/
Tree.pde
File metadata and controls
63 lines (56 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* This class represents an element of a Tree.
*/
public class Tree {
int estimation;
IVector value;
ArrayList<Tree> nodes;
public Tree(IVector vector, int estimation) {
this.value = vector;
this.nodes = new ArrayList<Tree>();
this.estimation = estimation;
}
public Tree(IVector vector) {
this.value = vector;
this.nodes = new ArrayList<Tree>();
}
public Tree() {
this(new IVector(0, 0), 0);
}
/**
* Adds a node to the list of nodes.
*/
public Tree addNode(IVector newVector, int estimation) {
final Tree tree = new Tree(newVector, estimation);
this.nodes.add(tree);
return tree;
}
/**
* Adds a node to the list of nodes.
*/
public Tree addNode(Tree node) {
this.nodes.add(node);
return node;
}
/**
* Recurcively checks if the given position exists within the tree formed
* by the current node and his direct and undirect sons.
*/
public boolean contains(IVector position) {
if (this.value.iEquals(position)) return true;
boolean returnValue = false;
for (Tree tree : nodes) {
returnValue = returnValue || tree.contains(position);
if (returnValue == true) return true;
}
return returnValue || false;
}
/**
* Checks the equality between the current object and the given Tree element, returns true if
* they have equal values for the value property, false otherwise.
*/
public boolean iEquals(Tree tree) {
if (tree == null) return false;
return this.value.iEquals(tree.value);
}
}