5

Given a set of data in a binary search tree, like the numbers 1 to 10, is it possible for multiple balanced binary search trees to exist?

Or is there just one, unique balanced BST for that set of data?

Thanks

Óscar López
  • 232,561
  • 37
  • 312
  • 386

1 Answers1

6

It all depends on the specific binary tree data structure being used, the insertion algorithm, the balancing criteria and the order of insertion, but yes - it's possible to have multiple equivalent and valid balanced BSTs for a given sequence of values.

For example, this is a valid Red/Black Tree where the numbers 1-10 were inserted in ascending order:

Red/Black Tree

On the other hand, this is a valid AVL Tree, where the numbers 1-10 were inserted exactly in the same order as in the Red/Black Tree:

AVL Tree

Clearly, the trees are not exactly the same - but the ordering and balancing properties hold for both.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • So, lets say I was using the AVL tree, would there be multiple, AVL trees for the same set of numbers? If so, is the order of insertion the action that causes these different trees to exist? – user2305684 May 27 '13 at 02:41
  • 2
    @user2305684 if we restrict the tree to a particular implementation, yes we can still get different results depending on the order of the insertions. But we can be sure that if the elements are inserted in the same order for the same data structure and algorithm, the resulting tree will be the same – Óscar López May 27 '13 at 02:47