User9123
User9123

Reputation: 774

Insert math equation into binary tree in C

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

Answers (1)

Swann
Swann

Reputation: 2473

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.

Upvotes: 1

Related Questions