Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to set a node on expression tree

 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to build an expression tree for the first time. There will be a prefix expression entered and I am thinking that I can check if there is an operator and if there is I can set the root node equal to the operator and if a number is the next character I can and the next char will be an int as well so i'd but I get an error because the interface is set to take TreeNode parameter. How Do I get it to accept an int as a TreeNode? This is what I have so far.

And this is my interface
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting an error that says I cannot use an int where TreeNode is supposed to be.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So use a TreeNode then. Your TreeNode can contain any object, can't it? Or at least you should have an implementation which can act as a leaf of the tree and contain an integer or a variable, right?
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So instead of making single int type i'd make it TreeNode type and the Character.isDigit((Char)single)) would still work, I get it know. I feel silly now thanks.
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will I still be able to apply an operator(+, -, *, /) to the number if it is of type TreeNode?
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I get an error since I'm using Character.isDigit((char)single)).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do realise that the value of a char like '2' is not 2? A char is not a character, but a number, so you need to check whether you are passing 2, or more likely 0x32 (=50 in decimal) for that char '2'.
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand somewhat... So I need to remove (char) and Character and have single of type TreeNode so it may work?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic