-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
186 lines (153 loc) · 4.97 KB
/
Node.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//Did: getcost, implicit cost setter in constructor, implicit evaluation fctn setter in constr, getEval()
package eper8035;
import java.util.*;
public class Node {
//declare vars
//consider which of these should be static/protected/etc.
//!!!might still have leftover mess from changing imput from String data to int[] digits
private int[] digits;
private Node parent;
private ArrayList<Node> children;
//private ArrayList<Node> siblings;
private int depth;
//index (0,1,2) of last digit changed
//for 1st node (root) avoid this bit of code or set to -1 and catch exc
private int last_changed;
private boolean visited;
private int heuristic;
private int order_added;
private int cost;
private int eval;
//constructor
//could try-catch nullpointer for root but seems clunky
//!!!make input arg be the int[], not string, and translate into int[] in main program
public Node(Node parent, int[] digits, int last_changed, int heuristic, int order) {
this.digits = digits;
this.last_changed = last_changed;
this.heuristic = heuristic;
this.order_added = order;
this.visited = false;
//see if this works
//think: are there any other nodes w/ null parents
//apart from root? (eg. disconnected)
if (parent == null) {
this.depth = 0;
this.last_changed = -1;
this.cost = 0;
}
else {
this.depth = parent.getDepth() + 1;
this.parent = parent;
this.cost = parent.getCost() + 1;
}
this.eval = this.cost + this.heuristic;
//the idea is to not "arbitrarily" set depth when we create node, so not input arg
//but still having it as var so we don't have to traverse whole tree every time
//!this.depth = parent.getDepth() = 1;
//other?
}
//methods
public int[] getDigits() {
return digits;
}
public Node getParent() {
return parent;
}
//!!
// public ArrayList<Node> getChildren() {
//could throw nullpointer if we run before running generateChildren()
//fixes: try catch (if catch: run generate children). This is so we don't generate them twice every time
//honestly we don't even need this method
//but actually might be useful still
// return children;
// }
public int lastChanged() {
return last_changed;
}
//static? -> not but maybe when we set it yes
public int getDepth() {
return depth;
}
//consider an append children fctn also but prob not nec
//could make a Boolean, if not succesful returns false
public void setChildren(ArrayList<Node> childList) {
this.children = childList;
//do stuff
}
public ArrayList<Node> getChildren() {
return this.children;
}
public int getEval() {
return eval;
}
//problem: not very transparent from outside cos uses private vars
//needs to be ordered (for loops should ensure that)
//generate all poss children now, filter which we expand later (not .equal prev exp nodes)
// public ArrayList<Node> generateChildren() {
// //assumes parent null thing works
// //i.e. if root (depth=0), then last_changed == -1
//
// //consider making the children in the main program and having a .add() function -why again?
// //issue: We're making an arraylist thing but we're not actually "assigning" children to Node
// ArrayList<Node> children = new ArrayList<Node>();
// int[] dig_temp = new int[3];
// for (int x = 0; x < 3; x++) {
// if (x != last_changed) {
// dig_temp = digits;
// if (dig_temp[x] != 0) {
// dig_temp[x] = digits[x]-1;
// Node child = new Node(this, dig_temp, x);
// children.add(child);
// }
// dig_temp = digits;
// if (dig_temp[x] != 9) {
// dig_temp[x] = digits[x]+1;
// Node child = new Node(this, dig_temp, x);
// children.add(child);
// }
// }
// }
// this.children = children;
// return children;
// }
public boolean equals(Node otherNode) {
// assumptions: if data is same, digits should be same
// if children are the same, last_changed should be same
int[] other_digits = otherNode.getDigits();
int other_changed = otherNode.lastChanged();
//can use Arrays.equals(), compare indiv contents for more transparency/safer
if (Arrays.equals(this.digits, other_digits) && this.last_changed == other_changed) {
return true;
}
return false;
}
public void setVisited(boolean b) {
this.visited = b;
}
public boolean visited() {
return this.visited;
}
public int getHeuristic(){
return this.heuristic;
}
public int getOrder() {
return this.order_added;
}
public int getCost() {
return this.cost;
}
//getPath() --> put in main progr
//This way don't create new one every time, but can get when nec.
//iterate through parents until root reached (e.g. while parent != null)
//
//need toStr() as well for when we print at the end
//could do the children thing in main program and work with strings here?
//but actually v unnecessary during search tbh, so no
//could do Integer.toString() for each digit in num
//OR Regex!!
//might be relevant for cycles
//but we could ignore instead of deleting
//public void deleteNode() {
//sth sth
//}
}