Solving Logic Puzzles with Binary Trees

Initializing live version
Download to Desktop

Requires a Wolfram Notebook System

Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

This Demonstration uses a binary tree representation of Boolean functions with two to four arguments to solve simple propositional logic puzzles.

Contributed by: Izidor Hafner (September 2016)
Open content licensed under CC BY-NC-SA



This Demonstration uses the setting of the "knights and knaves" logic puzzles. These puzzles pertain to an island in which some natives called "knights" always tell the truth and others called "knaves" always lie. Assume that every inhabitant of the island is either a knight or a knave.

If an inhabitant makes a statement , then we may conclude that is a knight if and only if is true. Such a fact is symbolically represented by .

Suppose there are three inhabitants , , , and the first two make the following claims:

: is a knight and is a knave.

: I am a knave and is a knave.

Symbolically we may express the example as: .

If ( is a knight), then is a knave, because knights tell the truth and said he was a knave. This is a contradiction, so is a knave. Therefore, choose a tree with and as the first nodes from the root. We seek a solution only on the right side of the tree. But from 's statement, must be a knight, and from 's statement it follows that is a knave.


[1] R. Audi, ed., The Cambridge Dictionary of Philosophy, Cambridge: Cambridge University Press, 1995 pp. 780–782.

[2] L. Borkowski, Elementy logiki formalnej (Elements of Formal Logic, in Polish), 3rd ed., Warsaw: Wyd, 1976.

[3] L. Carroll, Symbolic Logic and the Game of Logic, New York: Dover, 1958.

[4] I. M. Copi and C. Cohen, Introduction to Logic, 9th ed., New York: Macmillan, 1994 pp. 214–218.

[5] M. Gardner, Logic Machines, Diagrams and Boolean Algebra, New York: Dover Publications, 1968.

Feedback (field required)
Email (field required) Name
Occupation Organization
Note: Your message & contact information may be shared with the author of any specific Demonstration for which you give feedback.