Complete binary tree definitions

∥☆過路亽.° 提交于 2019-12-02 04:40:11
  • 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).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!