BIRT 3.7
Written by: Michael Williams
Featured Refcardz: Top Refcardz:
  1. HTML5 Canvas
  2. Ruby
  3. iPhone/iPad
  4. Spring Web Flow
  5. REST
  1. jQuery Selectors
  2. Spring Config.
  3. Java
  4. Ajax
  5. Java Concurrency

Link Details

Link 310059 thumbnail
User 410289 avatar

By CodeJustin
via scienceblogs.com
Published: Dec 03 2009 / 11:55

So, we've built up some pretty nifty binary trees - we can use the binary tree both as the basis of an implementation of a set, or as an implementation of a dictionary. But our implementation has had one major problem: it's got absolutely no way to maintain balance. What that means is that depending on the order in which things are inserted to the tree, we might have excellent performance, or we might be no better than a linear list. For example, look at these trees. As you can see, a tree with the same values can wind up quite different. In a good insert order, you can wind up with a nicely balanced tree: the minimum distance from root to leaf is 3; the maximum is 4. On the other hand, take the same values, and insert them in a different order and you get a rotten tree; the minimum distance from root to leaf is 1, and the maximum is 7. So depending on luck, you can get a tree that gives you good performance, or one that ends up giving you no better than a plain old list. Playing with a bit of randomization can often give you reasonably good performance on average - but if you're using a tree, it's probably because O(n) complexity is just too high. You want the O(lg n) complexity that you'll get from a binary tree - and not just sometimes.
  • 6
  • 0
  • 1097
  • 0

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (6)



Voters Against This Link (0)