public void Copy(CPair rCopied) { sSymbols = rCopied.Symbols; sCodes = rCopied.Codes; fProbability = rCopied.Probability; rLeftSon = rCopied.rLeftSon; rRightSon = rCopied.rRightSon; rParent = rCopied.rParent; }
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; }
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; }
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); }
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); }
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); }