From 7dbc90e4691ae3ed4fec5068461b4263346e67da Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 28 Aug 2016 22:35:31 -0400 Subject: Tossed attempt at subrule-supported parser Also, renamed some tests --- .../java/bjc/utils/examples/BinarySearchTest.java | 151 +++++++++++++++++++++ .../java/bjc/utils/examples/BinarySearcher.java | 151 --------------------- .../java/bjc/utils/examples/parsing/ShuntTest.java | 38 ++++++ .../bjc/utils/examples/parsing/ShuntTester.java | 38 ------ .../java/bjc/utils/examples/parsing/test.tree | 13 ++ 5 files changed, 202 insertions(+), 189 deletions(-) create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java delete mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java delete mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/test.tree (limited to 'BJC-Utils2/src/examples/java/bjc/utils') diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java new file mode 100644 index 0000000..c453e4b --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java @@ -0,0 +1,151 @@ +package bjc.utils.examples; + +import java.util.Scanner; + +import bjc.utils.funcdata.bst.BinarySearchTree; +import bjc.utils.funcdata.bst.TreeLinearizationMethod; + +/** + * Example showing how to use the binary search tree. + * + * @author ben + * + */ +public class BinarySearchTest { + private static void displayTree(BinarySearchTree tree, + Scanner inputSource) { + System.out.print( + "What order would you like the tree to be printed in (m for options): "); + + char command; + + while (true) { + command = inputSource.nextLine().charAt(0); + + TreeLinearizationMethod linearizationMethod = null; + + switch (command) { + 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': + linearizationMethod = TreeLinearizationMethod.PREORDER; + break; + + case 'i': + linearizationMethod = TreeLinearizationMethod.INORDER; + break; + + case 'o': + linearizationMethod = TreeLinearizationMethod.POSTORDER; + break; + + default: + System.out.println("ERROR: Unknown command."); + } + + if (linearizationMethod != null) { + tree.traverse(linearizationMethod, (element) -> { + System.out.println("Node: " + element); + return true; + }); + + return; + } + + System.out.print( + "What order would you like the tree to be printed in (m for options): "); + } + } + + /** + * Main method of class + * + * @param args + * Unused CLI args + */ + public static void main(String[] args) { + Scanner inputSource = new Scanner(System.in); + + System.out.println("Binary Tree Constructor/Searcher"); + + char command = ' '; + + BinarySearchTree searchTree = new BinarySearchTree<>( + (o1, o2) -> o1 - o2); + + while (command != 'e') { + System.out.print("Enter a command (m for help): "); + command = inputSource.nextLine().charAt(0); + + switch (command) { + 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: "); + command = inputSource.nextLine().charAt(0); + + searchTree.addNode(command); + break; + + case 'r': + System.out.print( + "Enter the letter to add to the binary tree: "); + command = inputSource.nextLine().charAt(0); + + searchTree.deleteNode(command); + break; + + case 'd': + displayTree(searchTree, inputSource); + break; + + case 'f': + System.out.print( + "Enter the letter to add to the binary tree: "); + command = inputSource.nextLine().charAt(0); + + System.out.println("Node " + command + " was " + + (searchTree.isInTree(command) ? "" : "not ") + + "found"); + break; + + case 't': + searchTree.trim(); + break; + + case 'b': + searchTree.balance(); + break; + + default: + System.out.println("ERROR: Unrecognized command."); + } + } + + inputSource.close(); + } +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java deleted file mode 100644 index 8fefe70..0000000 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java +++ /dev/null @@ -1,151 +0,0 @@ -package bjc.utils.examples; - -import java.util.Scanner; - -import bjc.utils.funcdata.bst.BinarySearchTree; -import bjc.utils.funcdata.bst.TreeLinearizationMethod; - -/** - * Example showing how to use the binary search tree. - * - * @author ben - * - */ -public class BinarySearcher { - private static void displayTree(BinarySearchTree tree, - Scanner inputSource) { - System.out.print( - "What order would you like the tree to be printed in (m for options): "); - - char command; - - while (true) { - command = inputSource.nextLine().charAt(0); - - TreeLinearizationMethod linearizationMethod = null; - - switch (command) { - 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': - linearizationMethod = TreeLinearizationMethod.PREORDER; - break; - - case 'i': - linearizationMethod = TreeLinearizationMethod.INORDER; - break; - - case 'o': - linearizationMethod = TreeLinearizationMethod.POSTORDER; - break; - - default: - System.out.println("ERROR: Unknown command."); - } - - if (linearizationMethod != null) { - tree.traverse(linearizationMethod, (element) -> { - System.out.println("Node: " + element); - return true; - }); - - return; - } - - System.out.print( - "What order would you like the tree to be printed in (m for options): "); - } - } - - /** - * Main method of class - * - * @param args - * Unused CLI args - */ - public static void main(String[] args) { - Scanner inputSource = new Scanner(System.in); - - System.out.println("Binary Tree Constructor/Searcher"); - - char command = ' '; - - BinarySearchTree searchTree = new BinarySearchTree<>( - (o1, o2) -> o1 - o2); - - while (command != 'e') { - System.out.print("Enter a command (m for help): "); - command = inputSource.nextLine().charAt(0); - - switch (command) { - 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: "); - command = inputSource.nextLine().charAt(0); - - searchTree.addNode(command); - break; - - case 'r': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = inputSource.nextLine().charAt(0); - - searchTree.deleteNode(command); - break; - - case 'd': - displayTree(searchTree, inputSource); - break; - - case 'f': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = inputSource.nextLine().charAt(0); - - System.out.println("Node " + command + " was " - + (searchTree.isInTree(command) ? "" : "not ") - + "found"); - break; - - case 't': - searchTree.trim(); - break; - - case 'b': - searchTree.balance(); - break; - - default: - System.out.println("ERROR: Unrecognized command."); - } - } - - inputSource.close(); - } -} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java new file mode 100644 index 0000000..47bbaf8 --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java @@ -0,0 +1,38 @@ +package bjc.utils.examples.parsing; + +import java.util.Scanner; + +import bjc.utils.funcdata.FunctionalStringTokenizer; +import bjc.utils.funcdata.IList; +import bjc.utils.parserutils.ShuntingYard; + +/** + * Test of shunting yard + * + * @author ben + * + */ +public class ShuntTest { + /** + * Main method + * + * @param args + * Unused CLI args + */ + public static void main(String[] args) { + Scanner inputSource = new Scanner(System.in); + + System.out.print("Enter a expression to shunt: "); + String line = inputSource.nextLine(); + + ShuntingYard yard = new ShuntingYard<>(true); + + IList shuntedTokens = yard + .postfix(new FunctionalStringTokenizer(line) + .toList((strang) -> strang), (strang) -> strang); + + System.out.println(shuntedTokens.toString()); + + inputSource.close(); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java deleted file mode 100644 index 5148be4..0000000 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java +++ /dev/null @@ -1,38 +0,0 @@ -package bjc.utils.examples.parsing; - -import java.util.Scanner; - -import bjc.utils.funcdata.FunctionalStringTokenizer; -import bjc.utils.funcdata.IList; -import bjc.utils.parserutils.ShuntingYard; - -/** - * Test of shunting yard - * - * @author ben - * - */ -public class ShuntTester { - /** - * Main method - * - * @param args - * Unused CLI args - */ - public static void main(String[] args) { - Scanner inputSource = new Scanner(System.in); - - System.out.print("Enter a expression to shunt: "); - String line = inputSource.nextLine(); - - ShuntingYard yard = new ShuntingYard<>(true); - - IList shuntedTokens = yard - .postfix(new FunctionalStringTokenizer(line) - .toList((strang) -> strang), (strang) -> strang); - - System.out.println(shuntedTokens.toString()); - - inputSource.close(); - } -} \ No newline at end of file diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/test.tree b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/test.tree new file mode 100644 index 0000000..795cc88 --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/test.tree @@ -0,0 +1,13 @@ +test 1 + 1 + 1 + 2 + 2 + 1 + +simp 1 + 2 + 3 + 4 + 3 + 2 \ No newline at end of file -- cgit v1.2.3