-
Notifications
You must be signed in to change notification settings - Fork 0
/
FactorialTree.java
123 lines (89 loc) · 2.86 KB
/
FactorialTree.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
import java.util.ArrayList;
public class FactorialTree<AnyType> {
private int numPos; // Number of possibilities
private Node<AnyType> root;
private ArrayList<Node<AnyType>> leafList;
public FactorialTree(AnyType[] list) {
numPos = list.length;
leafList = new ArrayList<Node<AnyType>>();
root = new Node<AnyType> (null, null, numPos);
buildTree(root, list, numPos);
}
// Number of possibilities become number of children of a node.
private void buildTree(Node<AnyType> parent, AnyType[] childList, int numChild){
// If it's a leaf node
if (numChild == 1){
Node<AnyType> leaf = new Node<AnyType>(parent, childList[0], numChild);
parent.addChildNode(leaf);
leafList.add(leaf);
return;
}
// If root.
else if (numChild == numPos){
for (int i = 0; i < childList.length; i++){
Node<AnyType> childNode = new Node<AnyType>(parent, childList[i], numChild);
parent.addChildNode(childNode);
buildTree(childNode, remove(childList, i), numChild-1);
}
return;
}
// Recursively populates parent Node's children.
else {
for (int i = 0; i < childList.length; i++){
Node<AnyType> childNode = new Node<AnyType>(parent, childList[i], numChild);
parent.addChildNode(childNode);
buildTree(childNode, remove(childList, i), numChild-1);
}
}
}
public ArrayList<ArrayList<AnyType>> getAllCombinations(){
ArrayList<ArrayList<AnyType>> comboList = new ArrayList<ArrayList<AnyType>>(leafList.size());
for (int i = 0; i < leafList.size(); i++){
Node<AnyType> leaf = leafList.get(i);
ArrayList<AnyType> nodesList = getCombination(new ArrayList<AnyType>(), leaf);
// Makes path a circuit that goes back to starting vertex
nodesList.add(leaf.data);
comboList.add(nodesList);
}
return comboList;
}
private ArrayList<AnyType> getCombination(ArrayList<AnyType> list, Node<AnyType> node) {
if (node.data == null) {
return list;
}
else {
AnyType nodeData = node.data;
list.add(nodeData);
getCombination(list, node.parent);
return list;
}
}
private AnyType[] remove(AnyType[] arr, int location){
ArrayList<AnyType> newList = new ArrayList<AnyType>(arr.length);
AnyType toRemove = arr[location];
for (AnyType t : arr)
if (!toRemove.equals(t))
newList.add(t);
AnyType[] newArr = newList.toArray( (AnyType[]) new Comparable[newList.size()] );
return newArr;
}
private static class Node<AnyType> {
AnyType data;
Node<AnyType> parent;
ArrayList<Node<AnyType>> children;
Node(Node<AnyType> parent, AnyType data, int numChild) {
this.parent = parent;
this.data = data;
if (numChild != 0)
children = new ArrayList<Node<AnyType>>(numChild);
else
children = null;
}
void addChildNode(Node<AnyType> n){
children.add(n);
}
public String toString(){
return ("" + data);
}
}
}