Re: Determining the height of a non-binary tree

"Potschadtke, Jens" <[email protected]>
Newsgroups gmane.comp.ai.weka
Message-ID <AM0PR04MB61805F4B1AE8EC76B164377E8F079@AM0PR04MB6180.eurprd04.prod.outlook.com>
Hello,

well I think this code is an unbound recursive function (a function that calls itself).
The problem seems to be that the recursive call doesn't depend on the called data.

Take for example the case when initially  m_sons[0].m_train != null.
Assume you first enter the function with an argument != null, you get to the for loop and call getHeight() with an argument != null.
So, you enter the function, get again to the for loop and call getHeight() again with the same argument != null.
And so on until you run out of stack space.

So, in essence you are not traversing the tree. In order to do that, you may have to call getHeight() with something derived from the current argument "data" (e.g. data.m_sons[i].m_train)

Best wishes,
Jens

-----Ursprüngliche Nachricht-----
Von: [email protected] <[email protected]> 
Gesendet: Donnerstag, 24. Juni 2021 09:23
An: [email protected]
Betreff: [Wekalist] Determining the height of a non-binary tree

Dear Weka developers,

I am trying to build a Level-Order (a.k.a. Breadth First) Tree Traversal technique for J48. In order to do this recursively, I have to determine first the height of the tree. The method I have written is as follows:

int getHeight(Instances data){
    int height=0;
    if (data==null){
      return 0;
    } else {
      for (int i=0; i<m_sons.length; i++) {
        if (i == 0) {
          height = getHeight(m_sons[i].m_train);
        } else {
          int currentHeight = getHeight(m_sons[i].m_train);
          if (currentHeight > height) {
            height = currentHeight + 1;
          }
        }
      }
    }
    return height;
  }

I get a StackOverflowException and at the code, the message that the expression m_sons[i].m_train can not be evaluated.
Could you please help?

Thank you in advance,

Spyros Halkidis
_______________________________________________
Wekalist mailing list -- [email protected] Send posts to [email protected] To unsubscribe send an email to [email protected] To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.