aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913/Java/project3/project3_strap012.java
blob: 966c4fb5e945f7e3172170a0a9e25ac01321e0f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package project3;
class AnagramTree {
    private TreeNode head;
    private class TreeNode {
        private byte[] summary;
        private WordNode words;
        private TreeNode left, right;
        private TreeNode(String word, byte[] summary) {
            words = new WordNode(word, null);
            this.summary = summary;
            left = null; right = null;
        }
    }

    private class WordNode {
        private String word;
        private WordNode next;
        private WordNode(String word, WordNode next) {
            this.word = word;
            this.next = next;
        }
    }

    public AnagramTree() {
        head = new TreeNode(null, null);
    }

    public void add (String word) {
        byte [] newSumm = new byte [26];
        newSumm = stringToSummary(word);
        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 {
                bottom.words = new WordNode(word, bottom.words);
                addWord = true;
                break;
            }
        }
        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() {
        System.out.println(findAnagrams(head.right));       //e should be lower than other words as it is e
    }

    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();
    }
    
    private int compareSummaries(byte[] left, byte[] right) {
        for (int i = 0; i < 25; i++) {
            if (left[i] != right[i]) {
                return left[i] - right[i];
            }
        }
        return 0;
    }

    private byte[] stringToSummary(String word) {
        byte[] temp = new byte[26];
        for (byte i = 0; i < word.length(); 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());
        }
        anagrams.anagrams();
    }
}