reserve SOURCE for non empty finite set,
 p for Probability of Trivial-SigmaField SOURCE,
 Tseq for FinSequence of BoolBinFinTrees IndexedREAL,
 q for FinSequence of NAT;

theorem Th17:
 ex Tseq,q st Tseq,q,p is_constructingBinHuffmanTree
proof
defpred P1[Nat, set, set] means
 ((ex u,v be object st u <> v & u in $2 & v in $2) implies
 ex X,Y being non empty finite Subset of BinFinTrees IndexedREAL,
    s being MinValueTree of X,
    t being MinValueTree of Y,
    w being finite binary DecoratedTree of IndexedREAL st
 $2 = X & Y = X \ {s} &
 w in {MakeTree (t,s,MaxVl(X) + 1),MakeTree (s,t,MaxVl(X) + 1) } &
 $3= (X \ {t,s} ) \/ {w} );
 A1: for n being Nat st 1 <= n & n < card (SOURCE) holds
for x being Element of (BoolBinFinTrees IndexedREAL)
 ex y being Element of (BoolBinFinTrees IndexedREAL) st P1[n,x,y]
proof
let n be Nat;
 assume 1 <= n & n < card (SOURCE);
let x be Element of (BoolBinFinTrees IndexedREAL);
 now assume ex u,v be object st u <> v & u in x & v in x;
 then consider u,v be object such that
 A2: u <> v & u in x & v in x;
 x in BoolBinFinTrees IndexedREAL; then
 ex z be Element of bool BinFinTrees IndexedREAL
 st z = x & z is finite & z <> {};
 then reconsider X = x as non empty finite Subset of BinFinTrees IndexedREAL;
 set s = the MinValueTree of X;
 set Y = X \ {s};
 reconsider Y as Subset of BinFinTrees IndexedREAL;
 reconsider Y as non empty finite Subset of BinFinTrees IndexedREAL
 by A2,Lm1;
 set t = the MinValueTree of Y;
 set w= MakeTree (t,s,MaxVl(X) + 1);
 A3: w in {MakeTree (t,s,MaxVl(X) + 1),
 MakeTree (s,t,MaxVl(X) + 1)} by TARSKI:def 2;
 set y = (X \ {t,s} ) \/ {w};
A4: s in X & t in Y by Def10;
 dom (MakeTree (t,s,MaxVl(X) + 1))
 is finite & dom (MakeTree (t,s,MaxVl(X) + 1)) is binary &
 dom (MakeTree (s,t,MaxVl(X) + 1))
 is finite & dom (MakeTree (s,t,MaxVl(X) + 1)) is binary by BINTREE1:def 3;
 then w in BinFinTrees IndexedREAL by Def2; then
 y c= BinFinTrees IndexedREAL by A4,Lm2;
 then y in (BoolBinFinTrees IndexedREAL);
 hence ex y being Element of (BoolBinFinTrees IndexedREAL) st
 ex X,Y be non empty finite Subset of BinFinTrees IndexedREAL,
    s being MinValueTree of X,
    t being MinValueTree of Y,
    w being finite binary DecoratedTree of IndexedREAL st
 x = X & Y = X \ {s} &
 w in {MakeTree (t,s,MaxVl(X) + 1),MakeTree (s,t,MaxVl(X) + 1)} &
 y = (X \ {t,s} ) \/ {w} by A3;
 end;
 hence thesis;
end;
Initial-Trees(p) in BoolBinFinTrees IndexedREAL;
then reconsider ITS = Initial-Trees(p)
 as Element of BoolBinFinTrees IndexedREAL;
consider Tseq being FinSequence of (BoolBinFinTrees IndexedREAL) such that
A5:
 len Tseq = card SOURCE
&( Tseq.1 = ITS or card SOURCE = 0)
&( for n being Nat st 1 <= n & n < card SOURCE holds
 P1[n,Tseq.n,Tseq.(n + 1)] ) from RECDEF_1:sch 4(A1);
defpred P2[object,object] means
 ex X be finite set st Tseq.$1 = X & $2 = card X & $2 <> 0;
A6: for k be Nat st k in Seg card (SOURCE)
 ex x being Element of NAT st P2[k,x]
proof
 let k be Nat;
 assume k in Seg card SOURCE; then
 k in dom Tseq by A5,FINSEQ_1:def 3; then
 Tseq.k in rng Tseq by FUNCT_1:3;
 then Tseq.k in BoolBinFinTrees IndexedREAL by FINSEQ_1:def 4,TARSKI:def 3;
 then
 A7: ex x be Element of bool BinFinTrees IndexedREAL
 st x= Tseq.k & x is finite & x <> {};
 then reconsider X = Tseq.k as finite set;
 take x = card X;
 thus thesis by A7;
end;
consider q being FinSequence of NAT such that
A8: dom q = Seg card SOURCE
 & for k be Nat st k in Seg card SOURCE holds P2[k,q.k]
 from FINSEQ_1:sch 5(A6);
A9: for k be Nat st k in Seg card SOURCE
 holds q.k = card(Tseq.k) & q.k <> 0
proof
 let k be Nat;
 assume k in Seg card (SOURCE); then
 ex X be finite set st Tseq.k = X & q.k = card X & q.k <> 0 by A8;
 hence q.k = card(Tseq.k) & q.k <> 0;
end;
A10: card (Initial-Trees(p)) = card SOURCE by Th5;
 1 <= card (SOURCE) by NAT_1:14; then
A11: 1 in Seg card SOURCE by FINSEQ_1:1;
then A12: q.1 = card SOURCE by A5,A9,A10;
A13: for k be Nat st 1<= k & k < card SOURCE holds
 (2 <= q.k implies q.(k+1) = q.k - 1)
proof
 let k be Nat;
 assume A14: 1<= k & k < card (SOURCE);
 thus 2 <= q.k implies q.(k+1) = q.k - 1
 proof
 assume A15: 2 <= q.k;
 A16: (ex u,v be object st u <> v & u in Tseq.k & v in Tseq.k) implies
 ex X,Y be non empty finite Subset of BinFinTrees IndexedREAL st
 ex s being MinValueTree of X,
    t being MinValueTree of Y,
    w being finite binary DecoratedTree of IndexedREAL st
 Tseq.k = X & Y = X \ {s} &
 w in {MakeTree (t,s,MaxVl(X) + 1),MakeTree (s,t,MaxVl(X) + 1)} &
 Tseq.(k + 1) = (X \ {t,s} ) \/ {w} by A5,A14;
 k in Seg card (SOURCE) by FINSEQ_1:1,A14; then
 A17: q.k = card(Tseq.k) by A9;
 A18: 1 <= k+1 by NAT_1:11;
 k + 1 <= card SOURCE by NAT_1:13,A14; then
 k+1 in Seg card SOURCE by A18,FINSEQ_1:1; then
 A19: q.(k+1) = card(Tseq.(k+1)) by A9;
 consider Tseqk be finite set such that
 A20: Tseq.k = Tseqk & q.k = card Tseqk & q.k <> 0 by FINSEQ_1:1,A14,A8;
 consider X,Y be non empty finite Subset of BinFinTrees IndexedREAL,
    s being MinValueTree of X,
    t being MinValueTree of Y,
    w being finite binary DecoratedTree of IndexedREAL such that
A21: Tseq.k = X & Y = X \ {s} &
 w in {MakeTree (t,s,MaxVl(X) + 1),MakeTree (s,t,MaxVl(X) + 1)} &
 Tseq.(k + 1) = (X \ {t,s} ) \/ {w} by Lm3,A20,A15,A16;
A22: s in X & t in Y by Def10;
 then t in X & not t in {s} by A21,XBOOLE_0:def 5; then
 A23: t in X & t <> s by TARSKI:def 1;
 not MakeTree (t,s,(MaxVl(X) + 1)) in X &
 not MakeTree (s,t,(MaxVl(X) + 1)) in X by Th6;
 then not w in X by A21,TARSKI:def 2;
 hence q.(k+1) = q.k - 1 by A22,A17,A19,A21,Lm5,A23;
 end;
end;

defpred P4[Nat] means $1 < card SOURCE implies q.($1+1) = q.1 - $1;
A24: P4[0];
A25: for n be Nat st P4[n] holds P4[n+1]
proof
 let n be Nat;
 assume A26: P4[n];
 assume A27: n+1 < card SOURCE;
 A28: n<=n+1 by NAT_1:11;
 1<= n+1 & n+1 <= card SOURCE by A27,NAT_1:11; then
 n+1 in Seg card SOURCE by FINSEQ_1:1; then
 A29: 1<= q.(n+1) by NAT_1:14,A9;
 A30: 1+1 <= q.(n+1)
 proof
 assume not 1+1 <= q.(n+1); then
 q.(n+1) <=1 by NAT_1:13; then
 1 = q.1 - n by A28,A26,A27,XXREAL_0:2,XXREAL_0:1,A29;
 hence contradiction by A27,A11,A5,A9,A10;
 end;
 q.((n+1)+1) = q.(n+1) - 1 by A13,A30,A27,NAT_1:11;
 hence thesis by A28,A26,A27,XXREAL_0:2;
 end;
A31: for n be Nat holds P4[n] from NAT_1:sch 2(A24,A25);
A32: for n be Nat st 1<=n & n < card SOURCE holds 2 <= q.n
proof
 let n be Nat;
 assume A33: 1<=n & n < card SOURCE;
 then reconsider n0 =n-1 as Nat by NAT_1:21;
 n-1 < n - 0 by XREAL_1:15; then
 n0 < card SOURCE by A33,XXREAL_0:2; then
 A34:q.(n0+1) = q.1 - n0 by A31;
 n + 1 <= card SOURCE by NAT_1:13,A33;
 then n + 1 - 1 <= card SOURCE -1 by XREAL_1:9;
 then q.1 + 1 - (card (SOURCE) -1) <= q.1 + 1 - n by XREAL_1:13;
 hence 2 <= q.n by A12,A34;
end;

A35: for k be Nat st 1<= k & k < len Tseq holds
 ex X,Y be non empty finite Subset of BinFinTrees IndexedREAL st
 ex s being MinValueTree of X,
    t being MinValueTree of Y,
    w being finite binary DecoratedTree of IndexedREAL st
 Tseq.k = X & Y = X \ {s} &
 w in {MakeTree (t,s,MaxVl(X) + 1),MakeTree (s,t,MaxVl(X) + 1)} &
 Tseq.(k+1) = (X \ {t,s} ) \/ {w}
proof
 let k be Nat;
 assume A36:1<= k & k < len Tseq;
 A37: (ex u,v be object st u <> v & u in Tseq.k & v in Tseq.k) implies
 ex X,Y be non empty finite Subset of BinFinTrees IndexedREAL st
 ex s being MinValueTree of X,
    t being MinValueTree of Y,
    w being finite binary DecoratedTree of IndexedREAL st
 Tseq.k = X & Y = X \ {s} &
 w in {MakeTree (t,s,MaxVl(X) + 1),MakeTree (s,t,MaxVl(X) + 1)} &
 Tseq.(k+1) = (X \ {t,s} ) \/ {w} by A5,A36;
 ex Tseqk be finite set st Tseq.k = Tseqk & q.k = card Tseqk & q.k <> 0
 by FINSEQ_1:1,A36,A5,A8;
 hence thesis by A37,Lm3,A36,A32,A5;
end;
reconsider n1 = card (SOURCE) -1 as Nat by NAT_1:14,21;
card (SOURCE) -1 < card (SOURCE) - 0 by XREAL_1:15;
then
A38: q.(n1+1) = q.1 - n1 by A31;
A39: card (SOURCE) in Seg card SOURCE by FINSEQ_1:3;
consider Tseqk be finite set such that
A40: Tseq.(card SOURCE) = Tseqk
 & q. (card (SOURCE)) = card Tseqk & q. (card SOURCE) <> 0
 by FINSEQ_1:3,A8;
consider u be object such that
 A41: Tseqk = {u} by Lm4,A12,A38,A40;
 card SOURCE in dom Tseq by FINSEQ_1:def 3,A5,A39; then
A42: Tseq.(card SOURCE) in rng Tseq by FUNCT_1:3;
A43: u in Tseqk by TARSKI:def 1,A41;
 then reconsider T = u as DecoratedTree of IndexedREAL by A40,A42;
 A44: dom T is finite & T is binary by A43, Def2,A40,A42;
 reconsider T as finite binary DecoratedTree of IndexedREAL
 by A44,FINSET_1:10;
{T} = Tseq.( (len Tseq) ) by A40,A41,A5;
hence thesis by A35,A5,A8,A9,A31,A32,Def12;
end;
