boofun.visualization.decision_tree

Decision tree visualization for Boolean functions.

This module provides tools to visualize decision trees that compute Boolean functions, including optimal trees and user-specified trees.

Functions

build_optimal_decision_tree(f[, ...])

Build an optimal (or near-optimal) decision tree for f.

decision_tree_to_dict(node)

Convert decision tree to dictionary for serialization.

plot_decision_tree(f[, tree, figsize, ...])

Plot a decision tree for a Boolean function.

Classes

DecisionTreeNode([is_leaf, value, variable, ...])

Node in a decision tree for a Boolean function.

class boofun.visualization.decision_tree.DecisionTreeNode(is_leaf: bool = False, value: int | None = None, variable: int | None = None, left: DecisionTreeNode | None = None, right: DecisionTreeNode | None = None, depth: int = 0)[source]

Node in a decision tree for a Boolean function.

Can be either: - Internal node: queries a variable, has left (0) and right (1) children - Leaf node: outputs a constant value (0 or 1)

__init__(is_leaf: bool = False, value: int | None = None, variable: int | None = None, left: DecisionTreeNode | None = None, right: DecisionTreeNode | None = None, depth: int = 0)[source]

Create a decision tree node.

Parameters:
  • is_leaf – True if this is a leaf node

  • value – Output value for leaf nodes (0 or 1)

  • variable – Variable index to query for internal nodes

  • left – Child for variable=0

  • right – Child for variable=1

  • depth – Depth of this node in the tree

evaluate(x: int) int[source]

Evaluate the decision tree on input x.

boofun.visualization.decision_tree.build_optimal_decision_tree(f: BooleanFunction, available_vars: List[int] | None = None, inputs: List[int] | None = None, depth: int = 0, max_depth: int = 20) DecisionTreeNode[source]

Build an optimal (or near-optimal) decision tree for f.

Uses a greedy algorithm based on influence to select variables.

Parameters:
  • f – BooleanFunction to build tree for

  • available_vars – Variables not yet queried (None = all)

  • inputs – Current set of inputs to distinguish (None = all)

  • depth – Current depth in the tree

  • max_depth – Maximum allowed depth

Returns:

Root of the decision tree

boofun.visualization.decision_tree.plot_decision_tree(f: BooleanFunction, tree: DecisionTreeNode | None = None, figsize: Tuple[int, int] = (12, 8), save_path: str | None = None, show: bool = True, node_size: int = 2000, font_size: int = 10) Any[source]

Plot a decision tree for a Boolean function.

Parameters:
  • f – BooleanFunction (used to build tree if not provided)

  • tree – Pre-built decision tree (None to build optimal)

  • figsize – Figure size

  • save_path – Path to save the plot

  • show – Whether to display

  • node_size – Size of tree nodes

  • font_size – Font size for labels

Returns:

Figure object

boofun.visualization.decision_tree.decision_tree_to_dict(node: DecisionTreeNode) Dict[str, Any][source]

Convert decision tree to dictionary for serialization.