summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/examples/java/bjc
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2015-09-29 13:44:16 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2015-09-29 13:44:16 -0400
commit57aacfc0a64f01dcf8ba9c41989f58aeb58cf501 (patch)
tree53b8604044b1716eb200167fc750a27d2a2ae2d2 /BJC-Utils2/src/examples/java/bjc
parent4e4d470626087c26ee18f5cb04c86647b5f2699c (diff)
Added examples from previous version.
That's all of the things from the previous version added.
Diffstat (limited to 'BJC-Utils2/src/examples/java/bjc')
-rw-r--r--BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java133
-rw-r--r--BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java84
-rw-r--r--BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java57
3 files changed, 274 insertions, 0 deletions
diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java
new file mode 100644
index 0000000..8e2ca51
--- /dev/null
+++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java
@@ -0,0 +1,133 @@
+package bjc.utils.examples;
+
+import java.util.Scanner;
+
+import bjc.utils.data.bst.BinarySearchTree;
+import bjc.utils.data.bst.ITreePart.TreeLinearizationMethod;
+
+/**
+ * Example showing how to use the binary search tree.
+ *
+ * @author ben
+ *
+ */
+public class BinarySearcher {
+ public static void main(String[] args) {
+ Scanner s = new Scanner(System.in);
+
+ System.out.println("Binary Tree Constructor/Searcher");
+
+ char c = 'n';
+
+ BinarySearchTree<Character> bst = new BinarySearchTree<>(
+ (o1, o2) -> o1 - o2);
+
+ while (c != 'e') {
+ System.out.print("Enter a command (m for help): ");
+ c = s.nextLine().charAt(0);
+
+ switch (c) {
+ case 'm':
+ System.out.println("Valid commands: ");
+ System.out.println("\tm: Display this help message.");
+ System.out.println("\te: Exit this program.");
+ System.out.println(
+ "\ta: Add a node to the binary tree.");
+ System.out.println("\td: Display the binary tree.");
+ System.out.println(
+ "\tr: Remove a node from the binary tree.");
+ System.out.println(
+ "\tf: Check if a given node is in the binary tree.");
+ System.out.println(
+ "\tt: Trim all deleted nodes from the tree.");
+ System.out.println(
+ "\tb: Balance the tree (also trims dead nodes)");
+ break;
+
+ case 'a':
+ System.out.print(
+ "Enter the letter to add to the binary tree: ");
+ c = s.nextLine().charAt(0);
+
+ bst.addNode(c);
+ break;
+ case 'r':
+ System.out.print(
+ "Enter the letter to add to the binary tree: ");
+ c = s.nextLine().charAt(0);
+
+ bst.deleteNode(c);
+ break;
+ case 'd':
+ displayTree(bst, s);
+ break;
+ case 'f':
+ System.out.print(
+ "Enter the letter to add to the binary tree: ");
+ c = s.nextLine().charAt(0);
+
+ System.out.println("Node " + c + " was "
+ + (bst.isInTree(c) ? "" : "not ") + "found");
+ break;
+ case 't':
+ bst.trim();
+ break;
+ case 'b':
+ bst.balance();
+ break;
+ default:
+ System.out.println("ERROR: Unrecognized command.");
+ }
+
+ }
+ s.close();
+ }
+
+ private static void displayTree(BinarySearchTree<Character> bst,
+ Scanner s) {
+ System.out.print(
+ "What order would you like the tree to be printed in (m for options): ");
+
+ char c;
+
+ while (true) {
+ c = s.nextLine().charAt(0);
+
+ TreeLinearizationMethod tlm = null;
+
+ switch (c) {
+ case 'm':
+ System.out.println("Possible tree printing methods: ");
+ System.out.println(
+ "\tp: Preorder printing (print parent first, then left & right).");
+ System.out.println(
+ "\ti: Inorder printing (print left first, then parent & right).");
+ System.out.println(
+ "\to: Postorder printing (print left first, then right & parent).");
+ break;
+ case 'p':
+ tlm = TreeLinearizationMethod.PREORDER;
+ break;
+ case 'i':
+ tlm = TreeLinearizationMethod.INORDER;
+ break;
+ case 'o':
+ tlm = TreeLinearizationMethod.POSTORDER;
+ break;
+ default:
+ System.out.println("ERROR: Unknown command.");
+ }
+
+ if (tlm != null) {
+ bst.traverse(tlm, ch -> {
+ System.out.println("Node: " + ch);
+ return true;
+ });
+ return;
+ }
+
+ System.out.print(
+ "What order would you like the tree to be printed in (m for options): ");
+ }
+ }
+}
diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java
new file mode 100644
index 0000000..fd0b221
--- /dev/null
+++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/DiabloItemGen.java
@@ -0,0 +1,84 @@
+package bjc.utils.examples;
+
+import java.util.StringTokenizer;
+
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.FunctionalStringTokenizer;
+import bjc.utils.gen.WeightedGrammar;
+
+public class DiabloItemGen {
+ private static WeightedGrammar<String> parts = new WeightedGrammar<>();
+
+ private static void addCase(String rn, int prob, String prts) {
+ parts.addCase(rn, prob, new FunctionalStringTokenizer(
+ new StringTokenizer(prts, " ")).toList(s -> s));
+ }
+
+ private static void addInfixRules() {
+ String rn = "<infix>";
+
+ addCase(rn, 60, "sword");
+ addCase(rn, 50, "armor");
+ addCase(rn, 40, "rune");
+ addCase(rn, 30, "scroll");
+ addCase(rn, 20, "potion");
+ addCase(rn, 10, "helm");
+ }
+
+ private static void addItemRules() {
+ String rn = "<item>";
+
+ addCase(rn, 10, "<infix>");
+ addCase(rn, 20, "<prefix> <infix>");
+ addCase(rn, 30, "<infix> <suffix>");
+ addCase(rn, 40, "<prefix> <infix> <suffix>");
+ addCase(rn, 50, "<prefix> <prefix> <infix>");
+ addCase(rn, 60, "<prefix> <prefix> <infix> <suffix>");
+ }
+
+ private static void addPrefixRules() {
+ String rn = "<prefix>";
+
+ addCase(rn, 60, "sturdy");
+ addCase(rn, 50, "fine");
+ addCase(rn, 40, "strong");
+ addCase(rn, 30, "azure");
+ addCase(rn, 20, "crimson");
+ addCase(rn, 10, "phasing");
+ }
+
+ private static void addSuffixRules() {
+ String rn = "<suffix>";
+
+ addCase(rn, 60, "of Health");
+ addCase(rn, 50, "of Wealth");
+ addCase(rn, 40, "of Life");
+ addCase(rn, 30, "of the Jackal");
+ addCase(rn, 20, "of Vitality");
+ addCase(rn, 10, "of Ability");
+ }
+
+ public static void main(String[] args) {
+ parts.addRule("<item>");
+ addItemRules();
+
+ parts.addRule("<suffix>");
+ addSuffixRules();
+
+ parts.addRule("<prefix>");
+ addPrefixRules();
+
+ parts.addRule("<infix>");
+ addInfixRules();
+
+ for (int i = 0; i < 100; i++) {
+ FunctionalList<String> ls = parts.genList("<item>", " ");
+
+ StringBuilder sb = new StringBuilder();
+
+ ls.forEach(sp -> sb.append(sp));
+
+ System.out.println(sb.toString().replaceAll("\\s+", " "));
+ }
+ }
+}
diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java
new file mode 100644
index 0000000..368c8ca
--- /dev/null
+++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/RandomStringExamples.java
@@ -0,0 +1,57 @@
+package bjc.utils.examples;
+
+import java.util.StringTokenizer;
+
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.FunctionalStringTokenizer;
+import bjc.utils.gen.RandomGrammar;
+
+public class RandomStringExamples {
+ private static RandomGrammar<String> rg;
+
+ public static void main(String[] args) {
+ rg = new RandomGrammar<>();
+
+ addRule("<sentance>", "<person> <opines> <something>",
+ "<person> thinks that I am <property>",
+ "I <opine> <something>", "You think that I am <property>");
+
+ addRule("<activity>", "dancing", "eating", "sleeping");
+
+ addRule("<object>", "<person>", "life", "my computer",
+ "my friends");
+
+ addRule("<opine>", "hate", "am jealous of", "love");
+
+ addRule("<opines>", "hates", "loves");
+
+ addRule("<person>", "my sister", "my father", "my girlfriend",
+ "the man next door");
+
+ addRule("<property>", "creative", "intelligent");
+
+ addRule("<something>", "<activity>", "<activity> with <person>",
+ "<object>");
+
+ for (int i = 0; i < 10; i++) {
+ FunctionalList<String> ls = rg.genList("<sentance>", " ");
+
+ StringBuilder sb = new StringBuilder();
+
+ ls.forEach(sp -> sb.append(sp));
+
+ System.out.println(sb.toString().replaceAll("\\s+", " "));
+ }
+ }
+
+ private static void addRule(String rule, String... cases) {
+ FunctionalList<FunctionalList<String>> cses = new FunctionalList<>();
+
+ for (String string : cases) {
+ cses.add(new FunctionalStringTokenizer(
+ new StringTokenizer(string, " ")).toList(s -> s));
+ }
+
+ rg.makeRule(rule, cses);
+ }
+}