Theory Tree

theory Tree
imports Main
(* Author: Tobias Nipkow *)

section ‹Binary Tree›

theory Tree
imports Main
begin

datatype 'a tree =
  is_Leaf: Leaf ("⟨⟩") |
  Node (left: "'a tree") (val: 'a) (right: "'a tree") ("(1⟨_,/ _,/ _⟩)")
  where
    "left Leaf = Leaf"
  | "right Leaf = Leaf"
datatype_compat tree

text‹Can be seen as counting the number of leaves rather than nodes:›

definition size1 :: "'a tree ⇒ nat" where
"size1 t = size t + 1"

lemma size1_simps[simp]:
  "size1 ⟨⟩ = 1"
  "size1 ⟨l, x, r⟩ = size1 l + size1 r"
by (simp_all add: size1_def)

lemma size1_ge0[simp]: "0 < size1 t"
by (simp add: size1_def)

lemma size_0_iff_Leaf: "size t = 0 ⟷ t = Leaf"
by(cases t) auto

lemma neq_Leaf_iff: "(t ≠ ⟨⟩) = (∃l a r. t = ⟨l, a, r⟩)"
by (cases t) auto

lemma finite_set_tree[simp]: "finite(set_tree t)"
by(induction t) auto

lemma size_map_tree[simp]: "size (map_tree f t) = size t"
by (induction t) auto

lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
by (simp add: size1_def)


subsection "The Height"

class height = fixes height :: "'a ⇒ nat"

instantiation tree :: (type)height
begin

fun height_tree :: "'a tree => nat" where
"height Leaf = 0" |
"height (Node t1 a t2) = max (height t1) (height t2) + 1"

instance ..

end

lemma height_map_tree[simp]: "height (map_tree f t) = height t"
by (induction t) auto

lemma size1_height: "size t + 1 ≤ 2 ^ height (t::'a tree)"
proof(induction t)
  case (Node l a r)
  show ?case
  proof (cases "height l ≤ height r")
    case True
    have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
    also have "size l + 1 ≤ 2 ^ height l" by(rule Node.IH(1))
    also have "size r + 1 ≤ 2 ^ height r" by(rule Node.IH(2))
    also have "(2::nat) ^ height l ≤ 2 ^ height r" using True by simp
    finally show ?thesis using True by (auto simp: max_def mult_2)
  next
    case False
    have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
    also have "size l + 1 ≤ 2 ^ height l" by(rule Node.IH(1))
    also have "size r + 1 ≤ 2 ^ height r" by(rule Node.IH(2))
    also have "(2::nat) ^ height r ≤ 2 ^ height l" using False by simp
    finally show ?thesis using False by (auto simp: max_def mult_2)
  qed
qed simp


subsection "The set of subtrees"

fun subtrees :: "'a tree ⇒ 'a tree set" where
"subtrees ⟨⟩ = {⟨⟩}" |
"subtrees (⟨l, a, r⟩) = insert ⟨l, a, r⟩ (subtrees l ∪ subtrees r)"

lemma set_treeE: "a ∈ set_tree t ⟹ ∃l r. ⟨l, a, r⟩ ∈ subtrees t"
by (induction t)(auto)

lemma Node_notin_subtrees_if[simp]: "a ∉ set_tree t ⟹ Node l a r ∉ subtrees t"
by (induction t) auto

lemma in_set_tree_if: "⟨l, a, r⟩ ∈ subtrees t ⟹ a ∈ set_tree t"
by (metis Node_notin_subtrees_if)


subsection "List of entries"

fun preorder :: "'a tree ⇒ 'a list" where
"preorder ⟨⟩ = []" |
"preorder ⟨l, x, r⟩ = x # preorder l @ preorder r"

fun inorder :: "'a tree ⇒ 'a list" where
"inorder ⟨⟩ = []" |
"inorder ⟨l, x, r⟩ = inorder l @ [x] @ inorder r"

lemma set_inorder[simp]: "set (inorder t) = set_tree t"
by (induction t) auto

lemma set_preorder[simp]: "set (preorder t) = set_tree t"
by (induction t) auto

lemma length_preorder[simp]: "length (preorder t) = size t"
by (induction t) auto

lemma length_inorder[simp]: "length (inorder t) = size t"
by (induction t) auto

lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
by (induction t) auto

lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
by (induction t) auto


subsection ‹Binary Search Tree predicate›

fun (in linorder) bst :: "'a tree ⇒ bool" where
"bst ⟨⟩ ⟷ True" |
"bst ⟨l, a, r⟩ ⟷ bst l ∧ bst r ∧ (∀x∈set_tree l. x < a) ∧ (∀x∈set_tree r. a < x)"

text‹In case there are duplicates:›

fun (in linorder) bst_eq :: "'a tree ⇒ bool" where
"bst_eq ⟨⟩ ⟷ True" |
"bst_eq ⟨l,a,r⟩ ⟷
 bst_eq l ∧ bst_eq r ∧ (∀x∈set_tree l. x ≤ a) ∧ (∀x∈set_tree r. a ≤ x)"

lemma (in linorder) bst_eq_if_bst: "bst t ⟹ bst_eq t"
by (induction t) (auto)

lemma (in linorder) bst_eq_imp_sorted: "bst_eq t ⟹ sorted (inorder t)"
apply (induction t)
 apply(simp)
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)

lemma (in linorder) distinct_preorder_if_bst: "bst t ⟹ distinct (preorder t)"
apply (induction t)
 apply simp
apply(fastforce elim: order.asym)
done

lemma (in linorder) distinct_inorder_if_bst: "bst t ⟹ distinct (inorder t)"
apply (induction t)
 apply simp
apply(fastforce elim: order.asym)
done


subsection "The heap predicate"

fun heap :: "'a::linorder tree ⇒ bool" where
"heap Leaf = True" |
"heap (Node l m r) =
  (heap l ∧ heap r ∧ (∀x ∈ set_tree l ∪ set_tree r. m ≤ x))"


subsection "Function ‹mirror›"

fun mirror :: "'a tree ⇒ 'a tree" where
"mirror ⟨⟩ = Leaf" |
"mirror ⟨l,x,r⟩ = ⟨mirror r, x, mirror l⟩"

lemma mirror_Leaf[simp]: "mirror t = ⟨⟩ ⟷ t = ⟨⟩"
by (induction t) simp_all

lemma size_mirror[simp]: "size(mirror t) = size t"
by (induction t) simp_all

lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
by (simp add: size1_def)

lemma height_mirror[simp]: "height(mirror t) = height t"
by (induction t) simp_all

lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
by (induction t) simp_all

lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
by (induction t) simp_all

lemma mirror_mirror[simp]: "mirror(mirror t) = t"
by (induction t) simp_all

end