public void Compress() { string inputChars = input.ReadString(); var st = new TrieSymbolTable <int>(); // initialize symbol table for (int i = 0; i < R; i++) { st.Put("" + (char)i, i); } int code = R + 1; // R is codeword for EOF while (inputChars.Length > 0) { string s = st.LongestPrefixOf(inputChars); output.Write(st[s], W); int t = s.Length; if (t < inputChars.Length && code < L) { // Add s to symbol table st.Put(inputChars.Substring(0, t + 1), code++); } // Scan past s in input inputChars = inputChars.Substring(t); } output.Write(R, W); output.Close(); }
public void TrieSymbolTable() { var st = new TrieSymbolTable <int>(); st.Put("by", 4); st.Put("sea", 2); st.Put("sells", 1); st.Put("she", 6); st.Put("shells", 3); st.Put("the", 5); Assert.Equal(6, st.Size()); var keys = st.KeysWithPrefix("sh"); int i = 0; var res = new string[] { "she", "shells" }; foreach (var key in keys) { Assert.Equal(key, res[i]); i++; } Assert.Equal("she", st.LongestPrefixOf("she")); Assert.Equal("she", st.LongestPrefixOf("shed")); st.TryGet("shells", out var val1); Assert.Equal(3, val1); st.TryGet("shed", out var val2); Assert.Equal(0, val2); st.Delete("shells"); st.TryGet("shells", out var val3); Assert.Equal(0, val3); }
public void TernarySearchTrieSymbolTableTest() { var st = new TrieSymbolTable <int>(); st.Put("by", 4); st.Put("sea", 2); st.Put("sells", 1); st.Put("she", 6); st.Put("shells", 3); st.Put("the", 5); st.TryGet("by", out var val1); Assert.Equal(4, val1); st.TryGet("bye", out var val2); Assert.Equal(0, val2); }