I am having a difficult time understanding how binary search trees are built. Do they require the initial data to be sorted in order to find the top most root?
1 Answers
The shape that a binary search tree takes depends on two things:
- The order in which the elements are inserted, and
- What balance operations, if any, the tree does during insertion.
If you have a pure binary search tree with no rebalancing logic whatsoever, then the order in which the elements are inserted will have a strong impact on the shape of the tree. For example, take the values 1, 2, 3, 4, 5, 6, 7. If you insert them in the order 4, 2, 6, 1, 3, 5, 7, you will get this tree:
4
/ \
2 6
/ \ / \
1 3 5 7
The reason for this is that we go through the following series of trees:
4
4
/
2
4
/ \
2 6
4
/ \
2 6
/
1
4
/ \
2 6
/ \
1 3
4
/ \
2 6
/ \ /
1 3 5
4
/ \
2 6
/ \ / \
1 3 5 7
On the other hand, if you insert the values in the order 1, 2, 3, 4, 5, 6, 7, you get this tree:
1
\
2
\
3
\
4
\
5
\
6
\
7
Interestingly, inserting elements into a BST in sorted order is one of the worst things you can do for the tree, since it makes the tree a linear structure. You are better off picking a random permutation of the elements to insert, or (if they're all known in advance) sorting them and using the following recursive algorithm on the sorted sequence:
- If there are no elements, you are done.
- Otherwise:
- Insert the median into the BST.
- Recursively insert the first half of the elements into the BST.
- Recursively insert the second half of the elements into the BST.
Hope this helps!

- 362,284
- 104
- 897
- 1,065
-
1"Insert the median into the BST." So, in order to have the median value I would have to have my linear data in order first though, so like 5,3,6,2,4,1 would have to be reordered into 1,2,3,4,5,6 and then 4 would be the root? – George L Jun 19 '13 at 20:40
-
@GeorgeL- I'm not sure I understand your comment. Can you clarify? – templatetypedef Jun 19 '13 at 20:41
-
Sorry, didn't realize hitting enter submits the comment. – George L Jun 19 '13 at 20:42
-
1@GeorgeL- If you wanted an optimal BST, you will need to sort the data in advance, or otherwise use an algorithm that will do at least as much work as sorting the data. – templatetypedef Jun 19 '13 at 20:44
-
@GeorgeL- That said, the root of a BST (unless the BST uses a balance algorithm) is always the first element inserted. – templatetypedef Jun 19 '13 at 20:46