06.09.2021 Views

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

20.2 Traversing trees 209<br />

20.2 Traversing trees<br />

Any time you see a new data structure, your first question should be, “<strong>How</strong> do I<br />

traverse it?” The most natural way <strong>to</strong> traverse a tree is recursively. For example,<br />

if the tree contains integers as cargo, this function returns their sum:<br />

def <strong>to</strong>tal(tree):<br />

if tree == None: return 0<br />

return <strong>to</strong>tal(tree.left) + <strong>to</strong>tal(tree.right) + tree.cargo<br />

The base case is the empty tree, which contains no cargo, so the sum is 0. The<br />

recursive step makes two recursive calls <strong>to</strong> find the sum of the child trees. When<br />

the recursive calls complete, we add the cargo of the parent and return the <strong>to</strong>tal.<br />

20.3 Expression trees<br />

A tree is a natural way <strong>to</strong> represent the structure of an expression. Unlike other<br />

notations, it can represent the computation unambiguously. For example, the<br />

infix expression 1 + 2 * 3 is ambiguous unless we know that the multiplication<br />

happens before the addition.<br />

This expression tree represents the same computation:<br />

tree<br />

cargo +<br />

left<br />

right<br />

cargo 1<br />

left<br />

right<br />

cargo<br />

left<br />

*<br />

right<br />

cargo 2<br />

cargo 3<br />

left right left right<br />

The nodes of an expression tree can be operands like 1 and 2 or opera<strong>to</strong>rs like<br />

+ and *. Operands are leaf nodes; opera<strong>to</strong>r nodes contain references <strong>to</strong> their<br />

operands. (All of these opera<strong>to</strong>rs are binary, meaning they have exactly two<br />

operands.)<br />

We can build this tree like this:<br />

>>> tree = Tree(’+’, Tree(1), Tree(’*’, Tree(2), Tree(3)))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!