453 qed simp_all |
453 qed simp_all |
454 |
454 |
455 |
455 |
456 subsection \<open>Height-Size Relation\<close> |
456 subsection \<open>Height-Size Relation\<close> |
457 |
457 |
458 text \<open>By Daniel St\"uwe\<close> |
458 text \<open>By Daniel St\"uwe, Manuel Eberl and Peter Lammich.\<close> |
459 |
459 |
460 fun fib_tree :: "nat \<Rightarrow> unit avl_tree" where |
460 lemma height_invers: |
461 "fib_tree 0 = Leaf" | |
|
462 "fib_tree (Suc 0) = Node 1 Leaf () Leaf" | |
|
463 "fib_tree (Suc(Suc n)) = Node (Suc(Suc(n))) (fib_tree (Suc n)) () (fib_tree n)" |
|
464 |
|
465 lemma [simp]: "ht (fib_tree h) = h" |
|
466 by (induction h rule: "fib_tree.induct") auto |
|
467 |
|
468 lemma [simp]: "height (fib_tree h) = h" |
|
469 by (induction h rule: "fib_tree.induct") auto |
|
470 |
|
471 lemma "avl(fib_tree h)" |
|
472 by (induction h rule: "fib_tree.induct") auto |
|
473 |
|
474 lemma fib_tree_size1: "size1 (fib_tree h) = fib (h+2)" |
|
475 by (induction h rule: fib_tree.induct) auto |
|
476 |
|
477 lemma height_invers[simp]: |
|
478 "(height t = 0) = (t = Leaf)" |
461 "(height t = 0) = (t = Leaf)" |
479 "avl t \<Longrightarrow> (height t = Suc h) = (\<exists> l a r . t = Node (Suc h) l a r)" |
462 "avl t \<Longrightarrow> (height t = Suc h) = (\<exists> l a r . t = Node (Suc h) l a r)" |
480 by (induction t) auto |
463 by (induction t) auto |
481 |
464 |
482 lemma fib_Suc_lt: "fib n \<le> fib (Suc n)" |
465 text \<open>Any AVL tree of height \<open>h\<close> has at least \<open>fib (h+2)\<close> leaves:\<close> |
483 by (induction n rule: fib.induct) auto |
466 |
484 |
467 lemma avl_fib_bound: "avl t \<Longrightarrow> height t = h \<Longrightarrow> fib (h+2) \<le> size1 t" |
485 lemma fib_mono: "n \<le> m \<Longrightarrow> fib n \<le> fib m" |
468 proof (induction h arbitrary: t rule: fib.induct) |
486 proof (induction n arbitrary: m rule: fib.induct ) |
469 case 1 thus ?case by (simp add: height_invers) |
487 case (2 m) |
470 next |
488 thus ?case using fib_neq_0_nat[of m] by auto |
471 case 2 thus ?case by (cases t) (auto simp: height_invers) |
489 next |
472 next |
490 case (3 n m) |
473 case (3 h) |
491 from 3 obtain m' where "m = Suc (Suc m')" |
474 from "3.prems" obtain l a r where |
492 by (metis le_Suc_ex plus_nat.simps(2)) |
475 [simp]: "t = Node (Suc(Suc h)) l a r" "avl l" "avl r" |
493 thus ?case using 3(1)[of "Suc m'"] 3(2)[of m'] 3(3) by auto |
476 and C: " |
494 qed simp |
477 height r = Suc h \<and> height l = Suc h |
495 |
478 \<or> height r = Suc h \<and> height l = h |
496 lemma size1_fib_tree_mono: |
479 \<or> height r = h \<and> height l = Suc h" (is "?C1 \<or> ?C2 \<or> ?C3") |
497 assumes "n \<le> m" |
480 by (cases t) (simp, fastforce) |
498 shows "size1 (fib_tree n) \<le> size1 (fib_tree m)" |
481 { |
499 using fib_tree_size1 fib_mono[OF assms] fib_mono[of "Suc n"] add_le_mono assms by fastforce |
482 assume ?C1 |
500 |
483 with "3.IH"(1) |
501 lemma fib_tree_minimal: "avl t \<Longrightarrow> size1 (fib_tree (ht t)) \<le> size1 t" |
484 have "fib (h + 3) \<le> size1 l" "fib (h + 3) \<le> size1 r" |
502 proof (induction "ht t" arbitrary: t rule: fib_tree.induct) |
485 by (simp_all add: eval_nat_numeral) |
503 case (2 t) |
486 hence ?case by (auto simp: eval_nat_numeral) |
504 from 2 obtain l a r where "t = Node (Suc 0) l a r" by (cases t) auto |
487 } moreover { |
505 with 2 show ?case by auto |
488 assume ?C2 |
506 next |
489 hence ?case using "3.IH"(1)[of r] "3.IH"(2)[of l] by auto |
507 case (3 h t) |
490 } moreover { |
508 note [simp] = 3(3)[symmetric] |
491 assume ?C3 |
509 from 3 obtain l a r where [simp]: "t = Node (Suc (Suc h)) l a r" by (cases t) auto |
492 hence ?case using "3.IH"(1)[of l] "3.IH"(2)[of r] by auto |
510 show ?case proof (cases rule: linorder_cases[of "ht l" "ht r"]) |
493 } ultimately show ?case using C by blast |
511 case equal |
494 qed |
512 with 3(3,4) have ht: "ht l = Suc h" "ht r = Suc h" by auto |
495 |
513 with 3 have "size1 (fib_tree (ht l)) \<le> size1 l" by auto moreover |
496 lemma fib_alt_induct [consumes 1, case_names 1 2 rec]: |
514 from 3(1)[of r] 3(3,4) ht(2) have "size1 (fib_tree (ht r)) \<le> size1 r" by auto ultimately |
497 assumes "n > 0" "P 1" "P 2" "\<And>n. n > 0 \<Longrightarrow> P n \<Longrightarrow> P (Suc n) \<Longrightarrow> P (Suc (Suc n))" |
515 show ?thesis using ht size1_fib_tree_mono[of h "Suc h"] by auto |
498 shows "P n" |
516 next |
499 using assms(1) |
517 case greater |
500 proof (induction n rule: fib.induct) |
518 with 3(3,4) have ht: "ht l = Suc h" "ht r = h" by auto |
501 case (3 n) |
519 from ht 3(1,2,4) have "size1 (fib_tree (Suc h)) \<le> size1 l" by auto moreover |
502 thus ?case using assms by (cases n) (auto simp: eval_nat_numeral) |
520 from ht 3(1,2,4) have "size1 (fib_tree h) \<le> size1 r" by auto ultimately |
503 qed (insert assms, auto) |
521 show ?thesis by auto |
504 |
522 next |
505 text \<open>An exponential lower bound for @{const fib}:\<close> |
523 case less (* analogously *) |
506 |
524 with 3 have ht: "ht l = h" "Suc h = ht r" by auto |
507 lemma fib_lowerbound: |
525 from ht 3 have "size1 (fib_tree h) \<le> size1 l" by auto moreover |
508 defines "\<phi> \<equiv> (1 + sqrt 5) / 2" |
526 from ht 3 have "size1 (fib_tree (Suc h)) \<le> size1 r" by auto ultimately |
509 defines "c \<equiv> 1 / \<phi> ^ 2" |
527 show ?thesis by auto |
510 assumes "n > 0" |
528 qed |
511 shows "real (fib n) \<ge> c * \<phi> ^ n" |
529 qed auto |
512 proof - |
530 |
513 have "\<phi> > 1" by (simp add: \<phi>_def) |
531 theorem avl_size_bound: "avl t \<Longrightarrow> fib(height t + 2) \<le> size1 t" |
514 hence "c > 0" by (simp add: c_def) |
532 using fib_tree_minimal fib_tree_size1 by fastforce |
515 from \<open>n > 0\<close> show ?thesis |
|
516 proof (induction n rule: fib_alt_induct) |
|
517 case (rec n) |
|
518 have "c * \<phi> ^ Suc (Suc n) = \<phi> ^ 2 * (c * \<phi> ^ n)" |
|
519 by (simp add: field_simps power2_eq_square) |
|
520 also have "\<dots> \<le> (\<phi> + 1) * (c * \<phi> ^ n)" |
|
521 by (rule mult_right_mono) (insert \<open>c > 0\<close>, simp_all add: \<phi>_def power2_eq_square field_simps) |
|
522 also have "\<dots> = c * \<phi> ^ Suc n + c * \<phi> ^ n" |
|
523 by (simp add: field_simps) |
|
524 also have "\<dots> \<le> real (fib (Suc n)) + real (fib n)" |
|
525 by (intro add_mono rec.IH) |
|
526 finally show ?case by simp |
|
527 qed (insert \<open>\<phi> > 1\<close>, simp_all add: c_def power2_eq_square eval_nat_numeral) |
|
528 qed |
|
529 |
|
530 text \<open>The size of an AVL tree is (at least) exponential in its height:\<close> |
|
531 |
|
532 lemma avl_lowerbound: |
|
533 defines "\<phi> \<equiv> (1 + sqrt 5) / 2" |
|
534 assumes "avl t" |
|
535 shows "real (size1 t) \<ge> \<phi> ^ (height t)" |
|
536 proof - |
|
537 have "\<phi> > 0" by(simp add: \<phi>_def add_pos_nonneg) |
|
538 hence "\<phi> ^ height t = (1 / \<phi> ^ 2) * \<phi> ^ (height t + 2)" |
|
539 by(simp add: field_simps power2_eq_square) |
|
540 also have "\<dots> \<le> real (fib (height t + 2))" |
|
541 using fib_lowerbound[of "height t + 2"] by(simp add: \<phi>_def) |
|
542 also have "\<dots> \<le> real (size1 t)" |
|
543 using avl_fib_bound[of t "height t"] assms by simp |
|
544 finally show ?thesis . |
|
545 qed |
533 |
546 |
534 end |
547 end |