Section Build.
Variable A : Type.
Variable f : A -> nat.
(* Iterative the one step predicate *)
Inductive build : list (btree A) -> btree A -> Prop :=
| build_one : forall t : btree A, build (t :: nil) t
| build_step :
forall (t : btree A) (l1 l2 : list (btree A)),
one_step f l1 l2 -> build l2 t -> build l1 t.
Proof using.
intros l t H; elim H; clear H l t; auto.
intros t l1 l2 (l3, (t1, (t2, (HH, (HH1, HH2))))) H0 H1; try assumption.
apply cover_node with (1 := HH1); auto.
apply cover_permutation with (2 := HH2); auto.
Qed.

(* Building is compatible with the weight *)
Theorem build_comp :
forall (l1 l2 : list (btree A)) (t1 t2 : btree A),
build l1 t1 ->
build l2 t2 ->
weight_tree_list f l1 = weight_tree_list f l2 ->
same_sum_leaves f l1 l2 -> weight_tree f t1 = weight_tree f t2.