-3

How would I go about finding the percentage of red nodes in a red-black tree? I'm familiar with the properties of a red-black tree, but just can't seem to wrap my head around how to approach this. One idea I have is simply traversing the tree after building it and counting up the red nodes but it seems inefficient for larger inputs.

  • 1
    Sorry, StackOverflow is not a code-writing or basic tutorial service. Please visit the [help] and read [ask] to learn how to use this site effectively. – Jim Garrison Oct 20 '16 at 06:35
  • 2
    Are you supposed to solve this without building the tree, or are you supposed to first implement red-black trees and then use your implementation to find out? – molbdnilo Oct 20 '16 at 06:41
  • @molbdnilo I'm supposed to implement to red-black tree first which I've done. One way I've thought of calculating the percent of red nodes is by traversing over the tree and counting up # of red nodes but that seems inefficient for large values of n... – ProgrammingNoob Oct 20 '16 at 20:08
  • @JimGarrison sorry, I might have worded my question poorly. I'm not asking for code but rather methods to approach the problem of finding the percentage of red nodes in a red-black tree. – ProgrammingNoob Oct 20 '16 at 20:10

1 Answers1

1

Well, you can trade time for space...

If you store the number of red and black nodes in each subtree, you can determine this only by looking at the root.

It is a property of red-black trees that information you store in the nodes which can be updated only by looking at a node's children (and possibly its parent; my memory is a bit hazy) doesn't affect the complexity of the tree operations.
So your tree building will be slower, but only by a constant factor.

Whether this factor is small enough that you save any time in practice if you only need to determine this percentage once is a different matter.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82