first commit

This commit is contained in:
2019-08-13 11:40:00 -05:00
commit a03be258da
155 changed files with 1145534 additions and 0 deletions

18
Labs/Lab 7/.idea/checkstyle-idea.xml generated Normal file
View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA">
<option name="configuration">
<map>
<entry key="active-configuration" value="LOCAL_FILE:C:/Users/barnestr/OneDrive - Milwaukee School of Engineering/Documents/2018-2019/CS 2852/CS2852_checkStyle-v2.xml:CS2852" />
<entry key="checkstyle-version" value="8.5" />
<entry key="copy-libs" value="true" />
<entry key="location-0" value="BUNDLED:(bundled):Sun Checks" />
<entry key="location-1" value="BUNDLED:(bundled):Google Checks" />
<entry key="location-2" value="LOCAL_FILE:C:/Users/barnestr/OneDrive - Milwaukee School of Engineering/Documents/2018-2019/CS 2852/CS2852_checkStyle-v2.xml:CS2852" />
<entry key="scan-before-checkin" value="false" />
<entry key="scanscope" value="JavaOnly" />
<entry key="suppress-errors" value="false" />
</map>
</option>
</component>
</project>

View File

@@ -0,0 +1,6 @@
This folder contains libraries copied from the "Lab 7" project.
It is managed by the CheckStyle-IDEA IDE plugin.
Do not modify this folder while the IDE is running.
When the IDE is stopped, you may delete this folder at any time. It will be recreated as needed.
In order to prevent the CheckStyle-IDEA IDE plugin from creating this folder,
uncheck the "Copy libraries from project directory" option in the CheckStyle-IDEA settings dialog.

6
Labs/Lab 7/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
Labs/Lab 7/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Lab 7.iml" filepath="$PROJECT_DIR$/Lab 7.iml" />
</modules>
</component>
</project>

424
Labs/Lab 7/.idea/workspace.xml generated Normal file
View File

@@ -0,0 +1,424 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="921bb292-4a58-430b-a166-45e1ef0a2c70" name="Default" comment="" />
<ignored path="$PROJECT_DIR$/out/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="CheckStyle-IDEA-workspace">
<option name="configuration">
<map>
<entry key="last-active-plugin-version" value="5.22.1" />
</map>
</option>
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/barnestr/MorseTree.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1479">
<caret line="97" column="51" selection-start-line="97" selection-start-column="51" selection-end-line="97" selection-end-column="51" />
<folding>
<element signature="e#0#3552#0" expanded="true" />
<element signature="e#1282#1283#0" expanded="true" />
<element signature="e#1337#1338#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/barnestr/MorseDecoder.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="147">
<caret line="30" column="41" selection-start-line="30" selection-start-column="41" selection-end-line="30" selection-end-column="41" />
<folding>
<element signature="e#0#3191#0" expanded="true" />
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/encoded.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="1" selection-start-line="1" selection-start-column="1" selection-end-line="1" selection-end-column="1" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/decoded.txt">
<provider selected="true" editor-type-id="text-editor" />
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/decoded.txt" />
<option value="$PROJECT_DIR$/src/barnestr/MorseTree.java" />
<option value="$PROJECT_DIR$/src/barnestr/MorseDecoder.java" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="-7" />
<option name="width" value="974" />
<option name="height" value="1047" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="Lab 7" type="b2602c69:ProjectViewProjectNode" />
<item name="Lab 7" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Lab 7" type="b2602c69:ProjectViewProjectNode" />
<item name="Lab 7" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Lab 7" type="b2602c69:ProjectViewProjectNode" />
<item name="Lab 7" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="barnestr" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="PackagesPane" />
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../../../../2017-2018/SE 1021/Spring 2018/Lab 4" />
<property name="project.structure.last.edited" value="Project" />
<property name="project.structure.proportion" value="0.0" />
<property name="project.structure.side.proportion" value="0.2" />
<property name="settings.editor.selected.configurable" value="org.infernus.idea.checkstyle.CheckStyleConfigurable" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration default="true" type="Applet">
<option name="HEIGHT" value="0" />
<option name="POLICY_FILE" value="$APPLICATION_HOME_DIR$/bin/appletviewer.policy" />
<option name="WIDTH" value="0" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="MorseDecoder" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="barnestr.MorseDecoder" />
<module name="Lab 7" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="barnestr.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="Application" factoryName="Application">
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="JUnit" factoryName="JUnit">
<option name="TEST_OBJECT" value="class" />
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="TestNG">
<option name="TEST_OBJECT" value="CLASS" />
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
<properties />
<listeners />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="#org.jetbrains.idea.devkit.run.PluginConfigurationType">
<module name="" />
<option name="VM_PARAMETERS" value="-Xmx512m -Xms256m -XX:MaxPermSize=250m -ea" />
<option name="PROGRAM_PARAMETERS" />
<predefined_log_file enabled="true" id="idea.log" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.MorseDecoder" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="921bb292-4a58-430b-a166-45e1ef0a2c70" name="Default" comment="" />
<created>1539697034625</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1539697034625</updated>
<workItem from="1539697039819" duration="2145000" />
<workItem from="1540251868903" duration="1859000" />
<workItem from="1540439100664" duration="570000" />
<workItem from="1540439687457" duration="2836000" />
<workItem from="1540504632645" duration="1221000" />
<workItem from="1540767969830" duration="599000" />
<workItem from="1540907626332" duration="88000" />
<workItem from="1541274631394" duration="23937000" />
<workItem from="1541373040628" duration="70000" />
<workItem from="1541443643737" duration="21696000" />
<workItem from="1541723696733" duration="1430000" />
<workItem from="1541725268498" duration="1325000" />
<workItem from="1555010978779" duration="46000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="57822000" />
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="2576" height="1416" extended-state="6" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.07193959" />
<window_info id="Structure" order="1" weight="0.25" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="UI Designer" order="4" />
<window_info id="Capture Tool" order="5" />
<window_info id="Favorites" order="6" side_tool="true" />
<window_info anchor="bottom" id="Database Changes" />
<window_info anchor="bottom" id="Docker" show_stripe_button="false" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" weight="0.3254818" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.39721626" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="CheckStyle" order="7" />
<window_info anchor="bottom" id="Version Control" order="8" show_stripe_button="false" />
<window_info anchor="bottom" id="Terminal" order="9" />
<window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
<window_info anchor="bottom" id="Messages" order="11" weight="0.32976446" />
<window_info anchor="right" id="Maven" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Capture Analysis" order="4" />
<window_info anchor="right" id="Theme Preview" order="5" />
<window_info anchor="right" id="Database" order="6" />
<window_info anchor="right" id="Palette&#9;" order="7" />
<window_info anchor="right" id="Maven Projects" order="8" />
</layout>
<layout-to-restore>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.19624217" />
<window_info id="Structure" order="1" weight="0.25" />
<window_info id="Capture Tool" order="2" />
<window_info id="Designer" order="3" />
<window_info id="UI Designer" order="4" />
<window_info id="Image Layers" order="5" />
<window_info id="Favorites" order="6" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.32869378" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.39721626" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Event Log" order="7" side_tool="true" />
<window_info anchor="bottom" id="Messages" order="8" weight="0.32976446" />
<window_info anchor="bottom" id="Version Control" order="9" show_stripe_button="false" />
<window_info anchor="bottom" id="CheckStyle" order="10" />
<window_info anchor="bottom" id="Terminal" order="11" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Maven Projects" order="4" />
<window_info anchor="right" id="Database" order="5" />
<window_info anchor="right" id="Palette&#9;" order="6" />
<window_info anchor="right" id="Capture Analysis" order="7" />
<window_info anchor="right" id="Theme Preview" order="8" />
</layout-to-restore>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="XDebuggerManager">
<watches-manager>
<configuration name="Application">
<watch expression="tree" language="JAVA" />
<watch expression="root" language="JAVA" />
<watch expression="token" language="JAVA" />
<watch expression="sc.next()" language="JAVA" />
</configuration>
</watches-manager>
</component>
<component name="editorHistoryManager">
<entry file="jar://C:/Program Files/Java/jdk1.8.0_144/src.zip!/java/lang/String.java" />
<entry file="jar://C:/Program Files/Java/jdk1.8.0_144/src.zip!/java/util/Scanner.java" />
<entry file="file://$PROJECT_DIR$/morsecode.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="502">
<caret line="36" column="11" selection-start-line="36" selection-start-column="11" selection-end-line="36" selection-end-column="11" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tree.decode(token)" />
<entry file="file://$PROJECT_DIR$/src/barnestr/MorseTree.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1479">
<caret line="97" column="51" selection-start-line="97" selection-start-column="51" selection-end-line="97" selection-end-column="51" />
<folding>
<element signature="e#0#3552#0" expanded="true" />
<element signature="e#1282#1283#0" expanded="true" />
<element signature="e#1337#1338#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/encoded.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="1" selection-start-line="1" selection-start-column="1" selection-end-line="1" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/decoded.txt">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/src/barnestr/MorseDecoder.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="147">
<caret line="30" column="41" selection-start-line="30" selection-start-column="41" selection-end-line="30" selection-end-column="41" />
<folding>
<element signature="e#0#3191#0" expanded="true" />
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
<component name="masterDetails">
<states>
<state key="ArtifactsStructureConfigurable.UI">
<settings>
<artifact-editor />
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="FacetStructureConfigurable.UI">
<settings>
<last-edited>No facets are configured</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="GlobalLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="JdkListConfigurable.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ModuleStructureConfigurable.UI">
<settings>
<last-edited>Lab 7</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ProjectLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

11
Labs/Lab 7/Lab 7.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

5
Labs/Lab 7/decoded.txt Normal file
View File

@@ -0,0 +1,5 @@
A SPACE SHOULD BE PLACED BETWEEN EACH ENCODED CHARACTER.
A SHOULD BE PLACED BETWEEN EACH WORD.
LINE BREAKS IN THE INPUT FILE SHOULD BE REPLICATED IN THE ENCODED FILE.

5
Labs/Lab 7/encoded.txt Normal file
View File

@@ -0,0 +1,5 @@
.- | ... .--. .- -.-. . | ... .... --- ..- .-.. -.. | -... . | .--. .-.. .- -.-. . -.. | -... . - .-- . . -. | . .- -.-. .... | . -. -.-. --- -.. . -.. | -.-. .... .- .-. .- -.-. - . .-. .-.-.- |
.- | * | ... .... --- ..- .-.. -.. | -... . | .--. .-.. .- -.-. . -.. | -... . - .-- . . -. | . .- -.-. .... | .-- --- .-. -.. .-.-.- |
.-.. .. -. . | -... .-. . .- -.- ... | .. -. | - .... . | .. -. .--. ..- - | ..-. .. .-.. . | ... .... --- ..- .-.. -.. | -... . | .-. . .--. .-.. .. -.-. .- - . -.. | .. -. | - .... . | . -. -.-. --- -.. . -.. | ..-. .. .-.. . .-.-.- |

40
Labs/Lab 7/morsecode.txt Normal file
View File

@@ -0,0 +1,40 @@
A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
J .---
K -.-
L .-..
M --
N -.
O ---
P .--.
Q --.-
R .-.
S ...
T -
U ..-
V ...-
W .--
X -..-
Y -.--
Z --..
0 -----
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....
7 --...
8 ---..
9 ----.
. .-.-.-
, --..--
/ -..-.
? ..--..

View File

@@ -0,0 +1,88 @@
/*
* CS 2852
* Fall 2018
* Lab 7 - Morse Code Decoder
* Name: Trevor Barnes
* Created: 10/15/18
*/
package barnestr;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* The main class that builds a morse tree and decodes an encoded file.
* This decoded data is then written out to another file
*
* @author Trevor Barnes
* @version 2.0
*/
public class MorseDecoder {
private static MorseTree<Character> tree = new MorseTree<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
loadDecoder(new File("morsecode.txt"));
System.out.println("Enter an input filename:");
String inputFile = in.nextLine();
System.out.println("Enter an output filename:");
String outputFile = in.nextLine();
decodeFile(inputFile, outputFile);
System.out.println("Encoded file saved in project directory");
}
/**
* Loads in a file with morse code data and builds a Morse Code decoding tree.
* @param file the Morse Code "key" file
*/
public static void loadDecoder(File file) {
try (Scanner sc = new Scanner(file)) {
while (sc.hasNext()) {
String line = sc.nextLine();
String code = line.substring(2, Math.max(line.lastIndexOf('.'),
line.lastIndexOf('-')) + 1);
tree.add(line.charAt(0), code);
}
} catch (FileNotFoundException e) {
System.out.println("Warning! File not found!");
}
}
/**
* Accepts the file location of an encoded file then decodes the data in a file
* @param inputFileName the name of the file to be decoded
* (assuming it is located in the project directory)
* @param outputFileName the name given name for the encoded output file
*/
public static void decodeFile(String inputFileName, String outputFileName) {
File encodedFile = new File(inputFileName);
File decodedFile = new File(outputFileName);
try (Scanner sc = new Scanner(encodedFile)) {
try (PrintWriter pw = new PrintWriter(decodedFile)) {
while (sc.hasNextLine()) {
StringTokenizer line = new StringTokenizer(sc.nextLine());
while (line.hasMoreTokens()) {
String token = line.nextToken();
if (token.equals("|")) {
pw.append(" ");
} else {
try {
char symbol = tree.decode(token);
pw.append(symbol);
} catch (IllegalArgumentException e) {
System.out.println("Warning! Skipping: " + token);
}
}
}
pw.append("\n");
}
}
} catch (FileNotFoundException e) {
System.out.println("Warning! File not found!");
}
}
}

View File

@@ -0,0 +1,116 @@
/*
* CS 2852
* Fall 2018
* Lab 7 - Morse Code Decoder
* Name: Trevor Barnes
* Created: 10/15/18
*/
package barnestr;
/**
* A class that represents a BinaryTree used to decode Morse Code files
*
* @param <E> the generic value used to construct the tree
*/
public class MorseTree<E> {
private static class Node<E> {
private E data;
private Node<E> left;
private Node<E> right;
private Node(E data) {
this.data = data;
left = null;
right = null;
}
@Override
public String toString() {
if (data == null) {
return null;
} else {
return data.toString();
}
}
}
private Node<E> root;
/**
* The constructor for a MorseTree that sets the root node's data to null
*/
public MorseTree() {
this.root = new Node<>(null);
}
/**
* A method that sets the passed in root to whatever result is given from
* the call to the recursive add method.
*
* @param symbol the symbol to be set at the given code location
* @param code the code that determines where a symbol will be placed in the tree
*/
public void add(E symbol, String code) {
root = addRecursive(root, symbol, code);
}
private Node<E> addRecursive(Node<E> localRoot, E symbol, String code) {
if (code.isEmpty()) {
// If the code string is empty, the symbol's location has been reached.
// Returns the new Node with the symbol as its data.
localRoot.data = symbol;
return localRoot;
}
if (code.charAt(0) == '.') {
// If the branch does not exist, add a Node with null data
if (localRoot.left == null) {
localRoot.left = new Node<>(null);
}
// Add in the left half of the localRoot's subtree
localRoot.left = addRecursive(localRoot.left, symbol, code.substring(1));
} else if (code.charAt(0) == '-') {
// If the branch does not exist, add a Node with null data
if (localRoot.right == null) {
localRoot.right = new Node<>(null);
}
// Add in the right half of the localRoot's subtree
localRoot.right = addRecursive(localRoot.right, symbol, code.substring(1));
}
// Return the localRoot Node
return localRoot;
}
/**
* A method retrieves a symbol by traversing the tree with the given
* code by calling a recursive decode method
*
* @param code the code that signifies the location of a value in the tree
* @return a generic with whatever value was decoded
* @throws IllegalArgumentException thrown when anything other than a
* dot or line is passed to it
*/
public E decode(String code) throws IllegalArgumentException {
Node<E> data = decodeRecursive(root, code);
return data.data;
}
private Node<E> decodeRecursive(Node<E> localRoot, String code) {
if (!code.startsWith(".") && !code.startsWith("-") && !code.isEmpty()) {
throw new IllegalArgumentException();
}
if (code.startsWith(".")) {
return decodeRecursive(localRoot.left, code.substring(1));
} else if (code.startsWith("-")) {
return decodeRecursive(localRoot.right, code.substring(1));
}
return localRoot;
}
}