equal
deleted
inserted
replaced
1 (* |
1 (* |
2 Author: Tobias Nipkow |
2 Author: Tobias Nipkow |
3 Invariants are under development |
3 |
|
4 Added trivial cases to function `adjust' to obviate invariants. |
4 *) |
5 *) |
5 |
6 |
6 section \<open>An AA Tree Implementation of Sets\<close> |
7 section \<open>AA Tree Implementation of Sets\<close> |
7 |
8 |
8 theory AA_Set |
9 theory AA_Set |
9 imports |
10 imports |
10 Isin2 |
11 Isin2 |
11 Cmp |
12 Cmp |
14 type_synonym 'a aa_tree = "('a,nat) tree" |
15 type_synonym 'a aa_tree = "('a,nat) tree" |
15 |
16 |
16 fun lvl :: "'a aa_tree \<Rightarrow> nat" where |
17 fun lvl :: "'a aa_tree \<Rightarrow> nat" where |
17 "lvl Leaf = 0" | |
18 "lvl Leaf = 0" | |
18 "lvl (Node lv _ _ _) = lv" |
19 "lvl (Node lv _ _ _) = lv" |
19 |
20 (* |
20 fun invar :: "'a aa_tree \<Rightarrow> bool" where |
21 fun invar :: "'a aa_tree \<Rightarrow> bool" where |
21 "invar Leaf = True" | |
22 "invar Leaf = True" | |
22 "invar (Node h l a r) = |
23 "invar (Node h l a r) = |
23 (invar l \<and> invar r \<and> |
24 (invar l \<and> invar r \<and> |
24 h = lvl l + 1 \<and> (h = lvl r + 1 \<or> (\<exists>lr b rr. r = Node h lr b rr \<and> h = lvl rr + 1)))" |
25 h = lvl l + 1 \<and> (h = lvl r + 1 \<or> (\<exists>lr b rr. r = Node h lr b rr \<and> h = lvl rr + 1)))" |
25 |
26 *) |
26 fun skew :: "'a aa_tree \<Rightarrow> 'a aa_tree" where |
27 fun skew :: "'a aa_tree \<Rightarrow> 'a aa_tree" where |
27 "skew (Node lva (Node lvb t1 b t2) a t3) = |
28 "skew (Node lva (Node lvb t1 b t2) a t3) = |
28 (if lva = lvb then Node lva t1 b (Node lva t2 a t3) else Node lva (Node lvb t1 b t2) a t3)" | |
29 (if lva = lvb then Node lva t1 b (Node lva t2 a t3) else Node lva (Node lvb t1 b t2) a t3)" | |
29 "skew t = t" |
30 "skew t = t" |
30 |
31 |
103 by(induction t) (auto simp: ins_list_simps inorder_split inorder_skew) |
104 by(induction t) (auto simp: ins_list_simps inorder_split inorder_skew) |
104 |
105 |
105 subsubsection "Proofs for delete" |
106 subsubsection "Proofs for delete" |
106 |
107 |
107 lemma del_maxD: |
108 lemma del_maxD: |
108 "\<lbrakk> del_max t = (t',x); t \<noteq> Leaf; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
109 "\<lbrakk> del_max t = (t',x); t \<noteq> Leaf \<rbrakk> \<Longrightarrow> inorder t' @ [x] = inorder t" |
109 inorder t' @ [x] = inorder t" |
|
110 by(induction t arbitrary: t' rule: del_max.induct) |
110 by(induction t arbitrary: t' rule: del_max.induct) |
111 (auto simp: sorted_lems split: prod.splits) |
111 (auto simp: sorted_lems split: prod.splits) |
112 |
112 |
113 lemma inorder_adjust: "t \<noteq> Leaf \<Longrightarrow> inorder(adjust t) = inorder t" |
113 lemma inorder_adjust: "t \<noteq> Leaf \<Longrightarrow> inorder(adjust t) = inorder t" |
114 by(induction t) |
114 by(induction t) |