Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Methods to count even numbers and nodes with two children in a BST

$
0
0
Hello

I'm having problems with two methods of my BST

    public int countEvens() {
        return countEvensHelper(root);
    }

    private int countEvensHelper(BinaryTreeNode p) {
        int count = 0;

        if (p != null) {
            if (((intElement) p.info).getNum() % 2 == 0) {
                count = 1;
            }
        }
        return (count + countEvensHelper(p.llink) + countEvensHelper(p.rlink));
    }
//*************************************************//

    public int countTwoChildren() {
        return countTwoChildrenHelper(root);
    }

    private int countTwoChildrenHelper(BinaryTreeNode p) {
        int count = 0;
        if (p == null) {
            return 0;
        }
        if (p.llink != null && p.rlink != null) {

            count = 1;
        }
        return count + countTwoChildrenHelper(p.llink) + countTwoChildrenHelper(p.rlink);
    }


even though I think the logic is right, both methods return nothing. I even tried a lot of different things but it just won't work..
and for the countEvens method it keeps insisting that the error is in this line

        return (count + countEvensHelper(p.llink) + countEvensHelper(p.rlink));


I hope someone could point out my mistakes

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>