Binary Search Tree Insertion – Handling Equal Elements

data structurestrees

Most BST examples show a sample of a BST with unique values; mainly
to demonstrate the order of values. e.g. values in the left subtree are smaller than the root, and values in the right subtree are larger.

Is this because BSTs are normally just used to represents SETs ?

If I insert an element say 4 which already exists in the BST, what should happen ?
e.g. In my case, 4 is associated with a payload. Does it mean I override the existing node's payload.

Best Answer

The classic examples of the BST demonstrate a set where there is one entry for a given value in the structure. An example of this is the TreeSet in Java (yes, thats a red-black tree behind the scenes - but its a tree and a set).

However, there's nothing saying that there can't be additional values stored at the location indicated by the value. Once you decide to do this, it becomes an associative array (sometimes called a map). Again, going to Java for the example, there is the TreeMap.

An example of this could be:

TreeMap<Integer, String> ageToName = new TreeMap<Integer, String>();
ageToName.put(4,"Alice");
ageToName.put(25,"Bob");
ageToName.put(16,"Charlie");

The structure of this would look like:

      16 -> Charlie          
     /             \         
    /               \        
4 -> Alice          25 -> Bob

A balanced binary tree (the red-black part of that structure) with a left child and a right child. You access it by looking for the value 4, or 16, or 25 and get back the associated data stored in that node.

One aspect of the tree is that you can't have two different values with the same index. There is no way with this design to insert David at age 16 also. However, one could put another data structure such as a list instead of the String at the node and allow you to store multiple items in that list.

But the (binary) tree, by itself, is a set that requires the indexes into it to be comparable (orderable) and that it will contain only distinct values (no duplicates).

Realize that everyone is free to implement their own trees and sets and how they deal with the addition of another item with the same key. With a TreeSet, if you add an already existing value, the add function returns false and leaves the set unchanged. With a TreeMap, if you call put with a key that already exists, it replaces the old value and returns the replaced value (null if nothing was there).

What should happen is whatever you need to have happen for that implementation. There's no inscribed tablet that all the computer scientists signed that dictates how any abstract data structure should behave. They behave as they should and when they need to behave other ways, document it and do it that way.

Related Topic