author | nipkow |
Fri, 09 Sep 2016 14:15:16 +0200 | |
changeset 63829 | 6a05c8cbf7de |
parent 63755 | src/HOL/Data_Structures/Balance_List.thy@182c111190e5 |
child 63843 | ade7c3a20917 |
permissions | -rw-r--r-- |
63829 | 1 |
(* Author: Tobias Nipkow *) |
63643 | 2 |
|
63829 | 3 |
section \<open>Creating Balanced Trees\<close> |
63643 | 4 |
|
63829 | 5 |
theory Balance |
63643 | 6 |
imports |
7 |
"~~/src/HOL/Library/Tree" |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
63643
diff
changeset
|
8 |
"~~/src/HOL/Library/Log_Nat" |
63643 | 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 |
||
63829 | 20 |
definition balance_list :: "'a list \<Rightarrow> 'a tree" where |
21 |
"balance_list xs = fst (bal xs (length xs))" |
|
22 |
||
23 |
definition balance_tree :: "'a tree \<Rightarrow> 'a tree" where |
|
24 |
"balance_tree = balance_list o inorder" |
|
25 |
||
63643 | 26 |
|
27 |
lemma bal_inorder: |
|
63755
182c111190e5
Renamed balanced to complete; added balanced; more about both
nipkow
parents:
63663
diff
changeset
|
28 |
"\<lbrakk> bal xs n = (t,ys); n \<le> length xs \<rbrakk> |
182c111190e5
Renamed balanced to complete; added balanced; more about both
nipkow
parents:
63663
diff
changeset
|
29 |
\<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs" |
63643 | 30 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
31 |
case (1 xs n) show ?case |
|
32 |
proof cases |
|
33 |
assume "n = 0" thus ?thesis using 1 by (simp add: bal.simps) |
|
34 |
next |
|
35 |
assume [arith]: "n \<noteq> 0" |
|
36 |
let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1" |
|
37 |
from "1.prems" obtain l r xs' where |
|
38 |
b1: "bal xs ?n1 = (l,xs')" and |
|
39 |
b2: "bal (tl xs') ?n2 = (r,ys)" and |
|
40 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
|
41 |
using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) |
|
42 |
have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs" |
|
43 |
using b1 "1.prems" by(intro "1.IH"(1)) auto |
|
44 |
have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')" |
|
45 |
using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto |
|
46 |
have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp |
|
47 |
hence "hd (drop ?n1 xs) # take ?n2 (tl (drop ?n1 xs)) = take (?n2 + 1) (drop ?n1 xs)" |
|
48 |
by (metis Suc_eq_plus1 take_Suc) |
|
49 |
hence *: "inorder t = take n xs" using t IH1 IH2 |
|
50 |
using take_add[of ?n1 "?n2+1" xs] by(simp) |
|
51 |
have "n - n div 2 + n div 2 = n" by simp |
|
52 |
hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric]) |
|
53 |
thus ?thesis using * by blast |
|
54 |
qed |
|
55 |
qed |
|
56 |
||
63829 | 57 |
corollary inorder_balance_list: "inorder(balance_list xs) = xs" |
63643 | 58 |
using bal_inorder[of xs "length xs"] |
63829 | 59 |
by (metis balance_list_def order_refl prod.collapse take_all) |
63643 | 60 |
|
61 |
lemma bal_height: "bal xs n = (t,ys) \<Longrightarrow> height t = floorlog 2 n" |
|
62 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
63 |
case (1 xs n) show ?case |
|
64 |
proof cases |
|
65 |
assume "n = 0" thus ?thesis |
|
66 |
using "1.prems" by (simp add: floorlog_def bal.simps) |
|
67 |
next |
|
68 |
assume [arith]: "n \<noteq> 0" |
|
69 |
from "1.prems" obtain l r xs' where |
|
70 |
b1: "bal xs (n div 2) = (l,xs')" and |
|
71 |
b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and |
|
72 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
|
73 |
using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) |
|
74 |
let ?log1 = "floorlog 2 (n div 2)" |
|
75 |
let ?log2 = "floorlog 2 (n - 1 - n div 2)" |
|
76 |
have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp |
|
77 |
have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp |
|
78 |
have "n div 2 \<ge> n - 1 - n div 2" by arith |
|
79 |
hence le: "?log2 \<le> ?log1" by(simp add:floorlog_mono) |
|
80 |
have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
|
81 |
also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1) |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
63643
diff
changeset
|
82 |
also have "\<dots> = floorlog 2 n" by (simp add: compute_floorlog) |
63643 | 83 |
finally show ?thesis . |
84 |
qed |
|
85 |
qed |
|
86 |
||
87 |
lemma bal_min_height: |
|
88 |
"bal xs n = (t,ys) \<Longrightarrow> min_height t = floorlog 2 (n + 1) - 1" |
|
89 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
90 |
case (1 xs n) show ?case |
|
91 |
proof cases |
|
92 |
assume "n = 0" thus ?thesis |
|
93 |
using "1.prems" by (simp add: floorlog_def bal.simps) |
|
94 |
next |
|
95 |
assume [arith]: "n \<noteq> 0" |
|
96 |
from "1.prems" obtain l r xs' where |
|
97 |
b1: "bal xs (n div 2) = (l,xs')" and |
|
98 |
b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and |
|
99 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
|
100 |
using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) |
|
101 |
let ?log1 = "floorlog 2 (n div 2 + 1) - 1" |
|
102 |
let ?log2 = "floorlog 2 (n - 1 - n div 2 + 1) - 1" |
|
103 |
let ?log2' = "floorlog 2 (n - n div 2) - 1" |
|
104 |
have "n - 1 - n div 2 + 1 = n - n div 2" by arith |
|
105 |
hence IH2: "min_height r = ?log2'" using "1.IH"(2) b1 b2 by simp |
|
106 |
have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp |
|
107 |
have *: "floorlog 2 (n - n div 2) \<ge> 1" by (simp add: floorlog_def) |
|
108 |
have "n div 2 + 1 \<ge> n - n div 2" by arith |
|
109 |
with * have le: "?log2' \<le> ?log1" by(simp add: floorlog_mono diff_le_mono) |
|
110 |
have "min_height t = min ?log1 ?log2' + 1" by (simp add: t IH1 IH2) |
|
111 |
also have "\<dots> = ?log2' + 1" using le by (simp add: min_absorb2) |
|
112 |
also have "\<dots> = floorlog 2 (n - n div 2)" by(simp add: floorlog_def) |
|
113 |
also have "n - n div 2 = (n+1) div 2" by arith |
|
114 |
also have "floorlog 2 \<dots> = floorlog 2 (n+1) - 1" |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
63643
diff
changeset
|
115 |
by (simp add: compute_floorlog) |
63643 | 116 |
finally show ?thesis . |
117 |
qed |
|
118 |
qed |
|
119 |
||
120 |
lemma balanced_bal: |
|
63755
182c111190e5
Renamed balanced to complete; added balanced; more about both
nipkow
parents:
63663
diff
changeset
|
121 |
assumes "bal xs n = (t,ys)" shows "balanced t" |
63643 | 122 |
proof - |
123 |
have "floorlog 2 n \<le> floorlog 2 (n+1)" by (rule floorlog_mono) auto |
|
63829 | 124 |
thus ?thesis unfolding balanced_def |
125 |
using bal_height[OF assms] bal_min_height[OF assms] by linarith |
|
63643 | 126 |
qed |
127 |
||
63829 | 128 |
corollary size_balance_list[simp]: "size(balance_list xs) = length xs" |
129 |
by (metis inorder_balance_list length_inorder) |
|
130 |
||
131 |
corollary balanced_balance_list[simp]: "balanced (balance_list xs)" |
|
132 |
by (metis balance_list_def balanced_bal prod.collapse) |
|
133 |
||
134 |
lemma height_balance_list: "height(balance_list xs) = floorlog 2 (length xs)" |
|
135 |
by (metis bal_height balance_list_def prod.collapse) |
|
136 |
||
137 |
lemma inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t" |
|
138 |
by(simp add: balance_tree_def inorder_balance_list) |
|
139 |
||
140 |
lemma size_balance_tree[simp]: "size(balance_tree t) = size t" |
|
141 |
by(simp add: balance_tree_def inorder_balance_list) |
|
142 |
||
143 |
corollary balanced_balance_tree[simp]: "balanced (balance_tree t)" |
|
144 |
by (simp add: balance_tree_def) |
|
145 |
||
146 |
lemma height_balance_tree: "height(balance_tree t) = floorlog 2 (size t)" |
|
147 |
by(simp add: balance_tree_def height_balance_list) |
|
148 |
||
149 |
hide_const (open) bal |
|
63643 | 150 |
|
151 |
end |