Search code examples
sortingtreebinary-treebinary-search-treepreorder

Adding elements to a binary Search tree without an order


I am learning about Binary Search Trees and had a question asking me to add things to a tree and draw what it would look like.

All the ones before this question specified something like "Assume the tree uses Alphabetical ordering to compare words" but this time it doesn't say that.

Is there a default sorting order to sort strings or int's when adding them to a tree?

For context, it asks me to:
Draw a picture below of the binary search tree that would result from inserting the following words into an empty binary search tree in the following order: Legolas, Frodo, Sam, Merry, Pippin, Aragorn, Gimli, Boromir.


Solution

  • Since the question specifically says "Binary search trees", you can compare node using the Lexicographical order (Alphabetical Order) while inserting nodes in the tree.

    for your example the tree would look like :

                                         Legolas
    
    
                Frodo                                                 Sam
    
    
      Aaragon              Gimili                             Merry
    
    
            Boromir                                               Pippin