Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions chapters/_subfiles/trees/_racket-evaluate_exm.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ and as a list `(op a b)` for an operation `op` with operands `a` and `b`.
The tree of @fig-expression is `'(* (- 7 2) (+ 3 (* 4 2)))`.
These functions compute its value, working from the leaves up:

```racket
```{racket}
#| code-fold: false
(define (apply-op op a b)
(cond [(eq? op '+) (+ a b)]
[(eq? op '-) (- a b)]
Expand All @@ -23,8 +24,14 @@ These functions compute its value, working from the leaves up:
- `(eq? x y)` is true when `x` and `y` are the same symbol.
- `(number? e)` is true when `e` is a number.

`(evaluate '(* (- 7 2) (+ 3 (* 4 2))))` returns 55.
Without the quote, `(* (- 7 2) (+ 3 (* 4 2)))` is itself Racket code, and it also returns 55:
Racket writes each arithmetic operation in prefix form (@def-prefix-form), with a pair of parentheses around each operation.
`evaluate` computes the value of the tree.
Without the quote, `(* (- 7 2) (+ 3 (* 4 2)))` is itself Racket code, and it has the same value,
because Racket writes each arithmetic operation in [prefix form](#def-prefix-form), with a pair of parentheses around each operation:

```{racket}
#| code-fold: false
(evaluate '(* (- 7 2) (+ 3 (* 4 2))))
(* (- 7 2) (+ 3 (* 4 2)))
```

:::
14 changes: 11 additions & 3 deletions chapters/_subfiles/trees/_racket-height_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
::: {#exm-racket-height}

Check warning on line 1 in chapters/_subfiles/trees/_racket-height_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-racket-height has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Size and height in Racket

The recursive formulas of @rem-size-height become

```racket
```{racket}
#| code-fold: false
(define (size T)
(if (empty? T)
0
Expand All @@ -17,7 +18,14 @@

Here `(max a b)` returns the larger of the numbers `a` and `b`.

With `B` as in @exr-racket-tree, `(size B)` returns 9 and `(height B)` returns 3,
and `(height '())` returns -1.
With `B` as in @exr-racket-tree,
these calls give the size and the height of $B$, and the height of the empty tree:

```{racket}
#| code-fold: false
(size B)
(height B)
(height '())
```

:::
11 changes: 9 additions & 2 deletions chapters/_subfiles/trees/_racket-level-order_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
::: {#exm-racket-level-order}

Check warning on line 1 in chapters/_subfiles/trees/_racket-level-order_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-racket-level-order has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Level order in Racket

The level order (@def-level-order) needs a queue (@def-queue).
Expand All @@ -6,7 +6,8 @@
removing a subtree that is not empty lists its root and adds its two subtrees at the back,
and removing an empty tree adds nothing.

```racket
```{racket}
#| code-fold: false
(define (level-loop queue)
(if (empty? queue)
empty
Expand All @@ -26,7 +27,13 @@
- `(append xs ys)` is the list of the elements of `xs` followed by those of `ys`,
so the two subtrees go to the back of the queue.

With `B` as in @exr-racket-tree, `(level-order B)` returns `'(m f t c h r w j p)`, the level order of @exm-level-order.
With `B` as in @exr-racket-tree, `level-order` returns the level order of @exm-level-order:

```{racket}
#| code-fold: false
(level-order B)
```

Each subtree that is not empty enters the queue when its parent's subtree is removed,
just as `bfs` marks a child when it removes the parent.

Expand Down
10 changes: 8 additions & 2 deletions chapters/_subfiles/trees/_racket-postfix_exm.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ The method of @exm-postfix-stack uses a list as the stack (@def-stack):
`cons` pushes, and `first` and `rest` pop.
With `apply-op` from @exm-racket-evaluate,

```racket
```{racket}
#| code-fold: false
(define (postfix-loop symbols stack)
(cond [(empty? symbols) (first stack)]
[(number? (first symbols))
Expand All @@ -22,6 +23,11 @@ With `apply-op` from @exm-racket-evaluate,
```

In the last clause, `(first stack)` is the number $b$ popped first and `(second stack)` is the number $a$ under it.
`(evaluate-postfix '(7 2 - 3 4 2 * + *))` returns 55.
The postfix form of the tree of @fig-expression has the same value as the tree:

```{racket}
#| code-fold: false
(evaluate-postfix '(7 2 - 3 4 2 * + *))
```

:::
13 changes: 9 additions & 4 deletions chapters/_subfiles/trees/_racket-traversals_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
::: {#exm-racket-traversals}

Check warning on line 1 in chapters/_subfiles/trees/_racket-traversals_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-racket-traversals has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### The three traversals in Racket

These functions follow the definitions of preorder (@def-preorder), inorder (@def-inorder) and postorder (@def-postorder),
with `B` as in @exr-racket-tree:

```racket
```{racket}
#| code-fold: false
(define (preorder T)
(if (empty? T)
empty
Expand All @@ -26,9 +27,13 @@
```

`(append L M N)` is the list of the elements of `L`, then `M`, then `N`.
The three traversals of `B` are:

- `(preorder B)` returns `'(m f c h j t r p w)`.
- `(inorder B)` returns `'(c f h j m p r t w)`.
- `(postorder B)` returns `'(c j h f p r w t m)`.
```{racket}
#| code-fold: false
(preorder B)
(inorder B)
(postorder B)
```

:::
3 changes: 2 additions & 1 deletion chapters/_subfiles/trees/_racket-tree_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ In Racket, store the empty tree (@def-binary-tree) as `'()`,
and $\operatorname{node}(x, L, R)$ as the list `(list x L R)` of its label and its two subtrees.
The binary tree $B$ of @exm-binary-tree is then

```racket
```{racket}
#| code-fold: false
(define B
'(m (f (c () ())
(h () (j () ())))
Expand Down
9 changes: 9 additions & 0 deletions chapters/_subfiles/trees/_racket-tree_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ It returns `'r`.
Its second element is its left subtree, `'(r (p () ()) ())`,
and the first element of that is its label, `'r`.

Racket returns the same values:

```{racket}
#| code-fold: false
(third B)
(second (third B))
(first (second (third B)))
```

:::
2 changes: 2 additions & 0 deletions chapters/trees.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ format:

{{< include ../latex-macros/macros.qmd >}}

{{< include _subfiles/_racket-setup.qmd >}}

::: {.callout-note appearance='simple' collapse=true}
## Source

Expand Down
Loading