Section PBTREE2BTREE.
Variable A : Type.
Variable eqA_dec : forall a b : A, {a = b} + {a <> b}.
Variable empty : A.
(* Turn a partial tree into a binary tree *)
Fixpoint to_btree (a : pbtree A) : btree A :=
match a with
| pbleaf b => leaf b
| pbleft l1 => to_btree l1
| pbright l1 => to_btree l1
| pbnode l1 l2 => node (to_btree l1) (to_btree l2)
end.
Proof using.
intros a b; generalize a; elim b; clear a b; simpl in |- *; auto.
intros a a0 H; inversion H; auto.
intros p H a H0; apply H; auto; inversion H0; auto.
intros p H a H0; apply H; auto; inversion H0; auto.
intros p H p0 H0 a H1; inversion H1; auto.
Qed.
Proof using.
intros a b; generalize a; elim b; clear a b; simpl in |- *; auto.
intros a a0 H; inversion H; auto.
intros p H p0 H0 a H1.
inversion H1; auto.
Qed.
Proof using.
intros t; elim t; simpl in |- *; auto.
intros p H p0 H0; apply f_equal2 with (f := app (A:=A)); auto.
Qed.
Proof using.
intros a H.
apply all_leaves_unique.
rewrite to_btree_all_leaves.
apply all_pbleaves_ulist; auto.
Qed.
Proof using.
intros a H.
apply all_pbleaves_unique.
rewrite <- to_btree_all_leaves.
apply all_leaves_ulist; auto.
Qed.

(* For each key, the resulting code of the computed tree is always smaller *)
Theorem to_btree_smaller :
forall t a,
length (find_code eqA_dec a (compute_code (to_btree t))) <=
length (find_code eqA_dec a (compute_pbcode t)).