-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRodCutting.java
More file actions
215 lines (179 loc) · 7.09 KB
/
RodCutting.java
File metadata and controls
215 lines (179 loc) · 7.09 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* ===========================================================================
* Coder Information
* Name: Michael Liut
* SID: 1132938
* MacID: liutm
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Submission:
* To: Dr. Antoine Deza
* Class: CAS 6O03
* Dept: Computing and Software,
* Faculty of Engineering,
* McMaster University
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Description:
* Rod Cutting (Stick Cutting) problem. Solved with the use of the
* Huffman Tree in O(n^2) time.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* References:
* Introduction to Algorithms, 3rd Edition, Cormen et. al., MIT Press
* https://en.wikipedia.org/wiki/Huffman_coding
* ===========================================================================
*/
/* Imports */
import java.util.*;
import java.lang.*;
/* Begin RodCutting Class */
public class RodCutting {
/*
* =========================================================================
* Begin Tree Structure aka Tree Node Class
* =========================================================================
*/
class Tree implements Comparable<Tree> {
/* Attribute Declarations */
private Tree right, left;
private long weight;
/* Default Constructor */
public Tree() {
this.weight = 0;
this.right = null;
this.left = null;
}
/* Constructor */
public Tree(long weight) {
this.weight = weight;
this.right = null;
this.left = null;
}
/* Method - Returns the Right Child Node */
public Tree getRightChild() {
return this.right;
}
/* Method - Returns the Left Child Node */
public Tree getLeftChild() {
return this.left;
}
/* Method - Adds a Right Child Node */
public void addRightChild(Tree rightChild) {
this.right = rightChild;
}
/* Method - Adds a Left Child Node */
public void addLeftChild(Tree leftChild) {
this.left = leftChild;
}
/* Method - Returns the Weight of the Node */
public long getWeight() {
return this.weight;
}
/* METHOD OVERRIDE - Comparison of Nodes */
@Override
public int compareTo(Tree node) {
if (this.weight < node.getWeight()) {return -1;}
else {
if (this.weight > node.getWeight()) {return 1;}
else{return 0;}
}
}
} // End of Tree Class
/*
* =========================================================================
* Begin HuffmanTree Class
* =========================================================================
*/
class HuffmanTree {
/* Attribute Declarations */
private Tree node;
private long weight;
/* Default Constructor */
public HuffmanTree() {
this.node = null;
this.weight = 0;
}
/* Constructor */
public HuffmanTree(Tree node) {
this.node = node;
this.weight = node.getWeight();
}
/* Method - Returns the Weight of the Node */
public long getWeight() {
return this.weight;
}
/* Method - HuffmanTree created on array parameter (length of rods) */
public boolean createHuffmanTree(ArrayList<Long> longList) {
/* Tree Structure Variable Declarations */
Tree tempOne, tempTwo, parentTree;
/* Tree Structure Variable Initialization */
tempOne = null;
tempTwo = null;
parentTree = null;
/* Initialize Variable to hold total node weight */
long totalWeight = 0;
/* ArrayList Tree Structure Instantiation */
ArrayList<Tree> treeList = new ArrayList<Tree>();
for (Long i : longList) {
Tree newNode = new Tree(i);
treeList.add(newNode);
}
/* Iterates Through Data Structure */
while (treeList.size() > 1) {
tempOne = Collections.min(treeList);
treeList.remove(tempOne);
tempTwo = Collections.min(treeList);
treeList.remove(tempTwo);
totalWeight = tempOne.getWeight() + tempTwo.getWeight();
parentTree = new Tree(totalWeight);
parentTree.addLeftChild(tempOne);
parentTree.addRightChild(tempTwo);
treeList.add(parentTree);
this.weight += parentTree.getWeight();
}
if (treeList.size() < 1){
return false;
} else {
this.node = treeList.get(0);
return true;
}
} // End of HuffmanTree Method
} // End of HuffmanTree Class
/*
* =========================================================================
* Main Method
* =========================================================================
*/
public static void main(String[] args) throws IllegalArgumentException{
try{
if(args.length < 2){
throw new IllegalArgumentException("Invalid Number of Arguments!");
}
long total = Long.parseLong(args[0]);
long occurences = Long.parseLong(args[1]);
if (occurences != args.length-2){
throw new IllegalArgumentException("Invalid Number of Arguments!");
} else {
ArrayList<Long> length = new ArrayList<Long>();
length.add(Long.parseLong(args[2]));
for(int i = 2; i < args.length-1; i++){
length.add(Long.parseLong(args[i+1])-Long.parseLong(args[i]));
}
length.add(total-Long.parseLong(args[args.length-1]));
/* Instantiate a HuffmanTree Object start */
RodCutting start = new RodCutting();
RodCutting.HuffmanTree cutting = start.new HuffmanTree();
cutting.createHuffmanTree(length);
System.out.printf(String.valueOf(cutting.getWeight())+"\n");
}
} catch(IllegalArgumentException e) {
System.out.println("Error! Invalid Argument(s)!");
System.out.println("Trying to break my program is not nice...");
}
} // End of Main Method
} // End of RodCutting Class
/*
* ===========================================================================
* ===========================================================================
* END OF PROGRAM
* ===========================================================================
* ===========================================================================
*/