From f7bbd561bb425588fd823ddc38ac56ab5435470f Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Sun, 8 Dec 2019 12:32:09 -0600 Subject: A --- csci1913/Java/project3/project3_strap012.java | 57 +++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'csci1913') diff --git a/csci1913/Java/project3/project3_strap012.java b/csci1913/Java/project3/project3_strap012.java index c8a4719..afd0aa7 100644 --- a/csci1913/Java/project3/project3_strap012.java +++ b/csci1913/Java/project3/project3_strap012.java @@ -1,14 +1,13 @@ package project3; - class AnagramTree { private TreeNode head; private class TreeNode { private byte[] summary; private WordNode words; private TreeNode left, right; - private TreeNode(String word) { - summary = stringToSummary(word); - // words = new WordNode(word); + private TreeNode(String word, byte[] summary) { + words = new WordNode(word); + this.summary = summary; left = null; right = null; } } @@ -23,15 +22,49 @@ class AnagramTree { } public AnagramTree() { - head = new TreeNode("e"); + head = new TreeNode("e", stringToSummary("e")); } 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; + } else { + top.words.next = new WordNode(word); + } + }} catch (NullPointerException ignore) { + top = new TreeNode(word, newSumm); + } + } + public void anagrams() { + findAnagrams(head.left); + findAnagrams(head.right); //e should be lower than other words as it is e } - public void anagrams() { + private void findAnagrams(TreeNode subtree) { + try { while (true) { + findAnagrams(subtree.right); + findAnagrams(subtree.left); + + } } catch (NullPointerException ignore) { /* Do nothing */} + } + + public String toString() { + StringBuilder builder = new StringBuilder(); + return builder.toString(); } private int compareSummaries(byte[] left, byte[] right) { @@ -46,8 +79,18 @@ class AnagramTree { private byte[] stringToSummary(String word) { byte[] temp = new byte[26]; for (byte i = 0; i < word.length(); i++) { - temp[i] = (byte) (word.charAt(i) - ('a' + i)); + temp[word.charAt(i) - 'a'] += 1; } return temp; } +} + +class Anagrammer { + public static void main(String[] args) { + Words words = new Words(args[0]); + AnagramTree anagrams = new AnagramTree(); + while (words.hasNext()) { + anagrams.add(words.next()); + } + } } \ No newline at end of file -- cgit v1.2.3