Are duplicate keys allowed in the definition of binary search trees?

后端 未结 12 882
慢半拍i
慢半拍i 2020-11-29 15:40

I\'m trying to find the definition of a binary search tree and I keep finding different definitions everywhere.

Some say that for any given subtree the left child k

12条回答
  •  萌比男神i
    2020-11-29 16:27

    All three definitions are acceptable and correct. They define different variations of a BST.

    Your college data structure's book failed to clarify that its definition was not the only possible.

    Certainly, allowing duplicates adds complexity. If you use the definition "left <= root < right" and you have a tree like:

          3
        /   \
      2       4
    

    then adding a "3" duplicate key to this tree will result in:

          3
        /   \
      2       4
        \
         3
    

    Note that the duplicates are not in contiguous levels.

    This is a big issue when allowing duplicates in a BST representation as the one above: duplicates may be separated by any number of levels, so checking for duplicate's existence is not that simple as just checking for immediate childs of a node.

    An option to avoid this issue is to not represent duplicates structurally (as separate nodes) but instead use a counter that counts the number of occurrences of the key. The previous example would then have a tree like:

          3(1)
        /     \
      2(1)     4(1)
    

    and after insertion of the duplicate "3" key it will become:

          3(2)
        /     \
      2(1)     4(1)
    

    This simplifies lookup, removal and insertion operations, at the expense of some extra bytes and counter operations.

提交回复
热议问题