Constructing truth table headings in boolean logic with Java

762 views Asked by At

I am working on a small project to construct truth table based on an infix boolean logic expressions. Example:

A ∧ (B ∨ C)

I am able to convert this to postfix:

ABC∨∧

and thus am able to construct the expression into a binary tree (can convert the tree back to infix, get the postfix or prefix):

Syntax Tree of ABC∨∧:

Syntax Tree of ABC∨∧

digraph graphname {
    node1 [label="∧"];
    node2 [label="A"];
    node3 [label="∨"];
    node6 [label="B"];
    node7 [label="C"];
    node1 -> node2;
    node1 -> node3;
    node3 -> node6;
    node3 -> node7;
}

Question

My question is when constructing truth tables, how do I figure out the rest of the column headings required to complete the truth table?

Currently I am able to generate all the permutations on the symbols:

| A | B | C |
| - | - | - |
| F | F | F |
| F | F | T |
| F | T | F |
| F | T | T |
| T | F | F |
| T | F | T |
| T | T | F |
| T | T | T |

Essentially to get these headings I just create a Set<Character> of distinct alphabetical characters based on the input.

However, this isn't a complete truth table for A ∧ (B ∨ C). For a complete truth table I need to add the columns (B ∨ C) and A ∧ (B ∨ C) so the complete truth table will look like:

| A | B | C | (B ∨ C) | A ∧ (B ∨ C) |
| - | - | - | ------- | ----------- |
| F | F | F |         |             |
| F | F | T |         |             |
| F | T | F |         |             |
| F | T | T |         |             |
| T | F | F |         |             |
| T | F | T |         |             |
| T | T | F |         |             |
| T | T | T |         |             |

So, how would I programmatically generate the additional headings needed (i.e. ones with logical connectives) enabling me to generate its semantic meaning?

Would some tree-traversal algorithm do this? Or would you just work with the infix/postfix version of the input?

Some Java code would be helpful, but an approach/algorithm would suffice :)

Thanks!

0

There are 0 answers