Search code examples
heightbinary-search-treeavl-treeinduction

Show that for any AVL tree with height h, all levels until h/2 are complete trees by induction


I was given this question on a test: "show by induction, that for a given AVL tree of height h, all levels of the tree until h/2 (round down) are complete binary trees". I wrote down the following answer and would like to know if my argument stands.

base: h= 0-> then level 0/2 is complete binary tree since theres just a root. h=1-> same argument since 1/2 round down is zero

step: assume correctness for all avl trees with level h and show for h+1. assume we have a AVL tree of height h+1. remove the root and receive two AVL trees of height h or one h and one h-1. divide into two cases: h is even, h is odd. h is odd: h/2=(h-1)/2 (round down) so we get that the same levels on the two sub trees are complete by induction, add back the root and we get that all level h+1/2 are complete. h is even: if the trees are of different height then h/2= (h-1)/2 + 1 (round down). so since the taller tree is a complete binary tree until h/2 it stands that he is complete binary tree until level h-1/2. add back the root and we get h+1/2 is complete binary tree.

id like to know how close this is to a correct answer.


Solution

  • The proof you have presented is indeed correct if you add parentheses where they are missing, and a few other improvements. See what I updated in bold:

    base: h= 0-> then level 0/2 is complete binary tree since there's just a root. h=1-> same argument since 1/2 round down is zero.

    step: assume correctness for all avl trees with level h and show for h+1. assume we have a AVL tree of height h+1. remove the root and receive two AVL trees of height h or one h and one h-1. divide into two cases: h is even, h is odd. h is odd: h/2=(h-1)/2 (round down) so we get that the same levels on the two sub trees are complete by induction, add back the root and we get that all level (h+1)/2 are complete. h is even: if the trees are of different height then h/2= (h-1)/2 + 1 (round down). so since the taller tree is a complete binary tree until h/2 it stands that he is complete binary tree until level (h-1)/2, which is h/2 - 1 as h is even. add back the root and we get h/2 is complete binary tree.