1

I have some questions on binary trees:

  • Wikipedia states that a binary tree is complete when "A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible." What does the last "as far left as possible" passage mean?

  • A well-formed binary tree is said to be "height-balanced" if (1) it is empty, or (2) its left and right children are height-balanced and the height of the left tree is within 1 of the height of the right tree, taken from How to determine if binary tree is balanced?, is this correct or there's "jitter" on the 1-value? I read on the answer I linked that there could be also a difference factor of 4 between the height of the right and the left tree

  • Do the complete and height-balanced definitions just apply to binary tree or just any other tree?

Community
  • 1
  • 1
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108

2 Answers2

0
  • Following the reference of the definition in wikipedia, I got to this page. The definition was taken from there but modified:

    Definition: A binary tree in which every level, except possibly the deepest, is completely filled. At depth n, the height of the tree, all nodes must be as far left as possible.

    It continues with a note below though,

    A complete binary tree has 2k nodes at every depth k < n and between 2n and 2^(n+1) - 1 nodes altogether.

    Sometimes, definitions vary according to convenience (be useful for something). That passage might be a variation which, as I understand, requires leaf nodes to fill first the left side of the deepest level (that is, fill from left to right). The definition that I usually found is exactly as described above but without that passage.

  • Usually the definition taken for height-balanced tree is the one you described. In other words:

    A tree is balanced if and only if for every node the heights of its two subtrees differ by at most 1.

    That definition was taken from here. Again, sometimes definitions are made more flexible to serve specific purposes. For example, the definition of an AVL tree says that

    In an AVL tree, the heights of the two child subtrees of any node differ by at most one

    Still, I remember once I had to rewrite an algorithm so that the tree would be considered height-balanced if the two child subtrees of any node differed by at most 2. Note that the definition you gave is recursive, this is very common for binary trees.

  • In a tree whose number of children is variable, you wouldn't be able to say that it is complete (any parent could have the number of children that you want). Still, it can apply to n-ary trees (with a fixed amount of n children).
PALEN
  • 2,784
  • 2
  • 23
  • 24
0

Do the complete and height-balanced definitions just apply to binary tree or just any other tree?

Short answer: Yes, it can be extended to any n-ary tree.

Ankush
  • 2,454
  • 2
  • 21
  • 27