# HG changeset patch # User nipkow # Date 1470770312 -7200 # Node ID f9ad2e59195749ab828516799b6d6160ffe49402 # Parent d83a1eeff9d2b4293747e35a22c8adc88c8d5e5a New theory Balance_List diff -r d83a1eeff9d2 -r f9ad2e591957 src/HOL/Data_Structures/Balance_List.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Data_Structures/Balance_List.thy Tue Aug 09 21:18:32 2016 +0200 @@ -0,0 +1,125 @@ +(* Tobias Nipkow *) + +section \Creating a Balanced Tree from a List\ + +theory Balance_List +imports + "~~/src/HOL/Library/Tree" + "~~/src/HOL/Library/Float" +begin + +fun bal :: "'a list \ nat \ 'a tree * 'a list" where +"bal xs n = (if n=0 then (Leaf,xs) else + (let m = n div 2; + (l, ys) = bal xs m; + (r, zs) = bal (tl ys) (n-1-m) + in (Node l (hd ys) r, zs)))" + +declare bal.simps[simp del] + +definition "balance xs = fst (bal xs (length xs))" + +lemma bal_inorder: + "bal xs n = (t,ys) \ n \ length xs \ inorder t = take n xs \ ys = drop n xs" +proof(induction xs n arbitrary: t ys rule: bal.induct) + case (1 xs n) show ?case + proof cases + assume "n = 0" thus ?thesis using 1 by (simp add: bal.simps) + next + assume [arith]: "n \ 0" + let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1" + from "1.prems" obtain l r xs' where + b1: "bal xs ?n1 = (l,xs')" and + b2: "bal (tl xs') ?n2 = (r,ys)" and + t: "t = \l, hd xs', r\" + using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) + have IH1: "inorder l = take ?n1 xs \ xs' = drop ?n1 xs" + using b1 "1.prems" by(intro "1.IH"(1)) auto + have IH2: "inorder r = take ?n2 (tl xs') \ ys = drop ?n2 (tl xs')" + using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto + have "drop (n div 2) xs \ []" using "1.prems"(2) by simp + hence "hd (drop ?n1 xs) # take ?n2 (tl (drop ?n1 xs)) = take (?n2 + 1) (drop ?n1 xs)" + by (metis Suc_eq_plus1 take_Suc) + hence *: "inorder t = take n xs" using t IH1 IH2 + using take_add[of ?n1 "?n2+1" xs] by(simp) + have "n - n div 2 + n div 2 = n" by simp + hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric]) + thus ?thesis using * by blast + qed +qed + +corollary balance_inorder: "inorder(balance xs) = xs" +using bal_inorder[of xs "length xs"] +by (metis balance_def order_refl prod.collapse take_all) + +lemma bal_height: "bal xs n = (t,ys) \ height t = floorlog 2 n" +proof(induction xs n arbitrary: t ys rule: bal.induct) + case (1 xs n) show ?case + proof cases + assume "n = 0" thus ?thesis + using "1.prems" by (simp add: floorlog_def bal.simps) + next + assume [arith]: "n \ 0" + from "1.prems" obtain l r xs' where + b1: "bal xs (n div 2) = (l,xs')" and + b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and + t: "t = \l, hd xs', r\" + using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) + let ?log1 = "floorlog 2 (n div 2)" + let ?log2 = "floorlog 2 (n - 1 - n div 2)" + have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp + have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp + have "n div 2 \ n - 1 - n div 2" by arith + hence le: "?log2 \ ?log1" by(simp add:floorlog_mono) + have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2) + also have "\ = ?log1 + 1" using le by (simp add: max_absorb1) + also have "\ = floorlog 2 n" by (simp add: Float.compute_floorlog) + finally show ?thesis . + qed +qed + +lemma bal_min_height: + "bal xs n = (t,ys) \ min_height t = floorlog 2 (n + 1) - 1" +proof(induction xs n arbitrary: t ys rule: bal.induct) + case (1 xs n) show ?case + proof cases + assume "n = 0" thus ?thesis + using "1.prems" by (simp add: floorlog_def bal.simps) + next + assume [arith]: "n \ 0" + from "1.prems" obtain l r xs' where + b1: "bal xs (n div 2) = (l,xs')" and + b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and + t: "t = \l, hd xs', r\" + using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits) + let ?log1 = "floorlog 2 (n div 2 + 1) - 1" + let ?log2 = "floorlog 2 (n - 1 - n div 2 + 1) - 1" + let ?log2' = "floorlog 2 (n - n div 2) - 1" + have "n - 1 - n div 2 + 1 = n - n div 2" by arith + hence IH2: "min_height r = ?log2'" using "1.IH"(2) b1 b2 by simp + have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp + have *: "floorlog 2 (n - n div 2) \ 1" by (simp add: floorlog_def) + have "n div 2 + 1 \ n - n div 2" by arith + with * have le: "?log2' \ ?log1" by(simp add: floorlog_mono diff_le_mono) + have "min_height t = min ?log1 ?log2' + 1" by (simp add: t IH1 IH2) + also have "\ = ?log2' + 1" using le by (simp add: min_absorb2) + also have "\ = floorlog 2 (n - n div 2)" by(simp add: floorlog_def) + also have "n - n div 2 = (n+1) div 2" by arith + also have "floorlog 2 \ = floorlog 2 (n+1) - 1" + by (simp add: Float.compute_floorlog) + finally show ?thesis . + qed +qed + +lemma balanced_bal: + assumes "bal xs n = (t,ys)" shows "height t - min_height t \ 1" +proof - + have "floorlog 2 n \ floorlog 2 (n+1)" by (rule floorlog_mono) auto + thus ?thesis + using bal_height[OF assms] bal_min_height[OF assms] by arith +qed + +corollary balanced_balance: "height(balance xs) - min_height(balance xs) \ 1" +by (metis balance_def balanced_bal prod.collapse) + +end diff -r d83a1eeff9d2 -r f9ad2e591957 src/HOL/ROOT --- a/src/HOL/ROOT Tue Aug 09 20:35:21 2016 +0200 +++ b/src/HOL/ROOT Tue Aug 09 21:18:32 2016 +0200 @@ -176,7 +176,9 @@ theories [document = false] "Less_False" "~~/src/HOL/Library/Multiset" + "~~/src/HOL/Library/Float" theories + Balance_List Tree_Map AVL_Map RBT_Map