71814
|
1 |
(* Tobias Nipkow *)
|
|
2 |
|
71818
|
3 |
section "AVL Tree with Balance Factors"
|
71814
|
4 |
|
|
5 |
theory AVL_Bal_Set
|
|
6 |
imports
|
|
7 |
Cmp
|
|
8 |
Isin2
|
|
9 |
begin
|
|
10 |
|
|
11 |
datatype bal = Lh | Bal | Rh
|
|
12 |
|
|
13 |
type_synonym 'a tree_bal = "('a * bal) tree"
|
|
14 |
|
|
15 |
text \<open>Invariant:\<close>
|
|
16 |
|
|
17 |
fun avl :: "'a tree_bal \<Rightarrow> bool" where
|
|
18 |
"avl Leaf = True" |
|
|
19 |
"avl (Node l (a,b) r) =
|
|
20 |
((case b of
|
|
21 |
Bal \<Rightarrow> height r = height l |
|
|
22 |
Lh \<Rightarrow> height l = height r + 1 |
|
|
23 |
Rh \<Rightarrow> height r = height l + 1)
|
|
24 |
\<and> avl l \<and> avl r)"
|
|
25 |
|
|
26 |
|
|
27 |
subsection \<open>Code\<close>
|
|
28 |
|
71818
|
29 |
datatype 'a tree_bal2 = Same "'a tree_bal" | Diff "'a tree_bal"
|
71814
|
30 |
|
71818
|
31 |
fun tree :: "'a tree_bal2 \<Rightarrow> 'a tree_bal" where
|
71814
|
32 |
"tree(Same t) = t" |
|
|
33 |
"tree(Diff t) = t"
|
|
34 |
|
71820
|
35 |
fun rot2 where
|
|
36 |
"rot2 A a B c C = (case B of
|
|
37 |
(Node B\<^sub>1 (b, bb) B\<^sub>2) \<Rightarrow>
|
|
38 |
let b\<^sub>1 = if bb = Rh then Lh else Bal;
|
|
39 |
b\<^sub>2 = if bb = Lh then Rh else Bal
|
|
40 |
in Node (Node A (a,b\<^sub>1) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,b\<^sub>2) C))"
|
71814
|
41 |
|
71818
|
42 |
fun balL :: "'a tree_bal2 \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal2" where
|
71815
|
43 |
"balL AB' c bc C = (case AB' of
|
|
44 |
Same AB \<Rightarrow> Same (Node AB (c,bc) C) |
|
|
45 |
Diff AB \<Rightarrow> (case bc of
|
|
46 |
Bal \<Rightarrow> Diff (Node AB (c,Lh) C) |
|
|
47 |
Rh \<Rightarrow> Same (Node AB (c,Bal) C) |
|
71814
|
48 |
Lh \<Rightarrow> Same(case AB of
|
71816
|
49 |
Node A (a,Lh) B \<Rightarrow> Node A (a,Bal) (Node B (c,Bal) C) |
|
71820
|
50 |
Node A (a,Rh) B \<Rightarrow> rot2 A a B c C)))"
|
71814
|
51 |
|
71818
|
52 |
fun balR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal2 \<Rightarrow> 'a tree_bal2" where
|
71815
|
53 |
"balR A a ba BC' = (case BC' of
|
|
54 |
Same BC \<Rightarrow> Same (Node A (a,ba) BC) |
|
|
55 |
Diff BC \<Rightarrow> (case ba of
|
|
56 |
Bal \<Rightarrow> Diff (Node A (a,Rh) BC) |
|
|
57 |
Lh \<Rightarrow> Same (Node A (a,Bal) BC) |
|
71814
|
58 |
Rh \<Rightarrow> Same(case BC of
|
71816
|
59 |
Node B (c,Rh) C \<Rightarrow> Node (Node A (a,Bal) B) (c,Bal) C |
|
71820
|
60 |
Node B (c,Lh) C \<Rightarrow> rot2 A a B c C)))"
|
71814
|
61 |
|
71818
|
62 |
fun insert :: "'a::linorder \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal2" where
|
71814
|
63 |
"insert x Leaf = Diff(Node Leaf (x, Bal) Leaf)" |
|
|
64 |
"insert x (Node l (a, b) r) = (case cmp x a of
|
|
65 |
EQ \<Rightarrow> Same(Node l (a, b) r) |
|
|
66 |
LT \<Rightarrow> balL (insert x l) a b r |
|
|
67 |
GT \<Rightarrow> balR l a b (insert x r))"
|
|
68 |
|
71818
|
69 |
fun baldR :: "'a tree_bal \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal2 \<Rightarrow> 'a tree_bal2" where
|
71816
|
70 |
"baldR AB c bc C' = (case C' of
|
|
71 |
Same C \<Rightarrow> Same (Node AB (c,bc) C) |
|
|
72 |
Diff C \<Rightarrow> (case bc of
|
|
73 |
Bal \<Rightarrow> Same (Node AB (c,Lh) C) |
|
|
74 |
Rh \<Rightarrow> Diff (Node AB (c,Bal) C) |
|
71814
|
75 |
Lh \<Rightarrow> (case AB of
|
71816
|
76 |
Node A (a,Lh) B \<Rightarrow> Diff(Node A (a,Bal) (Node B (c,Bal) C)) |
|
|
77 |
Node A (a,Bal) B \<Rightarrow> Same(Node A (a,Rh) (Node B (c,Lh) C)) |
|
71820
|
78 |
Node A (a,Rh) B \<Rightarrow> Diff(rot2 A a B c C))))"
|
71814
|
79 |
|
71818
|
80 |
fun baldL :: "'a tree_bal2 \<Rightarrow> 'a \<Rightarrow> bal \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal2" where
|
71816
|
81 |
"baldL A' a ba BC = (case A' of
|
|
82 |
Same A \<Rightarrow> Same (Node A (a,ba) BC) |
|
|
83 |
Diff A \<Rightarrow> (case ba of
|
|
84 |
Bal \<Rightarrow> Same (Node A (a,Rh) BC) |
|
|
85 |
Lh \<Rightarrow> Diff (Node A (a,Bal) BC) |
|
71814
|
86 |
Rh \<Rightarrow> (case BC of
|
71816
|
87 |
Node B (c,Rh) C \<Rightarrow> Diff(Node (Node A (a,Bal) B) (c,Bal) C) |
|
|
88 |
Node B (c,Bal) C \<Rightarrow> Same(Node (Node A (a,Rh) B) (c,Lh) C) |
|
71820
|
89 |
Node B (c,Lh) C \<Rightarrow> Diff(rot2 A a B c C))))"
|
71814
|
90 |
|
71818
|
91 |
fun split_max :: "'a tree_bal \<Rightarrow> 'a tree_bal2 * 'a" where
|
71814
|
92 |
"split_max (Node l (a, ba) r) =
|
|
93 |
(if r = Leaf then (Diff l,a) else let (r',a') = split_max r in (baldR l a ba r', a'))"
|
|
94 |
|
71818
|
95 |
fun delete :: "'a::linorder \<Rightarrow> 'a tree_bal \<Rightarrow> 'a tree_bal2" where
|
71814
|
96 |
"delete _ Leaf = Same Leaf" |
|
|
97 |
"delete x (Node l (a, ba) r) =
|
|
98 |
(case cmp x a of
|
|
99 |
EQ \<Rightarrow> if l = Leaf then Diff r
|
|
100 |
else let (l', a') = split_max l in baldL l' a' ba r |
|
|
101 |
LT \<Rightarrow> baldL (delete x l) a ba r |
|
|
102 |
GT \<Rightarrow> baldR l a ba (delete x r))"
|
|
103 |
|
|
104 |
lemmas split_max_induct = split_max.induct[case_names Node Leaf]
|
|
105 |
|
71819
|
106 |
lemmas splits = if_splits tree.splits tree_bal2.splits bal.splits
|
71814
|
107 |
|
|
108 |
subsection \<open>Proofs\<close>
|
|
109 |
|
|
110 |
lemma insert_Diff1[simp]: "insert x t \<noteq> Diff Leaf"
|
|
111 |
by (cases t)(auto split!: splits)
|
|
112 |
|
|
113 |
lemma insert_Diff2[simp]: "insert x t = Diff (Node l (a,Bal) r) \<longleftrightarrow> t = Leaf \<and> a = x \<and> l=Leaf \<and> r=Leaf"
|
|
114 |
by (cases t)(auto split!: splits)
|
|
115 |
|
|
116 |
lemma insert_Diff3[simp]: "insert x t \<noteq> Diff (Node l (a,Rh) Leaf)"
|
|
117 |
by (cases t)(auto split!: splits)
|
|
118 |
|
|
119 |
lemma insert_Diff4[simp]: "insert x t \<noteq> Diff (Node Leaf (a,Lh) r)"
|
|
120 |
by (cases t)(auto split!: splits)
|
|
121 |
|
|
122 |
|
|
123 |
subsubsection "Proofs for insert"
|
|
124 |
|
|
125 |
theorem inorder_insert:
|
|
126 |
"\<lbrakk> avl t; sorted(inorder t) \<rbrakk> \<Longrightarrow> inorder(tree(insert x t)) = ins_list x (inorder t)"
|
|
127 |
by(induction t) (auto simp: ins_list_simps split!: splits)
|
|
128 |
|
|
129 |
lemma avl_insert_case: "avl t \<Longrightarrow> case insert x t of
|
|
130 |
Same t' \<Rightarrow> avl t' \<and> height t' = height t |
|
|
131 |
Diff t' \<Rightarrow> avl t' \<and> height t' = height t + 1"
|
|
132 |
apply(induction x t rule: insert.induct)
|
|
133 |
apply(auto simp: max_absorb1 split!: splits)
|
|
134 |
done
|
|
135 |
|
|
136 |
corollary avl_insert: "avl t \<Longrightarrow> avl(tree(insert x t))"
|
|
137 |
using avl_insert_case[of t x] by (simp split: splits)
|
|
138 |
|
|
139 |
|
|
140 |
subsubsection "Proofs for delete"
|
|
141 |
|
|
142 |
lemma inorder_baldL:
|
|
143 |
"\<lbrakk> ba = Rh \<longrightarrow> r \<noteq> Leaf; avl r \<rbrakk>
|
|
144 |
\<Longrightarrow> inorder (tree(baldL l a ba r)) = inorder (tree l) @ a # inorder r"
|
|
145 |
by (auto split: splits)
|
|
146 |
|
|
147 |
lemma inorder_baldR:
|
|
148 |
"\<lbrakk> ba = Lh \<longrightarrow> l \<noteq> Leaf; avl l \<rbrakk>
|
|
149 |
\<Longrightarrow> inorder (tree(baldR l a ba r)) = inorder l @ a # inorder (tree r)"
|
|
150 |
by (auto split: splits)
|
|
151 |
|
|
152 |
lemma avl_split_max:
|
|
153 |
"\<lbrakk> split_max t = (t',a); avl t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow> case t' of
|
|
154 |
Same t' \<Rightarrow> avl t' \<and> height t = height t' |
|
|
155 |
Diff t' \<Rightarrow> avl t' \<and> height t = height t' + 1"
|
|
156 |
apply(induction t arbitrary: t' a rule: split_max_induct)
|
|
157 |
apply(fastforce simp: max_absorb1 max_absorb2 split!: splits prod.splits)
|
|
158 |
apply simp
|
|
159 |
done
|
|
160 |
|
|
161 |
lemma avl_delete_case: "avl t \<Longrightarrow> case delete x t of
|
|
162 |
Same t' \<Rightarrow> avl t' \<and> height t = height t' |
|
|
163 |
Diff t' \<Rightarrow> avl t' \<and> height t = height t' + 1"
|
|
164 |
apply(induction x t rule: delete.induct)
|
|
165 |
apply(auto simp: max_absorb1 max_absorb2 dest: avl_split_max split!: splits prod.splits)
|
|
166 |
done
|
|
167 |
|
|
168 |
corollary avl_delete: "avl t \<Longrightarrow> avl(tree(delete x t))"
|
|
169 |
using avl_delete_case[of t x] by(simp split: splits)
|
|
170 |
|
|
171 |
lemma inorder_split_maxD:
|
|
172 |
"\<lbrakk> split_max t = (t',a); t \<noteq> Leaf; avl t \<rbrakk> \<Longrightarrow>
|
|
173 |
inorder (tree t') @ [a] = inorder t"
|
|
174 |
apply(induction t arbitrary: t' rule: split_max.induct)
|
|
175 |
apply(fastforce split!: splits prod.splits)
|
|
176 |
apply simp
|
|
177 |
done
|
|
178 |
|
|
179 |
lemma neq_Leaf_if_height_neq_0[simp]: "height t \<noteq> 0 \<Longrightarrow> t \<noteq> Leaf"
|
|
180 |
by auto
|
|
181 |
|
|
182 |
theorem inorder_delete:
|
|
183 |
"\<lbrakk> avl t; sorted(inorder t) \<rbrakk> \<Longrightarrow> inorder (tree(delete x t)) = del_list x (inorder t)"
|
|
184 |
apply(induction t rule: tree2_induct)
|
|
185 |
apply(auto simp: del_list_simps inorder_baldL inorder_baldR avl_delete inorder_split_maxD
|
|
186 |
simp del: baldR.simps baldL.simps split!: splits prod.splits)
|
|
187 |
done
|
|
188 |
|
|
189 |
|
|
190 |
subsubsection \<open>Set Implementation\<close>
|
|
191 |
|
|
192 |
interpretation S: Set_by_Ordered
|
|
193 |
where empty = Leaf and isin = isin
|
|
194 |
and insert = "\<lambda>x t. tree(insert x t)"
|
|
195 |
and delete = "\<lambda>x t. tree(delete x t)"
|
|
196 |
and inorder = inorder and inv = avl
|
|
197 |
proof (standard, goal_cases)
|
|
198 |
case 1 show ?case by (simp)
|
|
199 |
next
|
|
200 |
case 2 thus ?case by(simp add: isin_set_inorder)
|
|
201 |
next
|
|
202 |
case 3 thus ?case by(simp add: inorder_insert)
|
|
203 |
next
|
|
204 |
case 4 thus ?case by(simp add: inorder_delete)
|
|
205 |
next
|
|
206 |
case 5 thus ?case by (simp add: empty_def)
|
|
207 |
next
|
|
208 |
case 6 thus ?case by (simp add: avl_insert)
|
|
209 |
next
|
|
210 |
case 7 thus ?case by (simp add: avl_delete)
|
|
211 |
qed
|
|
212 |
|
|
213 |
end
|