I'm building a program to determine how many red nodes are in a red black tree. I have already implemented a RED BLACK BST which builds the tree after reading a text input. I'm stuck on how to count the number of red nodes? I know the red nodes can only be left leaning and a red parent node can't have a red child. What would be the appropriate method on attacking this problem?
Asked
Active
Viewed 2,559 times
-1
-
1Please share the code you have so far (building the tree and counting nodes)! – alexbt Oct 23 '16 at 00:09
-
I've added what i have – user3408536 Oct 23 '16 at 20:13
-
You would pass the root node to your countRed method, than recursively call it again with left & right node (while calling isRed). You should give it a try! – alexbt Oct 23 '16 at 20:51
-
@Alex I've updated my code! I'm currently getting a null pointer exception however – user3408536 Oct 23 '16 at 21:22
-
actually now I'm getting this error: non-static variable root cannot be referenced from a static context when i run it in my main method – user3408536 Oct 23 '16 at 21:34
1 Answers
0
A recursive solution would look like this:
public static int countRed(Node node) {
int nbRed = 0;
if (node == null) {
return 0;
}
nbRed += countRed(node.left);
nbRed += countRed(node.right);
if(node.isRed()){
nbRed++;
}
return nbRed;
}
Tested
I tested your code with a simple loop:
RedBlackBST tree = new RedBlackBST();
tree.root = tree.put(null, 0, 0);
for (int i = 1; i < 10; i++) {
tree.root = tree.put(tree.root, i, i);
}
int redTotal = tree.countRed(tree.root);
This returns a total of 3 reds. If I inspect it in debug, it's accurate. If you feel the value isn't valid, then you should inspect your tree in debug and identify what isn't valid in your structure.

alexbt
- 16,415
- 6
- 78
- 87
-
thank you! However I still get the error: non-static variable root cannot be referenced from a static context when i run it in my main method... and when i change my root node to static then it says that non-static method node cannot be referenced from a static context when i run it in my main method? – user3408536 Oct 23 '16 at 21:47
-
Change 'tree.countRed(tree);' to 'tree.countRed(tree.root);' AND 'if (root.isRed(root))' to 'if (isRed(root))' – alexbt Oct 23 '16 at 22:16
-
Your code would benefit from a little refactoring. the method 'isRed' for instance => it should/could be a method in Node so you would call 'node.isRed()' instead of 'isRed(node)' – alexbt Oct 23 '16 at 22:18
-
thanks for all your help! It compiles and runs now however it doesn't count the nodes correctly so I guess i'll start over. – user3408536 Oct 23 '16 at 22:34
-
the code for the other methods was provided by the textbook so do you think the problem is in my count method then? – user3408536 Oct 23 '16 at 23:05
-
I tested creating a few node and calling countRed. I inspected the tree structure and the value was a match. If you expect another value, my guess is that the structure isn't built correctly. Anyhow, put a breakpoint and take some time to inspect your structure. You can at least confirm that the count is good, then the issue would be in rotateLeft/right (or the surrounding if-elses). – alexbt Oct 23 '16 at 23:40