Trees Query

Hey, I'm a newbie C programmer doing first semester kind of coding. I'm
attempting to read input from the keyboard of the form "Af RtS"
representing a boolean logic expression, here it represents ((NOT a AND f)
OR (NOT R and t and NOT s)).

This will (eventually) get simplified as much as possible and be used to
generate a PostScript file to draw a circuit diagram (logic gates etc).
I've only just started though and I'm wondering what the best data
structure to store characters is. A linked list will work with the simple
things I'm doing now but ideally I want to be able to use parenthesis to
allow more complicated statements to be made. At the moment I have a kind
of binary tree with each node having an AND and an OR pointer.

A -OR-> R

| |
and and
v v

f t
|
and
v

S

Traversing it by going down each branch sequentially... Not sure how I'm
going to allow parentheses though.

Just wondering if this is a decent way of doing it, any suggestions on
improvements or different ideas much appreciared. Also any comments on
general coding style etc. would be helpful.

Advertisements

ekiMbo wrote:
> Hey, I'm a newbie C programmer doing first semester kind of coding. I'm
> attempting to read input from the keyboard of the form "Af RtS"
> representing a boolean logic expression, here it represents ((NOT a AND f)
> OR (NOT R and t and NOT s)).
>
> This will (eventually) get simplified as much as possible and be used to
> generate a PostScript file to draw a circuit diagram (logic gates etc).
> I've only just started though and I'm wondering what the best data
> structure to store characters is. A linked list will work with the simple
> things I'm doing now but ideally I want to be able to use parenthesis to
> allow more complicated statements to be made. At the moment I have a kind
> of binary tree with each node having an AND and an OR pointer.
>
> A -OR-> R
>
> | |
> and and
> v v
>
> f t
> |
> and
> v
>
> S
>
> Traversing it by going down each branch sequentially... Not sure how I'm
> going to allow parentheses though.
>
> Just wondering if this is a decent way of doing it, any suggestions on
> improvements or different ideas much appreciared. Also any comments on
> general coding style etc. would be helpful.

Algorithms and the like are offtopic here.
<OT>
If you are going to evaluate the expressions eventually and for one
set of values for your variables, I suggest going directly for it by
using operator precedence and (recursive) function calls. Only for
evaluating many state combinations of the variables I would go to
the effort of handling a binary tree.
You pass on the read-in string or copies of parts (but I would then
rather use explicit AND and OR operators).

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!