-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
115 lines (96 loc) · 2.76 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
import java.util.ArrayList;
import java.util.List;
/**
* Put a short phrase describing the program here.
*
* @author greedywind / James Archer
*
*/
public class Node {
// n.STATE: the state in the state space to which the node corresponds;
private String state;
// n.PARENT: the node in the search tree that generated this node;
private Node parent;
// n.PATH-COST: the cost, traditionally denoted by g(n), of the path from
// the initial state to the node, as indicated by the parent pointers.
private int pathCost;
/**
* Constructs a node with the specified state.
*
* @param state
* the state in the state space to which the node corresponds.
*/
public Node(String state) {
this.state = state;
this.pathCost = 0;
}
/**
* Constructs a node with the specified state, parent and step
* cost.
*
* @param state
* the state in the state space to which the node corresponds.
* @param parent
* the node in the search tree that generated the node.
*
* @param stepCost
* the cost from the parent node to this node. Used to calculate
* the full pathCost from the root node to here, based on the
* parent's path cost.
*/
public Node(String state, Node parent, int stepCost) {
this.state=(state);
this.parent = parent;
this.pathCost = parent.pathCost + stepCost;
}
/**
* Returns the state in the state space to which the node corresponds.
*
* @return the state in the state space to which the node corresponds.
*/
public String getState() {
return state;
}
/**
* Returns this node's parent node, from which this node was generated.
*
* @return the node's parent node, from which this node was generated.
*/
public Node getParent() {
return parent;
}
/**
* Returns the cost of the path from the initial state to this node as
* indicated by the parent pointers.
*
* @return the cost of the path from the initial state to this node as
* indicated by the parent pointers.
*/
public int getPathCost() {
return pathCost;
}
/**
* Returns <code>true</code> if the node has no parent.
*
* @return <code>true</code> if the node has no parent.
*/
public boolean isRootNode() {
return parent == null;
}
/**
* Returns the path from the root node to this node.
*
* @return the path from the root node to this node.
*/
public List<Node> getPathFromRoot() {
List<Node> path = new ArrayList<Node>();
Node current = this;
while (!current.isRootNode()) {
path.add(0, current);
current = current.getParent();
}
// ensure the root node is added
path.add(0, current);
return path;
}
}