|
| 1 | +package dataStructures.trie; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * insert |
| 12 | + * search |
| 13 | + * delete |
| 14 | + * deleteAndPrune |
| 15 | + * wordsWithPrefix |
| 16 | + * getAllWords |
| 17 | + */ |
| 18 | +public class TrieTest { |
| 19 | + @Test |
| 20 | + public void search_shouldFindWordIfExists() { |
| 21 | + Trie trie = new Trie(); |
| 22 | + trie.insert("cs2040s"); |
| 23 | + trie.insert("cs3230"); |
| 24 | + trie.insert("cs4231"); |
| 25 | + trie.insert("cs4234"); |
| 26 | + |
| 27 | + Assert.assertTrue(trie.search("cs2040s")); |
| 28 | + Assert.assertTrue(trie.search("cs4231")); |
| 29 | + Assert.assertFalse(trie.search("cs2040")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void delete_shouldNotFindDeletedWord() { |
| 34 | + Trie trie = new Trie(); |
| 35 | + trie.insert("cs2040s"); |
| 36 | + trie.insert("cs3230"); |
| 37 | + trie.insert("cs4231"); |
| 38 | + trie.insert("cs4234"); |
| 39 | + |
| 40 | + Assert.assertTrue(trie.search("cs4231")); |
| 41 | + trie.delete("cs4231"); |
| 42 | + Assert.assertFalse(trie.search("cs4231")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void deleteAndPrune_shouldNotFindDeletedWord() { |
| 47 | + Trie trie = new Trie(); |
| 48 | + trie.insert("cs2040s"); |
| 49 | + trie.insert("cs3230"); |
| 50 | + trie.insert("cs4231"); |
| 51 | + trie.insert("cs4234"); |
| 52 | + |
| 53 | + Assert.assertTrue(trie.search("cs4231")); |
| 54 | + trie.deleteAndPrune("cs4231"); |
| 55 | + Assert.assertFalse(trie.search("cs4231")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void deleteAndPrune_shouldPrune() { |
| 60 | + Trie trie = new Trie(); |
| 61 | + trie.insert("cs2040s"); |
| 62 | + trie.insert("cs3230"); |
| 63 | + trie.insert("cs4231"); |
| 64 | + trie.insert("cs4234"); |
| 65 | + trie.insert("cs4261"); |
| 66 | + |
| 67 | + trie.deleteAndPrune("cs4261"); |
| 68 | + Assert.assertFalse(trie.checkNodeExistsAtPosition("cs4261", 5)); |
| 69 | + Assert.assertTrue(trie.checkNodeExistsAtPosition("cs4261", 4)); // because of presence of other "cs42"s |
| 70 | + trie.deleteAndPrune("cs3230"); |
| 71 | + Assert.assertFalse(trie.checkNodeExistsAtPosition("cs3230", 3)); |
| 72 | + Assert.assertTrue(trie.checkNodeExistsAtPosition("cs3230", 2)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void wordsWithPrefix_shouldDisplayCurrentWordsThatMatches() { |
| 77 | + Trie trie = new Trie(); |
| 78 | + trie.insert("biology"); |
| 79 | + trie.insert("biodiversity"); |
| 80 | + trie.insert("bioinformatics"); |
| 81 | + trie.insert("biotechnology"); |
| 82 | + trie.insert("biotic"); |
| 83 | + trie.insert("bicycle"); |
| 84 | + trie.insert("bijection"); |
| 85 | + trie.insert("symbiotic"); |
| 86 | + trie.insert("submarine"); |
| 87 | + trie.insert("submerged"); |
| 88 | + |
| 89 | + List<String> resultA = trie.wordsWithPrefix("bio"); |
| 90 | + Collections.sort(resultA); |
| 91 | + List<String> expectedA = Arrays.asList("biology", "biodiversity", "bioinformatics", "biotechnology", "biotic"); |
| 92 | + Collections.sort(expectedA); |
| 93 | + Assert.assertEquals(expectedA, resultA); |
| 94 | + |
| 95 | + List<String> resultB = trie.wordsWithPrefix("sub"); |
| 96 | + Collections.sort(resultB); |
| 97 | + List<String> expectedB = Arrays.asList("submarine", "submerged"); |
| 98 | + Collections.sort(expectedB); |
| 99 | + Assert.assertEquals(expectedB, resultB); |
| 100 | + |
| 101 | + trie.delete("biodiversity"); |
| 102 | + trie.delete("biotic"); |
| 103 | + |
| 104 | + List<String> resultC = trie.wordsWithPrefix("bio"); |
| 105 | + Collections.sort(resultC); |
| 106 | + List<String> expectedC = Arrays.asList("biology", "bioinformatics", "biotechnology"); |
| 107 | + Collections.sort(expectedC); |
| 108 | + Assert.assertEquals(expectedC, resultC); |
| 109 | + |
| 110 | + List<String> resultD = trie.wordsWithPrefix("sy"); |
| 111 | + Collections.sort(resultD); |
| 112 | + List<String> expectedD = Arrays.asList("symbiotic"); |
| 113 | + Collections.sort(expectedD); |
| 114 | + Assert.assertEquals(expectedD, resultD); |
| 115 | + |
| 116 | + trie.delete("symbiotic"); |
| 117 | + |
| 118 | + List<String> resultE = trie.wordsWithPrefix("sy"); |
| 119 | + Collections.sort(resultE); |
| 120 | + List<String> expectedE = Arrays.asList(); |
| 121 | + Collections.sort(expectedE); |
| 122 | + Assert.assertEquals(expectedE, resultE); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void getAllWords_shouldDisplayCurrentWords() { |
| 127 | + Trie trie = new Trie(); |
| 128 | + trie.insert("eldric"); |
| 129 | + trie.insert("seth"); |
| 130 | + trie.insert("gilbert"); |
| 131 | + trie.insert("KAI TING"); // converted to lower-case |
| 132 | + trie.insert("daniel"); |
| 133 | + trie.insert("andre"); |
| 134 | + |
| 135 | + List<String> resultA = trie.getAllWords(); |
| 136 | + Collections.sort(resultA); |
| 137 | + List<String> expectedA = Arrays.asList("eldric", "seth", "gilbert", "kai ting", "andre", "daniel"); |
| 138 | + Collections.sort(expectedA); |
| 139 | + Assert.assertEquals(expectedA, resultA); |
| 140 | + |
| 141 | + trie.delete("daniel"); |
| 142 | + trie.delete("eldric"); |
| 143 | + List<String> resultB = trie.getAllWords(); |
| 144 | + Collections.sort(resultB); |
| 145 | + List<String> expectedB = Arrays.asList("seth", "gilbert", "kai ting", "andre"); |
| 146 | + Collections.sort(expectedB); |
| 147 | + Assert.assertEquals(expectedB, resultB); |
| 148 | + } |
| 149 | +} |
0 commit comments