Mathematical formula to a recursive method

Viewed 426

I need to implement current formula.It is for scoring the nodes in a taxonomy. Basically, the score of a node depends on the amount of child nodes and their scores ((nodes(h+1)) is the amount of nodes on the next level and Cl(concept) is a set of children).

Formula

In my usecase term frequencies are only defined for the leaves as of now. I have made an implementation, but the issue is that when there are 2 children for the node then the implementation only goes to one side.

For a given taxonomy:

     1
    / \
   2   3
   |   |
   4   17
  / \
 11 13

the frequencies are given: freq(11) = 3, freq(13) = 5 and freq(17) = 10. When I try to get the score for node(1), the result is 0.0, because the recursion doesn't go into descendent node(4), it retrieves only freq(17) and that's it. Normally, the result should be 7.

Here is the implementation:

public static float calcScore(int keyID, Map<Integer, Integer> frequencies, Map<Integer, Integer> subTaxonomy) {
    float res = 0f;
    int nodes = 0;
    if (frequencies.containsKey(keyID)) {
        return frequencies.get(keyID) + 0f;
    }

    for (Map.Entry<Integer, Integer> entry : subTaxonomy.entrySet()) {
        if (entry.getValue() - 1 == subTaxonomy.get(keyID)) {
            nodes++;
            res += calcScore(entry.getKey(), frequencies, subTaxonomy);
        }
    }
    return 1 / nodes * res;
}

NOTE:

subTaxonomy - stores the nodeID and its level in the taxonomy

frequencies - store the frequencies for leaf nodes.

I also created a snippet at Ideone: Source

How should I edit the code, so that it traverses all over the children for a given node?

UPDATE

So now, in updated source, it traverses all over taxonomy, but the result is 0.0 still.

2 Answers
Related