src/HOL/Library/Tree.thy
changeset 57450 2baecef3207f
parent 57449 f81da03b9ebd
child 57530 439f881c8744
equal deleted inserted replaced
57449:f81da03b9ebd 57450:2baecef3207f
    10   where
    10   where
    11     "left Leaf = Leaf"
    11     "left Leaf = Leaf"
    12   | "right Leaf = Leaf"
    12   | "right Leaf = Leaf"
    13 
    13 
    14 lemma neq_Leaf_iff: "(t \<noteq> Leaf) = (\<exists>l a r. t = Node l a r)"
    14 lemma neq_Leaf_iff: "(t \<noteq> Leaf) = (\<exists>l a r. t = Node l a r)"
    15 by (cases t) auto
    15   by (cases t) auto
    16 
    16 
    17 fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
    17 fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
    18 "subtrees Leaf = {Leaf}" |
    18   "subtrees Leaf = {Leaf}" |
    19 "subtrees (Node l a r) = insert (Node l a r) (subtrees l \<union> subtrees r)"
    19   "subtrees (Node l a r) = insert (Node l a r) (subtrees l \<union> subtrees r)"
    20 
    20 
    21 lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. Node l a r \<in> subtrees t"
    21 lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. Node l a r \<in> subtrees t"
    22   by (induction t)(auto)
    22   by (induction t)(auto)
    23 
    23 
    24 lemma Node_notin_subtrees_if[simp]:
    24 lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
    25   "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
       
    26   by (induction t) auto
    25   by (induction t) auto
    27 
    26 
    28 lemma in_set_tree_if:
    27 lemma in_set_tree_if: "Node l a r \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
    29   "Node l a r \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
       
    30   by (metis Node_notin_subtrees_if)
    28   by (metis Node_notin_subtrees_if)
    31 
    29 
    32 fun inorder :: "'a tree \<Rightarrow> 'a list" where
    30 fun inorder :: "'a tree \<Rightarrow> 'a list" where
    33 "inorder Leaf = []" |
    31   "inorder Leaf = []" |
    34 "inorder (Node l x r) = inorder l @ [x] @ inorder r"
    32   "inorder (Node l x r) = inorder l @ [x] @ inorder r"
    35 
    33 
    36 lemma set_inorder[simp]: "set (inorder t) = set_tree t"
    34 lemma set_inorder[simp]: "set (inorder t) = set_tree t"
    37   by (induction t) auto
    35   by (induction t) auto
    38 
    36 
    39 subsection {* Binary Search Tree predicate *}
    37 subsection {* Binary Search Tree predicate *}
    40 
    38 
    41 inductive bst :: "'a::linorder tree \<Rightarrow> bool" where
    39 fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
    42 Leaf[intro!, simp]: "bst Leaf" |
    40   "bst Leaf \<longleftrightarrow> True" |
    43 Node: "bst l \<Longrightarrow> bst r \<Longrightarrow> (\<And>x. x \<in> set_tree l \<Longrightarrow> x < a) \<Longrightarrow> (\<And>x. x \<in> set_tree r \<Longrightarrow> a < x) \<Longrightarrow>
    41   "bst (Node l a r) \<longleftrightarrow> bst l \<and> bst r \<and> (\<forall>x\<in>set_tree l. x < a) \<and> (\<forall>x\<in>set_tree r. a < x)"
    44     bst (Node l a r)"
       
    45 
    42 
    46 lemma bst_imp_sorted: "bst t \<Longrightarrow> sorted (inorder t)"
    43 lemma (in linorder) bst_imp_sorted: "bst t \<Longrightarrow> sorted (inorder t)"
    47   by (induction rule: bst.induct) (auto simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
    44   by (induction t) (auto simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
    48 
    45 
    49 end
    46 end