-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.java
144 lines (116 loc) · 3.27 KB
/
TreeNode.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* This code comes from https://github.com/vivin/GenericTree
* It is used as the generic node for a subset tree
*/
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TreeNode<T> {
public T data;
public T indices;
public int limit;
public List<TreeNode<T>> children;
public TreeNode() {
super();
children = new ArrayList<TreeNode<T>>();
}
public TreeNode(T data) {
this();
setData(data);
}
public List<TreeNode<T>> getChildren() {
return this.children;
}
public int getNumberOfChildren() {
return getChildren().size();
}
public boolean hasChildren() {
return (getNumberOfChildren() > 0);
}
public void setChildren(List<TreeNode<T>> children) {
/*for ( TreeNode<T> child : children ) {
TreeNode<T> newChild = new TreeNode<T>( child.getData( ) );
newChild.setIndices( child.getIndices( ) );
newChild.setLimit( child.getLimit( ) );
newChild.setChildren( child.getChildren( ) );
children.add( newChild );
}*/
this.children = children;
}
public void addChild(TreeNode<T> child) {
children.add(child);
}
public void addChildAt(int index, TreeNode<T> child) throws IndexOutOfBoundsException {
children.add(index, child);
}
public void removeChildren() {
this.children = new ArrayList<TreeNode<T>>();
}
public void removeChildAt(int index) throws IndexOutOfBoundsException {
children.remove(index);
}
public TreeNode<T> getChildAt(int index) throws IndexOutOfBoundsException {
return children.get(index);
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public T getIndices() {
return this.indices;
}
public void setIndices(T indices) {
this.indices = indices;
}
public int getLimit() {
return this.limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public String toString() {
return getData().toString();
}
public boolean equals(TreeNode<T> node) {
return node.getData().equals(getData());
}
public int hashCode() {
return getData().hashCode();
}
public String toStringVerbose() {
String stringRepresentation = getData().toString() + ":[";
for (TreeNode<T> node : getChildren()) {
stringRepresentation += node.toStringVerbose() + ", ";
}
//Pattern.DOTALL causes ^ and $ to match
Pattern pattern = Pattern.compile(", $", Pattern.DOTALL);
Matcher matcher = pattern.matcher(stringRepresentation);
stringRepresentation = matcher.replaceFirst("");
stringRepresentation += "]";
return stringRepresentation;
}
}
@SuppressWarnings("unchecked")
class NodeComparator implements Comparator {
public int compare( Object o1, Object o2 ) {
List<Integer> list1 = (List<Integer>)(((TreeNode)o1).getData());
List<Integer> list2 = (List<Integer>)(((TreeNode)o2).getData());
int sum1 = 0;
int sum2 = 0;
for ( int i = 0; i < list1.size(); ++i ) {
sum1 += list1.get( i );
sum2 += list2.get( i );
}
if ( sum1 > sum2 ) {
return 1;
}
else if ( sum1 < sum2 ) {
return -1;
}
return 0;
}
}