| author | wenzelm | 
| Fri, 04 Jul 2014 15:50:28 +0200 | |
| changeset 57507 | a609065c9e15 | 
| parent 57450 | 2baecef3207f | 
| child 57530 | 439f881c8744 | 
| permissions | -rw-r--r-- | 
| 57250 | 1 | (* Author: Tobias Nipkow *) | 
| 2 | ||
| 3 | header {* Binary Tree *}
 | |
| 4 | ||
| 5 | theory Tree | |
| 6 | imports Main | |
| 7 | begin | |
| 8 | ||
| 57449 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 9 | datatype_new 'a tree = Leaf | Node (left: "'a tree") (val: 'a) (right: "'a tree") | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 10 | where | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 11 | "left Leaf = Leaf" | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 12 | | "right Leaf = Leaf" | 
| 57250 | 13 | |
| 57449 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 14 | lemma neq_Leaf_iff: "(t \<noteq> Leaf) = (\<exists>l a r. t = Node l a r)" | 
| 57450 | 15 | by (cases t) auto | 
| 57250 | 16 | |
| 17 | fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where | |
| 57450 | 18 |   "subtrees Leaf = {Leaf}" |
 | 
| 19 | "subtrees (Node l a r) = insert (Node l a r) (subtrees l \<union> subtrees r)" | |
| 57250 | 20 | |
| 57449 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 21 | lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. Node l a r \<in> subtrees t" | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 22 | by (induction t)(auto) | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 23 | |
| 57450 | 24 | lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t" | 
| 57449 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 25 | by (induction t) auto | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 26 | |
| 57450 | 27 | lemma in_set_tree_if: "Node l a r \<in> subtrees t \<Longrightarrow> a \<in> set_tree t" | 
| 57449 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 28 | by (metis Node_notin_subtrees_if) | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 29 | |
| 57250 | 30 | fun inorder :: "'a tree \<Rightarrow> 'a list" where | 
| 57450 | 31 | "inorder Leaf = []" | | 
| 32 | "inorder (Node l x r) = inorder l @ [x] @ inorder r" | |
| 57250 | 33 | |
| 57449 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 34 | lemma set_inorder[simp]: "set (inorder t) = set_tree t" | 
| 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 35 | by (induction t) auto | 
| 57250 | 36 | |
| 57449 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 hoelzl parents: 
57250diff
changeset | 37 | subsection {* Binary Search Tree predicate *}
 | 
| 57250 | 38 | |
| 57450 | 39 | fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where | 
| 40 | "bst Leaf \<longleftrightarrow> True" | | |
| 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)" | |
| 57250 | 42 | |
| 57450 | 43 | lemma (in linorder) bst_imp_sorted: "bst t \<Longrightarrow> sorted (inorder t)" | 
| 44 | by (induction t) (auto simp: sorted_append sorted_Cons intro: less_imp_le less_trans) | |
| 57250 | 45 | |
| 46 | end |