Robotics From Zero
Module: React To Change

Behavior Trees

Behavior trees are a modular, composable alternative to state machines. Learn sequences, selectors, conditions, actions, decorators, and the tick mechanism.

12 min read

Behavior Trees

A behavior tree (BT) is a hierarchical structure for organizing behavior. Unlike state machines (which focus on states), behavior trees focus on tasks.

The Core Idea

A behavior tree is a tree of nodes. Each frame, you tick the tree — starting at the root and flowing down to the leaves. Each node returns one of three statuses:

  • SUCCESS — the task completed successfully
  • FAILURE — the task failed
  • RUNNING — the task is still in progress

Nodes are composed in a hierarchy, and the result propagates up from child to parent.

Behavior tree node types — sequence (→), selector (?), condition (◇), action (□), and decorator (◈) with their return values
The five building blocks of behavior trees. Sequences run children in order (AND logic). Selectors try alternatives (OR logic). Conditions check booleans. Actions do work. Decorators modify child behavior (invert, repeat, timeout).

The Four Node Types

1. Sequences (AND logic)

A sequence node runs its children in order. If any child returns FAILURE, the sequence stops and returns FAILURE. If all children return SUCCESS, the sequence returns SUCCESS.

Sequence: [Approach Door, Open Door, Enter Room]

This is like saying: "Do A, then B, then C. If any step fails, the whole thing fails."

2. Selectors (OR logic)

A selector node tries its children in order until one succeeds. If a child returns SUCCESS, the selector stops and returns SUCCESS. If all children return FAILURE, the selector returns FAILURE.

Selector: [Use Main Entrance, Use Side Door, Use Emergency Exit]

This is like saying: "Try A. If that fails, try B. If that fails, try C."

3. Conditions

A condition node checks a boolean and immediately returns SUCCESS or FAILURE. Examples:

  • IsBatteryLow?
  • IsPathClear?
  • HasMission?

Conditions are the "leaves" that read sensor data or check internal state.

4. Actions

An action node performs a task. Examples:

  • MoveToGoal (returns RUNNING while moving, SUCCESS when reached, FAILURE if stuck)
  • OpenGripper (returns SUCCESS when opened)
  • PublishStatus (returns SUCCESS immediately)

Actions are the "leaves" that make the robot do something.

Example: Patrol and Charge

Let's build a behavior tree for a patrol robot that charges when low on battery.

How it works:

  1. Every frame, we tick the root (a selector).
  2. The selector tries the first child: "Handle Low Battery" sequence.
    • Condition: IsBatteryLow?
      • If battery is high → returns FAILURE
      • If battery is low → returns SUCCESS
    • If battery is low, the sequence continues to FindCharger, then DockAndCharge.
    • If the whole sequence succeeds, the selector returns SUCCESS (and stops — no patrolling this frame).
  3. If "Handle Low Battery" returns FAILURE (battery is fine), the selector tries the second child: "Patrol".
    • MoveToNextWaypoint → returns RUNNING while moving, SUCCESS when reached.
    • ScanForIntruders → returns SUCCESS after scanning.
Charging behavior tree — selector with battery handling (priority) and patrol (fallback) branches
This behavior tree gives battery handling higher priority than patrolling. The selector tries 'Handle Low Battery' first — if battery is fine (FAILURE), it falls through to 'Patrol.' Priority is simply determined by child order.

The key insight: battery handling has higher priority because it's the first child of the selector. If battery is low, patrol never runs.

Note

Behavior trees were originally developed for video game AI (think NPCs in Halo or The Sims). They've since become popular in robotics because they're easy to author, visualize, and reuse.

Decorators

A decorator wraps a child node and modifies its behavior. Common decorators:

DecoratorBehavior
InverterFlips SUCCESSFAILURE
RepeaterRuns child N times or until failure
RetryUntilSuccessKeeps retrying child until it succeeds
TimeoutReturns FAILURE if child takes too long
ForceSuccessAlways returns SUCCESS (useful for "try but don't care if it fails")

Example using an inverter:

The inverter flips the result of IsPathClear?. So if the path is blocked, the condition returns FAILURE, which the inverter flips to SUCCESS, allowing the sequence to proceed to StopAndReplan.

Parallel Nodes

Some behavior tree implementations include parallel nodes that tick multiple children simultaneously (not sequentially).

Parallel node — ticking multiple children simultaneously and combining their results with a success threshold
A parallel node ticks all children in the same frame. You configure a success threshold (e.g., 2 of 3 must succeed). This lets you run navigation, status publishing, and safety monitoring simultaneously.

This runs all three tasks in parallel. The parallel node succeeds if at least 2 of 3 children succeed.

Warning

"Parallel" in behavior trees doesn't mean multi-threading. It means all children are ticked in the same frame and their results are combined. The tasks still run sequentially at the code level — they just all get a chance to execute.

Ticking: The Execution Model

Unlike state machines (which react to events), behavior trees are polled. You call tree.tick() at a fixed rate (e.g., 10 Hz), and the tree evaluates from the root.

Behavior tree execution loop
class Robot:
    def __init__(self):
        self.bt = build_behavior_tree()
 
    def run(self):
        rate = 10  # Hz
        while True:
            status = self.bt.tick()
            if status == SUCCESS:
                print("Mission complete!")
                break
            elif status == FAILURE:
                print("Mission failed.")
                break
            # RUNNING means keep going
            sleep(1.0 / rate)
 
Tick propagation — showing how a tick flows from root through composite nodes down to leaf actions and conditions
Each tick flows from root to leaves. The root selector tries its first child (a sequence). The sequence evaluates its children in order. SUCCESS/FAILURE propagates back up. RUNNING means 'keep ticking me next frame.'

Each tick, the tree re-evaluates from scratch. This makes BTs reactive — if a condition changes (e.g., battery suddenly drops), the tree adapts on the next tick.

When Behavior Trees Excel

Behavior trees are ideal when:

  • You have many small, composable behaviors (search, approach, grasp, retreat)
  • Behaviors need to run in parallel or be prioritized (high-priority tasks try first)
  • You want to visualize and edit behavior in a GUI (game engines and robot frameworks offer visual BT editors)
  • You want to reuse subtrees (the "avoid obstacle" subtree can be used in many contexts)

They're less ideal when:

  • You have a small number of discrete modes (use a state machine)
  • Transitions need to be event-driven rather than polled
  • Performance is critical (ticking every frame has overhead)

What's Next?

Now you know both state machines and behavior trees. But when should you use each? In the next lesson, we'll compare the two and discuss trade-offs for different robot tasks.

Got questions? Join the community

Discuss this lesson, get help, and connect with other learners on r/softwarerobotics.

Join r/softwarerobotics

Frequently Asked Questions

What are behavior tree nodes?

Behavior tree nodes are the building blocks of a behavior tree. There are three main types: action nodes (perform tasks like "move to waypoint"), condition nodes (check the world state like "is battery low?"), and control nodes (determine execution flow). Control nodes include sequences (run children in order, stop on failure) and selectors (try children in order, stop on success).

How do behavior trees differ from state machines?

State machines define explicit states and transitions between them, which can lead to "state explosion" as complexity grows. Behavior trees organize behaviors hierarchically using composable nodes, making it easy to add, remove, or rearrange behaviors without rewriting transitions. Behavior trees also provide built-in support for preemption, fallback logic, and parallel execution.

What is a tick in behavior tree execution?

A tick is one evaluation cycle of the behavior tree. During each tick, the tree is traversed from the root downward, and each visited node returns a status: Success, Failure, or Running (still in progress). The robot's control loop calls tick at a fixed rate, typically 10 to 50 Hz, allowing the tree to continuously react to changing conditions and update the robot's behavior.

Further Reading

Related Lessons

Discussion

Sign in to join the discussion.