Theory Balance

theory Balance
imports Tree_Real
```(* Author: Tobias Nipkow *)

section ‹Creating Balanced Trees›

theory Balance
imports
"HOL-Library.Tree_Real"
begin

fun bal :: "nat ⇒ 'a list ⇒ 'a tree * 'a list" where
"bal n xs = (if n=0 then (Leaf,xs) else
(let m = n div 2;
(l, ys) = bal m xs;
(r, zs) = bal (n-1-m) (tl ys)
in (Node l (hd ys) r, zs)))"

declare bal.simps[simp del]

definition bal_list :: "nat ⇒ 'a list ⇒ 'a tree" where
"bal_list n xs = fst (bal n xs)"

definition balance_list :: "'a list ⇒ 'a tree" where
"balance_list xs = bal_list (length xs) xs"

definition bal_tree :: "nat ⇒ 'a tree ⇒ 'a tree" where
"bal_tree n t = bal_list n (inorder t)"

definition balance_tree :: "'a tree ⇒ 'a tree" where
"balance_tree t = bal_tree (size t) t"

lemma bal_simps:
"bal 0 xs = (Leaf, xs)"
"n > 0 ⟹
bal n xs =
(let m = n div 2;
(l, ys) = bal m xs;
(r, zs) = bal (n-1-m) (tl ys)
in (Node l (hd ys) r, zs))"

text‹Some of the following lemmas take advantage of the fact
that ‹bal xs n› yields a result even if ‹n > length xs›.›

lemma size_bal: "bal n xs = (t,ys) ⟹ size t = n"
proof(induction n xs arbitrary: t ys rule: bal.induct)
case (1 n xs)
thus ?case
by(cases "n=0")
(auto simp add: bal_simps Let_def split: prod.splits)
qed

lemma bal_inorder:
"⟦ bal n xs = (t,ys); n ≤ length xs ⟧
⟹ inorder t = take n xs ∧ ys = drop n xs"
proof(induction n xs arbitrary: t ys rule: bal.induct)
case (1 n xs) 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 ?n1 xs = (l,xs')" and
b2: "bal ?n2 (tl xs') = (r,ys)" and
t: "t = ⟨l, hd xs', r⟩"
by(auto simp: Let_def bal_simps 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 inorder_bal_list[simp]:
"n ≤ length xs ⟹ inorder(bal_list n xs) = take n xs"
unfolding bal_list_def by (metis bal_inorder eq_fst_iff)

corollary inorder_balance_list[simp]: "inorder(balance_list xs) = xs"

corollary inorder_bal_tree:
"n ≤ size t ⟹ inorder(bal_tree n t) = take n (inorder t)"

corollary inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t"

corollary size_bal_list[simp]: "size(bal_list n xs) = n"
unfolding bal_list_def by (metis prod.collapse size_bal)

corollary size_balance_list[simp]: "size(balance_list xs) = length xs"

corollary size_bal_tree[simp]: "size(bal_tree n t) = n"

corollary size_balance_tree[simp]: "size(balance_tree t) = size t"

lemma min_height_bal:
"bal n xs = (t,ys) ⟹ min_height t = nat(⌊log 2 (n + 1)⌋)"
proof(induction n xs arbitrary: t ys rule: bal.induct)
case (1 n xs) show ?case
proof cases
assume "n = 0" thus ?thesis
using "1.prems" by (simp add: bal_simps)
next
assume [arith]: "n ≠ 0"
from "1.prems" obtain l r xs' where
b1: "bal (n div 2) xs = (l,xs')" and
b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and
t: "t = ⟨l, hd xs', r⟩"
by(auto simp: bal_simps Let_def split: prod.splits)
let ?log1 = "nat (floor(log 2 (n div 2 + 1)))"
let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))"
have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp
have IH2: "min_height r = ?log2" using "1.IH"(2) b1 b2 by simp
have "(n+1) div 2 ≥ 1" by arith
hence 0: "log 2 ((n+1) div 2) ≥ 0" by simp
have "n - 1 - n div 2 + 1 ≤ n div 2 + 1" by arith
hence le: "?log2 ≤ ?log1"
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 "n - 1 - n div 2 + 1 = (n+1) div 2" by linarith
also have "nat (floor(log 2 ((n+1) div 2))) + 1
= nat (floor(log 2 ((n+1) div 2) + 1))"
using 0 by linarith
also have "… = nat (floor(log 2 (n + 1)))"
using floor_log2_div2[of "n+1"] by (simp add: log_mult)
finally show ?thesis .
qed
qed

lemma height_bal:
"bal n xs = (t,ys) ⟹ height t = nat ⌈log 2 (n + 1)⌉"
proof(induction n xs arbitrary: t ys rule: bal.induct)
case (1 n xs) show ?case
proof cases
assume "n = 0" thus ?thesis
using "1.prems" by (simp add: bal_simps)
next
assume [arith]: "n ≠ 0"
from "1.prems" obtain l r xs' where
b1: "bal (n div 2) xs = (l,xs')" and
b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and
t: "t = ⟨l, hd xs', r⟩"
by(auto simp: bal_simps Let_def split: prod.splits)
let ?log1 = "nat ⌈log 2 (n div 2 + 1)⌉"
let ?log2 = "nat ⌈log 2 (n - 1 - n div 2 + 1)⌉"
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 0: "log 2 (n div 2 + 1) ≥ 0" by auto
have "n - 1 - n div 2 + 1 ≤ n div 2 + 1" by arith
hence le: "?log2 ≤ ?log1"
by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq)
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 "… = nat ⌈log 2 (n div 2 + 1) + 1⌉" using 0 by linarith
also have "… = nat ⌈log 2 (n + 1)⌉"
using ceiling_log2_div2[of "n+1"] by (simp)
finally show ?thesis .
qed
qed

lemma balanced_bal:
assumes "bal n xs = (t,ys)" shows "balanced t"
unfolding balanced_def
using height_bal[OF assms] min_height_bal[OF assms]
by linarith

lemma height_bal_list:
"n ≤ length xs ⟹ height (bal_list n xs) = nat ⌈log 2 (n + 1)⌉"
unfolding bal_list_def by (metis height_bal prod.collapse)

lemma height_balance_list:
"height (balance_list xs) = nat ⌈log 2 (length xs + 1)⌉"

corollary height_bal_tree:
"n ≤ length xs ⟹ height (bal_tree n t) = nat⌈log 2 (n + 1)⌉"
unfolding bal_list_def bal_tree_def
using height_bal prod.exhaust_sel by blast

corollary height_balance_tree:
"height (balance_tree t) = nat⌈log 2 (size t + 1)⌉"
by (simp add: bal_tree_def balance_tree_def height_bal_list)

corollary balanced_bal_list[simp]: "balanced (bal_list n xs)"
unfolding bal_list_def by (metis  balanced_bal prod.collapse)

corollary balanced_balance_list[simp]: "balanced (balance_list xs)"

corollary balanced_bal_tree[simp]: "balanced (bal_tree n t)"

corollary balanced_balance_tree[simp]: "balanced (balance_tree t)"

lemma wbalanced_bal: "bal n xs = (t,ys) ⟹ wbalanced t"
proof(induction n xs arbitrary: t ys rule: bal.induct)
case (1 n xs)
show ?case
proof cases
assume "n = 0"
thus ?thesis
next
assume "n ≠ 0"
with "1.prems" obtain l ys r zs where
rec1: "bal (n div 2) xs = (l, ys)" and
rec2: "bal (n - 1 - n div 2) (tl ys) = (r, zs)" and
t: "t = ⟨l, hd ys, r⟩"
by(auto simp add: bal_simps Let_def split: prod.splits)
have l: "wbalanced l" using "1.IH"(1)[OF ‹n≠0› refl rec1] .
have "wbalanced r" using "1.IH"(2)[OF ‹n≠0› refl rec1[symmetric] refl rec2] .
with l t size_bal[OF rec1] size_bal[OF rec2]
show ?thesis by auto
qed
qed

text‹An alternative proof via @{thm balanced_if_wbalanced}:›
lemma "bal n xs = (t,ys) ⟹ balanced t"
by(rule balanced_if_wbalanced[OF wbalanced_bal])

lemma wbalanced_bal_list[simp]: "wbalanced (bal_list n xs)"
by(simp add: bal_list_def) (metis prod.collapse wbalanced_bal)

lemma wbalanced_balance_list[simp]: "wbalanced (balance_list xs)"