diff options
Diffstat (limited to '')
-rw-r--r-- | .vscode/launch.json | 18 | ||||
-rw-r--r-- | csci1913/Java/project3/anagrams.txt | 11 | ||||
-rw-r--r-- | csci1913/Java/project3/project3_strap012.java | 80 |
3 files changed, 67 insertions, 42 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json index bc8c6e0..76c9ca2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,8 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
-
-
+ {
+ "type": "java",
+ "name": "Debug (Launch) with Arguments Prompt",
+ "request": "launch",
+ "mainClass": "",
+ "args": "${command:SpecifyProgramArgs}"
+ },
{
"name": "Python",
"type": "python",
@@ -13,12 +18,6 @@ "program": "${file}",
"console": "integratedTerminal"
},
- // {
- // "type": "java",
- // "name": "Java",
- // "request": "launch",
- // "mainClass": "Driver"
- // },
{
"name": "C++",
"type": "cppdbg",
@@ -31,7 +30,6 @@ "environment": [],
"externalConsole": false,
"MIMode": "gdb",
- //"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
@@ -39,6 +37,6 @@ "ignoreFailures": true
}
]
- },
+ }
]
}
\ No newline at end of file diff --git a/csci1913/Java/project3/anagrams.txt b/csci1913/Java/project3/anagrams.txt new file mode 100644 index 0000000..873a0cc --- /dev/null +++ b/csci1913/Java/project3/anagrams.txt @@ -0,0 +1,11 @@ +test +estt +this +sett +ttes +paul +bill +ringo +a +the +eht
\ No newline at end of file diff --git a/csci1913/Java/project3/project3_strap012.java b/csci1913/Java/project3/project3_strap012.java index afd0aa7..966c4fb 100644 --- a/csci1913/Java/project3/project3_strap012.java +++ b/csci1913/Java/project3/project3_strap012.java @@ -6,7 +6,7 @@ class AnagramTree { private WordNode words; private TreeNode left, right; private TreeNode(String word, byte[] summary) { - words = new WordNode(word); + words = new WordNode(word, null); this.summary = summary; left = null; right = null; } @@ -15,55 +15,70 @@ class AnagramTree { private class WordNode { private String word; private WordNode next; - private WordNode(String word) { + private WordNode(String word, WordNode next) { this.word = word; - next = null; + this.next = next; } } public AnagramTree() { - head = new TreeNode("e", stringToSummary("e")); + head = new TreeNode(null, null); } public void add (String word) { byte [] newSumm = new byte [26]; newSumm = stringToSummary(word); - TreeNode top = head, bottomR = head.right, bottomL = head.left; - // boolean goLeft = false; - try { while (true) { - int comp = compareSummaries(top.summary, newSumm); - if (comp < 0) { // left is less than right, go left - top = bottomL; - // goLeft = true; - bottomL = bottomL.left; - } else if (comp > 0) { // right is less, go right - top = bottomR; - // goLeft = false; - bottomR = bottomR.right; + TreeNode top = head, bottom = top.right; + boolean goLeft = false, addWord = false; + try { while (bottom != null) { + int comp = compareSummaries(bottom.summary, newSumm); + if (comp > 0) { // left is less than right, go left + top = bottom; + goLeft = true; + bottom = bottom.left; + } else if (comp < 0) { // right is less, go right + top = bottom; + goLeft = false; + bottom = bottom.right; } else { - top.words.next = new WordNode(word); + bottom.words = new WordNode(word, bottom.words); + addWord = true; + break; } - }} catch (NullPointerException ignore) { - top = new TreeNode(word, newSumm); } - } + if (addWord) {} // Do nothing + else { + if (goLeft) + top.left = new TreeNode(word, newSumm); + else + top.right = new TreeNode(word, newSumm); + } + } catch (NullPointerException ignore) { + System.out.println("A"); + } + } public void anagrams() { - findAnagrams(head.left); - findAnagrams(head.right); //e should be lower than other words as it is e - } - - private void findAnagrams(TreeNode subtree) { - try { while (true) { - findAnagrams(subtree.right); - findAnagrams(subtree.left); - - } } catch (NullPointerException ignore) { /* Do nothing */} + System.out.println(findAnagrams(head.right)); //e should be lower than other words as it is e } - public String toString() { + private String findAnagrams(TreeNode subtree) { StringBuilder builder = new StringBuilder(); - + try { + if (subtree.words.next != null) { + while (subtree.words != null) { + builder.append(subtree.words.word); + builder.append(" "); + subtree.words = subtree.words.next; + } + } + if (subtree != null) { + findAnagrams(subtree.right); + findAnagrams(subtree.left); + } + } catch (NullPointerException ignore) { + //Do nothing + } return builder.toString(); } @@ -92,5 +107,6 @@ class Anagrammer { while (words.hasNext()) { anagrams.add(words.next()); } + anagrams.anagrams(); } }
\ No newline at end of file |