Are empty Binary Search Trees valid?

前端 未结 7 1298
挽巷
挽巷 2021-01-12 12:48

I have two questions regarding binary search trees, both about empty trees.

  1. Is an empty tree (null) valid?
  2. Is a root node without children valid?
相关标签:
7条回答
  • 2021-01-12 13:14

    Is an empty tree (null) valid?

    There is no tree if it is empty. Hence the question of validity doesn't arise.

    Is a root node without children valid?

    Yes. That is a tee with only one element in it.

    0 讨论(0)
  • A binary tree can be defined recursively as:

    A single node with either no children, or with two children - 
    each of which is a binary tree.
    
    0 讨论(0)
  • 2021-01-12 13:25

    Yes both conditions are true. For a binary tree, each node can have zero, one, or two children. If a tree has only a root node and no other node then it is called a NULL tree.

    0 讨论(0)
  • 2021-01-12 13:26

    Exactly what an empty tree is of course depends on your implementation, as does the meaning of the word "valid", but in general I would say "yes" to both questions. The empty tree represents the case where the set of entities you are intersted in is empty, and the single node the case where the set contains one entity.

    0 讨论(0)
  • 2021-01-12 13:29

    Yes, and yes.

    0 讨论(0)
  • 2021-01-12 13:35

    A single node with no children is certainly valid and according to Binary Tree Structure:

    A (mutable) binary tree, BiTree, can be in an empty state or a non-empty state:

    • When it is empty, it contains no data.
    • When it is not empty, it contains a data object called the root element, and 2 distinct BiTree objects called the left subtree and the right subtree.

    For completeness, this Wikipedia entry is a pretty useful summary of terminology and the like.

    0 讨论(0)
提交回复
热议问题