4

I have a few million consecutive numbers e.g. {3, 1, 2, ...}. In the same order as they are, numbers are inserted into a binary search tree. I need to found what is the height of the tree.

Is there any method to determine a height of the tree, without build a whole tree?

Lukas1212
  • 41
  • 1
  • 1

2 Answers2

0

If it is balanced as a part of the build, it is log2(n) where n is the count of numbers. Otherwise, that is a best case and worst case is n.

Necreaux
  • 9,451
  • 7
  • 26
  • 43
0

I am not aware of any algorithm. Quick thought is that it could go along the lines - first element is root, tree height is 1, then check next two elements and depending on what they are height h = 2 or 3, then check next 4 elements and depending on what they are h = h + (1 or 2 or 3 or 4) then check next 8 elements and h = h + (f(elements) - function which returns how much those elements increase tree height) etc... This is just very simplified example on what I would try to pursue. Not even sure if it would be faster than simply building tree and saving height information along the line, but at least it would be performed in place if you mananage to write that function. That is interesting problem BTW.

nesvarbu
  • 986
  • 2
  • 9
  • 24