binary-search-tree

Insert duplicate values in a binary search tree


Can i insert the same value in a binary search tree? And if I can, should this insert happen at the left or right of the existing node with that value?

this is a binary search tree

Can I insert 23 to this tree?


Solution

  • Yes, unless otherwise specified by the actual context, a binary search tree can have duplicates. There is no rule for choosing a side.

    Moreover, even if a side is agreed upon, binary search trees are typically self-balancing, and that means that this choice of side will not be maintained.

    For instance, imagine the extreme case where only the value 42 is inserted in a tree, like five times, and we would agree that duplicates are inserted in the right subtree, then we would get this tree:

           42
             \
              42
                \
                 42
                   \
                    42
                      \
                       42
    

    However, the BST would be rebalanced after a few inserts, and the resulting tree would be more like this (by that rebalancing):

           42
          /  \
        42    42
          \     \
           42    42
           
    

    And now it is clear you cannot assume at which side duplicate values could be stored.

    Another approach is to allow duplicates but not store them as separate nodes. Instead you could make the "payload" (if any) an array of data that was inserted with the same key. Or, if there is no payload to go with a key, have a counter at each node indicating how often that key was inserted.

    This might be preferable.