0

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.

User9123
  • 675
  • 1
  • 8
  • 20

1 Answers1

1

I am not an expert in the field, but I wrote basic expression parsers in the past.

  1. You will need to tokenize your expression. Transform it from a string of characters to a list of understandable chunks.

  2. You may want to create two structures, one for operators and one for operands. Hint, operators have a priority associated with them.

  3. 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.

Swann
  • 2,413
  • 2
  • 20
  • 28