java - How to collect variables from a tree formula -


the question title might poorly expressed, i'm sorry that. representing propositional logic formula tree in java, so:

abstract class formula { }  final class andformula extends formula {   private formula leftchild, rightchild;    public andformula(formula leftchild, formula rightchild) {     this.leftchild = leftchild;     this.rightchild = rightchild;   }    // add getters...  }  final class orformula extends formula {   private formula leftchild, rightchild;    public orformula(formula leftchild, formula rightchild) {     this.leftchild = leftchild;     this.rightchild = rightchild;   }    // add getters...  }  final class atomicformula extends formula {   private string name;    public atomicformula(string name) {     this.name = name;   }    // add getters...  } 

now let's want collect names of atomic formulas in particular way: want names of atomic formulas never appear on right hand side of orformula. have following:

public static set<string> specialvars(formula f) {    set<string> ans = new hashset<string>();    if( f.getclass() == andformula.class ) {     // recursively add special vars left child:     ans.addall( specialsvar( f.leftchild ) );      // recursively add special vars right child:     ans.addall( specialsvar( f.rightchild ) );   } else if( f.getclass() == orformula.class ) {     // recursively add special vars left child:     ans.addall( specialsvar( f.leftchild ) );      // don't add right child!   } else if( f.getclass() === atomicformula.class ) {     // base case:     ans.add( f.getname() );   }    return ans; } 

this code seems work, question is: idiomatic java? if not, idiomatic way write code? there unexpected behaviour should aware of?

thanks!


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -