private static void SortNodes(HuffmanNode Node, List <HuffmanNode> Nodes)
        {
            if (Nodes.Contains(Node) == false)
            {
                Nodes.Add(Node);
            }

            if (Node.Left != null)
            {
                SortNodes(Node.Left, Nodes);
            }

            if (Node.Right != null)
            {
                SortNodes(Node.Right, Nodes);
            }
        }
        public void Decompress(string FileName, string path)
        {
            FileInfo oFileInfo = new FileInfo(FileName);

            if (oFileInfo.Exists == true)
            {
                string sCompressedFileName = String.Format("{0}.compressed", oFileInfo.FullName.Replace(oFileInfo.Extension, ""));

                byte[] oBuffer = null;
                using (FileStream oFileStream = File.OpenRead(sCompressedFileName))
                {
                    oBuffer = new byte[oFileStream.Length];
                    oFileStream.Read(oBuffer, 0, oBuffer.Length);
                }


                // Deserialize binary tree
                var root = new HuffmanNode();
                using (Stream stream = File.Open(path, FileMode.Open))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    root = (HuffmanNode)bformatter.Deserialize(stream);
                }

                //  Find the zero node
                HuffmanNode oZeroHuffmanNode = root;
                while (oZeroHuffmanNode.Left != null)
                {
                    oZeroHuffmanNode = oZeroHuffmanNode.Left;
                }

                //  Unpack the file contents
                HuffmanNode   oCurrentHuffmanNode = null;
                StringBuilder oStringBuilder      = new StringBuilder();

                for (int i = 0; i < oBuffer.Length; i++)
                {
                    string sBinaryWord = "";
                    byte   oByte       = oBuffer[i];

                    if (oByte == 0)
                    {
                        sBinaryWord = oZeroHuffmanNode.BinaryWord;
                    }
                    else
                    {
                        sBinaryWord = Convert.ToString(oByte, 2);
                    }

                    if ((sBinaryWord.Length < 8) && (i < (oBuffer.Length - 1)))
                    {
                        //  Pad binary word out to 8 places
                        StringBuilder oBinaryStringBuilder = new StringBuilder(sBinaryWord);
                        while (oBinaryStringBuilder.Length < 8)
                        {
                            oBinaryStringBuilder.Insert(0, "0");
                        }

                        sBinaryWord = oBinaryStringBuilder.ToString();
                    }

                    //  Use the binary word to navigate the tree looking for the value
                    for (int j = 0; j < sBinaryWord.Length; j++)
                    {
                        char cValue = sBinaryWord[j];

                        if (oCurrentHuffmanNode == null)
                        {
                            oCurrentHuffmanNode = root;
                        }

                        if (cValue == '0')
                        {
                            oCurrentHuffmanNode = oCurrentHuffmanNode.Left;
                        }
                        else
                        {
                            oCurrentHuffmanNode = oCurrentHuffmanNode.Right;
                        }

                        if ((oCurrentHuffmanNode.Left == null) && (oCurrentHuffmanNode.Right == null))
                        {
                            //  No more child nodes to choose from, so this must be a value node
                            oStringBuilder.Append(oCurrentHuffmanNode.Value.Value);
                            oCurrentHuffmanNode = null;
                        }
                    }
                }

                //  Write out file
                string sUncompressedFileName = Path.Combine(oFileInfo.Directory.FullName, String.Format("{0}.uncompressed", oFileInfo.Name.Replace(oFileInfo.Extension, "")));

                if (File.Exists(sUncompressedFileName) == true)
                {
                    File.Delete(sUncompressedFileName);
                }

                using (StreamWriter oStreamWriter = new StreamWriter(File.OpenWrite(sUncompressedFileName)))
                {
                    oStreamWriter.Write(oStringBuilder.ToString());
                }
            }
        }