// Constructor, takes in a flat tree string public MorseCodeTree(char[] value, int index) { val = value[index]; if (index * 2 + 1 < value.Length) { dat = new MorseCodeTree(value, index * 2 + 1); } if (index * 2 + 2 < value.Length) { dit = new MorseCodeTree(value, index * 2 + 2); } }
public static void Run() { // Text to tanslate string code = ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--"; // Flattening out the binary tree char[] treeValues = " TEMNAIOGKDWRUS-.QZYCXBJP?L_FVH09?8???7???????61???????2???3?45".ToCharArray(); // Initiating the binary tree (it builds itself using recursion) MorseCodeTree tree = new MorseCodeTree(treeValues, 0); string[] words = code.Split(' '); char[] decoded = new char[words.Length]; for (int x = 0; x < words.Length; x++) { decoded[x] = tree.treeCrawler(words[x].ToCharArray(), 0); Console.Write(decoded[x]); } Console.ReadKey(); }