示例#1
0
        public char treeCrawler(char[] morseString, int index)
        {
            // If we ever reach a ? node, we inputed some faulty morse code and return ? (no correct path goes through a ?, due to how morse is encoded)
            if (val == '?')
            {
                return('?');
            }

            // If index is the size of the string, we've reached destination, and return val
            if (index == morseString.Length)
            {
                return(val);
            }

            // Check if current index of string is a dat or a dit, recursively crawls the tree
            if (morseString[index] == '-')
            {
                if (dat == null)
                {
                    return('?');
                }
                return(dat.treeCrawler(morseString, index + 1));
            }

            if (morseString[index] == '.')
            {
                if (dit == null)
                {
                    return('?');
                }
                return(dit.treeCrawler(morseString, index + 1));
            }

            if (morseString.Contains('/'))
            {
                return(' ');
            }

            return('?');
        }
        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();
        }