Ejemplo n.º 1
0
 public void Copy(CPair rCopied)
 {
     sSymbols     = rCopied.Symbols;
     sCodes       = rCopied.Codes;
     fProbability = rCopied.Probability;
     rLeftSon     = rCopied.rLeftSon;
     rRightSon    = rCopied.rRightSon;
     rParent      = rCopied.rParent;
 }
Ejemplo n.º 2
0
 public CPair(string sSymbols, double fProbability)
 {
     Assert.IsTrue(sSymbols.Length.CompareTo(0).Equals(1));
     Assert.IsTrue(fProbability.CompareTo(0).Equals(1));
     this.sSymbols     = sSymbols;
     this.fProbability = fProbability;
     rLeftSon          = null;
     rRightSon         = null;
     rParent           = null;
 }
Ejemplo n.º 3
0
 public CPair(CPair rLeftSon, CPair rRightSon)
 {
     this.sSymbols     = rLeftSon.Symbols + rRightSon.Symbols;
     this.fProbability = rLeftSon.Probability + rRightSon.Probability;
     rLeftSon.UpdateCodes("1");
     rRightSon.UpdateCodes("0");
     this.rLeftSon          = rLeftSon;
     this.rRightSon         = rRightSon;
     this.rLeftSon.rParent  = this;
     this.rRightSon.rParent = this;
 }
Ejemplo n.º 4
0
        private static CPair FindAndDeleteMinProbability(List <CPair> aCodes)
        {
            CPair fMin         = new CPair("TempSymp", double.MaxValue);
            int   iNumberOfMin = -1;

            for (int i = 0; i < aCodes.Count; i++)
            {
                if (aCodes[i].Probability < fMin.Probability)
                {
                    fMin.Copy(aCodes[i]);
                    iNumberOfMin = i;
                }
            }
            aCodes.RemoveAt(iNumberOfMin);
            return(fMin);
        }
Ejemplo n.º 5
0
        public static CPair[] HaffmanCode(CPair[] aSequence)
        {
            Assert.IsTrue(aSequence.Length.CompareTo(2).Equals(1));
            List <CPair> aTreeCodes = new List <CPair>();

            foreach (CPair rPair in aSequence)
            {
                aTreeCodes.Add(rPair);
            }
            while (aTreeCodes.Count != 1)
            {
                CPair rFirstMin  = FindAndDeleteMinProbability(aTreeCodes),
                      rSecondMin = FindAndDeleteMinProbability(aTreeCodes);
                CPair rNewRoot   = new CPair(rFirstMin, rSecondMin);
                aTreeCodes.Add(rNewRoot);
            }
            return(aTreeCodes[0].Leafs);
        }
Ejemplo n.º 6
0
        public Form1()
        {
            InitializeComponent();
            int iLength = 5;

            char[]   aSymbols       = new char[] { 'a', 'b', 'c', 'd', 'e' };
            double[] aProbabilities = new double[] { 0.02, 0.25, 0.08, 0.35, 0.3 };
            CPair[]  aPairs         = new CPair[iLength];
            for (int i = 0; i < iLength; i++)
            {
                aPairs[i] = new CPair(aSymbols[i].ToString(), aProbabilities[i]);
            }
            aPairs = CFunctionsKeeper.HaffmanCode(aPairs);
            string str = "";

            foreach (CPair rPair in aPairs)
            {
                str += "Symbol " + rPair.Symbols + " with probability "
                       + rPair.Probability.ToString() + " has code " + rPair.Codes + "\r\n";
            }
            MessageBox.Show(str);
        }