作为我自己的练习,我试图在二叉树上定义和证明一些属性。
这是我的 btree 定义:
Inductive tree : Type :=
| Leaf
| Node (x : nat) (t1 : tree) (t2 : tree).
我想证明的第一个属性是 btree 的高度至少log2(n+1)
是n
节点数。所以我简单地定义countNodes
了:
Fixpoint countNodes (t : tree) :=
match t with
| Leaf => 0
| Node _ t1 t2 => 1 + (countNodes t1) + (countNodes t2)
end.
并且heightTree
:
Fixpoint heightTree (t : tree) :=
match t with
| Leaf => 0
| Node _ t1 t2 => 1 + (max (heightTree t1) (heightTree t2))
end.
现在,这是我的(不完整的)定理。任何人都可以向我提供有关如何完成此归纳的提示吗?看来我应该有 2 个基本案例(Leaf
和Node _ Leaf Leaf
),我该怎么做?
Theorem height_of_tree_is_at_least_log2_Sn : forall t : tree,
log2 (1 + (countNodes t)) <= heightTree t.
Proof.
intros.
induction t.
- simpl. rewrite Nat.log2_1. apply le_n.
-
剩余目标:
1 goal (ID 26)
x : nat
t1, t2 : tree
IHt1 : log2 (1 + countNodes t1) <= heightTree t1
IHt2 : log2 (1 + countNodes t2) <= heightTree t2
============================
log2 (1 + countNodes (Node x t1 t2)) <= heightTree (Node x t1 t2)
PS:当我试图证明任何节点的度数只能是 0、1 或 2 时,我遇到了类似的问题。在归纳步骤上也有问题。