From 19dc6f5fbb180a6d390657d51887cc206774b7ff Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Sun, 8 Dec 2019 13:49:28 -0600 Subject: It's a little fucked mate --- csci1913/Java/project3/anagrams.txt | 11 ++++ csci1913/Java/project3/project3_strap012.java | 80 ++++++++++++++++----------- 2 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 csci1913/Java/project3/anagrams.txt (limited to 'csci1913') 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 -- cgit v1.2.3