Pre-algebra Refresh · 20 min

Expressions are trees

Operator precedence isn't a slogan to memorize; it's a parser. Every expression has exactly one tree, and evaluating it is a climb up that tree.

0 / 0

A string with exactly one meaning

Type 3 + 4 * 2 ** 2 into a Python prompt and into a JavaScript console. Both say 19. Not 196, not 49, but 19, every time, on every machine.

That agreement is not luck. It’s a grammar. And the grammar you half-remember as “PEMDAS” is the exact same thing a programming language uses to read your code.

Precedence is a parser

“Order of operations” gets taught as a slogan. Drop the slogan. Here is what it actually is: a set of precedence rules that take a flat string of symbols and decide, with no ambiguity, what groups with what.

Exponents bind tighter than ×\times and ÷\div; those bind tighter than ++ and -. “Binds tighter” means “groups first.” It’s the same precedence table printed in the C language reference. And the point of all of it is a single guarantee: every well-formed expression has exactly one value.

The shape behind the string

That grouping has a shape, a tree. Operators are the branch points; numbers are the leaves. The expression 3+4×223 + 4 \times 2^2 parses to a tree with ++ at the root, 33 on one side, and the whole subtree 4×224 \times 2^2 on the other.

Post-order traversal = the forward pass: leaves first, root last, every node resolved before its parent.

Type an expression. The widget parses it into its tree, the same parse a JavaScript engine performs. Move the parentheses and watch the tree re-shape. The string is just the tree, flattened onto one line so it fits on a page.

Evaluating is climbing the tree

To get the value, you climb from the leaves upward. A branch can’t compute until both its children are known.

222^2 resolves to 44. Now 4×44 \times 4 resolves to 1616. Now 3+163 + 16 resolves to 1919. Children before parents; computer scientists call that a post-order traversal. Press Step in the widget and watch it happen, one node at a time.

Climb the tree

Evaluate the tree for 3+4×223 + 4 \times 2^2, deepest branch first.

What is the value?

Exponents are repeated multiplication

One node type in that tree was an exponent. ana^n, for a counting number nn, is just nn copies of aa multiplied together: 24=2×2×2×2=162^4 = 2\times2\times2\times2 = 16. Nothing deeper than that.

And a0=1a^0 = 1. Not zero, one. Walk the pattern downward: 23=82^3 = 8, 22=42^2 = 4, 21=22^1 = 2. Each step divides by 22. One more step lands on 20=12^0 = 1. The pattern forces it. (The full rulebook for exponents is module 2’s job; today you only need this much.)

A zero exponent

Evaluate, remembering what a zero exponent does:

50+235^0 + 2^3

What is the value?

One expression, one value: why it matters

The whole reason precedence rules exist is that guarantee from earlier: one expression, one value, no ambiguity.

That determinism is not a school nicety. It is the property that lets a computation be trusted at all: feed the same inputs to the same expression and you get the same output, every single run.

Where this goes

A neural network is an arithmetic expression. A very wide one, with billions of leaves and branch points instead of four, but the same kind of object: numbers at the bottom, operations stacked above.

Running the network to get a prediction is exactly what you just did to that little tree: climb from the leaves up, children before parents. That climb has a name you will hear constantly from module 16 onward. It’s called the forward pass. You just ran one.

Lesson complete

Nice tinkering.