Skip to content

Commit cbe4b61

Browse files
authored
General implementation cleanup (#849)
* Monte Carlo [cpp]: Hardcode sample count This removes the need for user input * Verlet Integration [java]: Move VerletValue class into Verlet class * Tree Traversal [java]: Move main method into Tree class * Huffman Encoding [java]: Move Huffman class to the top of the file Executing a single java file without compilation (in java >= 11) only works if the first class it finds contains the main method.
1 parent c249854 commit cbe4b61

File tree

8 files changed

+56
-60
lines changed

8 files changed

+56
-60
lines changed

contents/huffman_encoding/code/java/huffman.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import java.util.*;
22

3+
class Huffman {
4+
public static void main(String[] args) {
5+
HuffmanTree huffmanTree = new HuffmanTree("bibbity_bobbity");
6+
huffmanTree.createTree();
7+
String encoded = huffmanTree.encode();
8+
System.out.println("Encoded String: " + encoded);
9+
System.out.println("Decoded String: " + huffmanTree.decode(encoded));
10+
}
11+
}
12+
313
class TreeNode {
414
String letter = "";
515
int frequency = 0;
@@ -101,13 +111,3 @@ public String decode(String encoded) {
101111
return decoded.toString();
102112
}
103113
}
104-
105-
class Huffman {
106-
public static void main(String[] args) {
107-
HuffmanTree huffmanTree = new HuffmanTree("bibbity_bobbity");
108-
huffmanTree.createTree();
109-
String encoded = huffmanTree.encode();
110-
System.out.println("Encoded String: " + encoded);
111-
System.out.println("Decoded String: " + huffmanTree.decode(encoded));
112-
}
113-
}

contents/monte_carlo_integration/code/c++/monte_carlo.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ double monte_carlo_pi(unsigned samples) {
3939
int main() {
4040
unsigned samples;
4141

42-
std::cout << "Enter samples to use: ";
43-
std::cin >> samples;
44-
45-
double pi_estimate = monte_carlo_pi(samples);
42+
double pi_estimate = monte_carlo_pi(10000000);
4643
std::cout << "Pi = " << pi_estimate << '\n';
4744
std::cout << "Percent error is: " << 100 * std::abs(pi_estimate - PI) / PI << " %\n";
4845
}

contents/tree_traversal/code/java/MainClass.java

-31
This file was deleted.

contents/tree_traversal/code/java/Tree.java

+30
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,34 @@ public int compareTo(Node other) {
124124
return Integer.compare(this.id, other.id);
125125
}
126126
}
127+
128+
public static void main(String[] args) {
129+
System.out.println("Creating Tree");
130+
Tree tree = new Tree(3, 3);
131+
132+
System.out.println("Using recursive DFS :");
133+
tree.dfsRecursive();
134+
135+
System.out.println("Using stack-based DFS :");
136+
tree.dfsStack();
137+
138+
System.out.println("Using queue-based BFS :");
139+
tree.bfsQueue();
140+
141+
System.out.println("Using post-order recursive DFS :");
142+
tree.dfsRecursivePostOrder();
143+
144+
145+
// Uncommenting the following 2 lines will result in an exception thrown because at least one Node of the Tree has more than 2 children and therefor a DFSRecursiveInorderBinary doesn't work.
146+
System.out.println("Using in-order binary recursive DFS : (fail)");
147+
tree.dfsRecursiveInOrderBinary();
148+
149+
tree = new Tree(3, 2);
150+
System.out.println("Using in-order binary recursive DFS : (succeed)");
151+
tree.dfsRecursiveInOrderBinary();
152+
153+
154+
System.out.println("");
155+
}
156+
127157
}

contents/tree_traversal/tree_traversal.md

-2
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ Here is a video describing tree traversal:
330330
{% sample lang="java" %}
331331
##### Tree.java
332332
[import, lang:"java"](code/java/Tree.java)
333-
##### MainClass.java
334-
[import, lang:"java"](code/java/MainClass.java)
335333
{% sample lang="js" %}
336334
[import, lang:"javascript"](code/javascript/tree.js)
337335
{% sample lang="py" %}

contents/verlet_integration/code/java/Verlet.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
public class Verlet {
2+
3+
private static class VerletValues {
4+
public double time;
5+
public double vel;
6+
7+
public VerletValues(double time, double vel) {
8+
this.time = time;
9+
this.vel = vel;
10+
}
11+
}
12+
13+
214
static double verlet(double pos, double acc, double dt) {
315

416
// Note that we are using a temp variable for the previous position

contents/verlet_integration/code/java/VerletValues.java

-9
This file was deleted.

contents/verlet_integration/verlet_integration.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Here is what it looks like in code:
3737
{% sample lang="c" %}
3838
[import:3-14, lang:"c"](code/c/verlet.c)
3939
{% sample lang="java" %}
40-
[import:2-17, lang:"java"](code/java/Verlet.java)
40+
[import:14-29, lang:"java"](code/java/Verlet.java)
4141
{% sample lang="py" %}
4242
[import:1-10, lang:"python"](code/python/verlet.py)
4343
{% sample lang="hs" %}
@@ -95,7 +95,7 @@ However, the error for this is $$\mathcal{O}(\Delta t)$$, which is quite poor, b
9595
{% sample lang="c" %}
9696
[import:16-31, lang:"c"](code/c/verlet.c)
9797
{% sample lang="java" %}
98-
[import:19-37, lang:"java"](code/java/Verlet.java)
98+
[import:31-49, lang:"java"](code/java/Verlet.java)
9999
{% sample lang="py" %}
100100
[import:12-23, lang:"python"](code/python/verlet.py)
101101
{% sample lang="hs" %}
@@ -167,7 +167,7 @@ Here is the velocity Verlet method in code:
167167
{% sample lang="c" %}
168168
[import:33-43, lang:"c"](code/c/verlet.c)
169169
{% sample lang="java" %}
170-
[import:39-51, lang:"java"](code/java/Verlet.java)
170+
[import:51-63, lang:"java"](code/java/Verlet.java)
171171
{% sample lang="py" %}
172172
[import:25-34, lang:"python"](code/python/verlet.py)
173173
{% sample lang="hs" %}
@@ -225,7 +225,6 @@ Both of these methods work simply by iterating timestep-by-timestep and can be w
225225
{% sample lang="c" %}
226226
[import, lang:"c"](code/c/verlet.c)
227227
{% sample lang="java" %}
228-
[import, lang:"java"](code/java/VerletValues.java)
229228
[import, lang:"java"](code/java/Verlet.java)
230229
{% sample lang="py" %}
231230
[import, lang:"python"](code/python/verlet.py)

0 commit comments

Comments
 (0)