Re: Determining the height of a non-binary tree

Eibe Frank <[email protected]>
Newsgroups gmane.comp.ai.weka
Message-ID <CADehzLV9zzx3DF1jpbuwhC0QuiiwSrCciGWoBXruGD-0MV64Mg@mail.gmail.com>
The code that follows gives the desired result in WEKA's Groovy console.

Replace the name of the dataset in the main method to make this work for
you.

Cheers,
Eibe


package weka.classifiers.trees.j48;

import weka.core.Instances;
import weka.classifiers.trees.J48;

class MyJ48 extends J48 {

   public static int getHeight(ClassifierTree data){
    int height=0;
    if (data.isLeaf()){
      return 0;
    }
    for (int i=0; i<data.m_sons.length; i++) {
      height = Math.max(height, getHeight(data.m_sons[i]));
    }
    return height + 1;
  }

  public static void main(String[] args) {

    Instances data = (new
weka.core.converters.ConverterUtils.DataSource("C:/Users/eibe/datasets/UCI/anneal.arff")).getDataSet();
    data.setClassIndex(data.numAttributes() - 1);
    J48 j48 = new J48();
    j48.buildClassifier(data);
    System.out.println(j48);
    System.out.println("Height: " + MyJ48.getHeight(j48.m_root));
  }
}



On Fri, Jun 25, 2021 at 7:20 PM <[email protected]> wrote:

> Dear Professor Eibe Frank,
>
> I have changed my code with the following one:
>
> int getHeight(ClassifierTree data){
>     int height=0;
>     if (data==null){
>       return 0;
>     }
>     for (int i=0; i<data.m_sons.length; i++) {
>       height = Math.max(height, getHeight(data.m_sons[i]));
>     }
>     return height + 1;
>   }
>
> but I don't know with which argument to call the function
> in order to supply to it the ClassifierTree. I tried
>
> height = getHeight(new ClassifierTree(m_toSelectModel));
>
> but this gives me a NullPointerException!
>
> 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.