Reputation: 774
I was wondering how inserting an equation such as 5 * 4 + 2 / 3
into a binary tree would work. I have tried doing this on my own however I can only make the tree grow to one side.
Upvotes: 0
Views: 294
Reputation: 2473
I am not an expert in the field, but I wrote basic expression parsers in the past.
You will need to tokenize
your expression. Transform it from a string of characters to a list
of understandable chunks.
You may want to create two structures, one for operators and one for operands. Hint, operators have a priority associated with them.
You can apply an algorithm to transform your operators/operands to an abstract syntax tree
(AST) it's basically just a set of rules, generally used with a queue and a stack.
Upvotes: 1