0

I have a BST and 2 queues that feed into it. In order for insertion and deletion to be in the shortest time possible I need to balance my tree every so often. For this I use the DSW algorithm.

I have all this implemented and it all works great. My problem is I don't know when the best time to balance the tree is.

I've tried looking for papers on this and any kind of information but I can't find any.

I basically need to know when it's optimal to balance the tree such that it isn't too often that it takes too much time but that it's often enough that my insertion & deletion times don't take very long. So in the end the total run time is as short as possible.

Any ideas?

Kara
  • 6,115
  • 16
  • 50
  • 57
Ree
  • 863
  • 2
  • 18
  • 38
  • 1
    This is just an off-of-the-top-of-my-head suggestion: what about comparing the number of elements in the tree to the maximum depth? You know that for `n` elements, the optimal depth should be `log2(n)+1`. If the actual max depth gets too far away from the optimal depth, then you know that the tree is improperly balanced. – bstempi Oct 17 '13 at 13:15
  • I like this idea but how far is too far away? – Ree Oct 17 '13 at 13:53
  • I suppose that depends on how efficient you want your tree to be. If you made "too far away" 2 or 3, you'd have a very efficient tree, but you'd be rebalancing more often. If you define it as something larger, you spend less time balancing your tree at the expense of the tree access time. I suppose if your tree is read-heavy, keep the number low. If it's write-heavy, keep it higher. – bstempi Oct 17 '13 at 14:50
  • An idea I just had: If you don't know what kind of workload you'll have ahead of time, you could keep internal counters of the reads and writes to the tree. You could allow that ratio to determine the threshold. So, if a user starts inserting a ton of data, it backs off of the rebalancing. Once the user starts reading it and the ratio shifts, it could decide to rebalance. – bstempi Oct 17 '13 at 14:56
  • I'll write something more formal up with some sort of example when I get a chance. – bstempi Oct 17 '13 at 21:06

1 Answers1

0

It all depends on your use case. Take more measurements how your tree behaves in your case.

You may also try to measure how unbalanced the tree is and balance it when it reaches some threshold.

pierd
  • 1
  • 1
  • 1
  • Can you add some details or an example of how this can be done in pseudo code? This answer is bordering on just being a comment without that detail. – Ryan Gates Oct 17 '13 at 13:21