equal
deleted
inserted
replaced
270 |
270 |
271 |
271 |
272 subsubsection \<open>Size\<close> |
272 subsubsection \<open>Size\<close> |
273 |
273 |
274 fun diff :: "'a tree \<Rightarrow> nat \<Rightarrow> nat" where |
274 fun diff :: "'a tree \<Rightarrow> nat \<Rightarrow> nat" where |
275 "diff Leaf 0 = 0" | |
275 "diff Leaf _ = 0" | |
276 "diff (Node l x r) n = (if n=0 then 1 else if even n then diff r (n div 2 - 1) else diff l (n div 2))" |
276 "diff (Node l x r) n = (if n=0 then 1 else if even n then diff r (n div 2 - 1) else diff l (n div 2))" |
277 |
277 |
278 fun size_fast :: "'a tree \<Rightarrow> nat" where |
278 fun size_fast :: "'a tree \<Rightarrow> nat" where |
279 "size_fast Leaf = 0" | |
279 "size_fast Leaf = 0" | |
280 "size_fast (Node l x r) = (let n = size_fast r in 1 + 2*n + diff l n)" |
280 "size_fast (Node l x r) = (let n = size_fast r in 1 + 2*n + diff l n)" |