|
1 (* Tobias Nipkow *) |
|
2 |
|
3 section \<open>Creating a Balanced Tree from a List\<close> |
|
4 |
|
5 theory Balance_List |
|
6 imports |
|
7 "~~/src/HOL/Library/Tree" |
|
8 "~~/src/HOL/Library/Float" |
|
9 begin |
|
10 |
|
11 fun bal :: "'a list \<Rightarrow> nat \<Rightarrow> 'a tree * 'a list" where |
|
12 "bal xs n = (if n=0 then (Leaf,xs) else |
|
13 (let m = n div 2; |
|
14 (l, ys) = bal xs m; |
|
15 (r, zs) = bal (tl ys) (n-1-m) |
|
16 in (Node l (hd ys) r, zs)))" |
|
17 |
|
18 declare bal.simps[simp del] |
|
19 |
|
20 definition "balance xs = fst (bal xs (length xs))" |
|
21 |
|
22 lemma bal_inorder: |
|
23 "bal xs n = (t,ys) \<Longrightarrow> n \<le> length xs \<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs" |
|
24 proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
25 case (1 xs n) show ?case |
|
26 proof cases |
|
27 assume "n = 0" thus ?thesis using 1 by (simp add: bal.simps) |
|
28 next |
|
29 assume [arith]: "n \<noteq> 0" |
|
30 let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1" |
|
31 from "1.prems" obtain l r xs' where |
|
32 b1: "bal xs ?n1 = (l,xs')" and |
|
33 b2: "bal (tl xs') ?n2 = (r,ys)" and |
|
34 t: "t = \<langle>l, hd xs', r\<rangle>" |
|
35 using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) |
|
36 have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs" |
|
37 using b1 "1.prems" by(intro "1.IH"(1)) auto |
|
38 have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')" |
|
39 using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto |
|
40 have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp |
|
41 hence "hd (drop ?n1 xs) # take ?n2 (tl (drop ?n1 xs)) = take (?n2 + 1) (drop ?n1 xs)" |
|
42 by (metis Suc_eq_plus1 take_Suc) |
|
43 hence *: "inorder t = take n xs" using t IH1 IH2 |
|
44 using take_add[of ?n1 "?n2+1" xs] by(simp) |
|
45 have "n - n div 2 + n div 2 = n" by simp |
|
46 hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric]) |
|
47 thus ?thesis using * by blast |
|
48 qed |
|
49 qed |
|
50 |
|
51 corollary balance_inorder: "inorder(balance xs) = xs" |
|
52 using bal_inorder[of xs "length xs"] |
|
53 by (metis balance_def order_refl prod.collapse take_all) |
|
54 |
|
55 lemma bal_height: "bal xs n = (t,ys) \<Longrightarrow> height t = floorlog 2 n" |
|
56 proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
57 case (1 xs n) show ?case |
|
58 proof cases |
|
59 assume "n = 0" thus ?thesis |
|
60 using "1.prems" by (simp add: floorlog_def bal.simps) |
|
61 next |
|
62 assume [arith]: "n \<noteq> 0" |
|
63 from "1.prems" obtain l r xs' where |
|
64 b1: "bal xs (n div 2) = (l,xs')" and |
|
65 b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and |
|
66 t: "t = \<langle>l, hd xs', r\<rangle>" |
|
67 using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) |
|
68 let ?log1 = "floorlog 2 (n div 2)" |
|
69 let ?log2 = "floorlog 2 (n - 1 - n div 2)" |
|
70 have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp |
|
71 have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp |
|
72 have "n div 2 \<ge> n - 1 - n div 2" by arith |
|
73 hence le: "?log2 \<le> ?log1" by(simp add:floorlog_mono) |
|
74 have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
|
75 also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1) |
|
76 also have "\<dots> = floorlog 2 n" by (simp add: Float.compute_floorlog) |
|
77 finally show ?thesis . |
|
78 qed |
|
79 qed |
|
80 |
|
81 lemma bal_min_height: |
|
82 "bal xs n = (t,ys) \<Longrightarrow> min_height t = floorlog 2 (n + 1) - 1" |
|
83 proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
84 case (1 xs n) show ?case |
|
85 proof cases |
|
86 assume "n = 0" thus ?thesis |
|
87 using "1.prems" by (simp add: floorlog_def bal.simps) |
|
88 next |
|
89 assume [arith]: "n \<noteq> 0" |
|
90 from "1.prems" obtain l r xs' where |
|
91 b1: "bal xs (n div 2) = (l,xs')" and |
|
92 b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and |
|
93 t: "t = \<langle>l, hd xs', r\<rangle>" |
|
94 using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) |
|
95 let ?log1 = "floorlog 2 (n div 2 + 1) - 1" |
|
96 let ?log2 = "floorlog 2 (n - 1 - n div 2 + 1) - 1" |
|
97 let ?log2' = "floorlog 2 (n - n div 2) - 1" |
|
98 have "n - 1 - n div 2 + 1 = n - n div 2" by arith |
|
99 hence IH2: "min_height r = ?log2'" using "1.IH"(2) b1 b2 by simp |
|
100 have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp |
|
101 have *: "floorlog 2 (n - n div 2) \<ge> 1" by (simp add: floorlog_def) |
|
102 have "n div 2 + 1 \<ge> n - n div 2" by arith |
|
103 with * have le: "?log2' \<le> ?log1" by(simp add: floorlog_mono diff_le_mono) |
|
104 have "min_height t = min ?log1 ?log2' + 1" by (simp add: t IH1 IH2) |
|
105 also have "\<dots> = ?log2' + 1" using le by (simp add: min_absorb2) |
|
106 also have "\<dots> = floorlog 2 (n - n div 2)" by(simp add: floorlog_def) |
|
107 also have "n - n div 2 = (n+1) div 2" by arith |
|
108 also have "floorlog 2 \<dots> = floorlog 2 (n+1) - 1" |
|
109 by (simp add: Float.compute_floorlog) |
|
110 finally show ?thesis . |
|
111 qed |
|
112 qed |
|
113 |
|
114 lemma balanced_bal: |
|
115 assumes "bal xs n = (t,ys)" shows "height t - min_height t \<le> 1" |
|
116 proof - |
|
117 have "floorlog 2 n \<le> floorlog 2 (n+1)" by (rule floorlog_mono) auto |
|
118 thus ?thesis |
|
119 using bal_height[OF assms] bal_min_height[OF assms] by arith |
|
120 qed |
|
121 |
|
122 corollary balanced_balance: "height(balance xs) - min_height(balance xs) \<le> 1" |
|
123 by (metis balance_def balanced_bal prod.collapse) |
|
124 |
|
125 end |