diff --git a/chapters/_subfiles/trees/_racket-evaluate_exm.qmd b/chapters/_subfiles/trees/_racket-evaluate_exm.qmd index e7013cc4b..3661155ba 100644 --- a/chapters/_subfiles/trees/_racket-evaluate_exm.qmd +++ b/chapters/_subfiles/trees/_racket-evaluate_exm.qmd @@ -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)] @@ -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))) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-height_exm.qmd b/chapters/_subfiles/trees/_racket-height_exm.qmd index 098b32732..351effceb 100644 --- a/chapters/_subfiles/trees/_racket-height_exm.qmd +++ b/chapters/_subfiles/trees/_racket-height_exm.qmd @@ -3,7 +3,8 @@ The recursive formulas of @rem-size-height become -```racket +```{racket} +#| code-fold: false (define (size T) (if (empty? T) 0 @@ -17,7 +18,14 @@ The recursive formulas of @rem-size-height become 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 '()) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-level-order_exm.qmd b/chapters/_subfiles/trees/_racket-level-order_exm.qmd index 14cf8b2d2..0dec9b3c7 100644 --- a/chapters/_subfiles/trees/_racket-level-order_exm.qmd +++ b/chapters/_subfiles/trees/_racket-level-order_exm.qmd @@ -6,7 +6,8 @@ Here the queue holds subtrees rather than vertices: 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 @@ -26,7 +27,13 @@ and removing an empty tree adds nothing. - `(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. diff --git a/chapters/_subfiles/trees/_racket-postfix_exm.qmd b/chapters/_subfiles/trees/_racket-postfix_exm.qmd index 312e70029..96437cdb3 100644 --- a/chapters/_subfiles/trees/_racket-postfix_exm.qmd +++ b/chapters/_subfiles/trees/_racket-postfix_exm.qmd @@ -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)) @@ -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 * + *)) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-traversals_exm.qmd b/chapters/_subfiles/trees/_racket-traversals_exm.qmd index 30d44607b..43c8feed9 100644 --- a/chapters/_subfiles/trees/_racket-traversals_exm.qmd +++ b/chapters/_subfiles/trees/_racket-traversals_exm.qmd @@ -4,7 +4,8 @@ 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 @@ -26,9 +27,13 @@ with `B` as in @exr-racket-tree: ``` `(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) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-tree_exr.qmd b/chapters/_subfiles/trees/_racket-tree_exr.qmd index 1de2a7379..743886dd6 100644 --- a/chapters/_subfiles/trees/_racket-tree_exr.qmd +++ b/chapters/_subfiles/trees/_racket-tree_exr.qmd @@ -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 () ()))) diff --git a/chapters/_subfiles/trees/_racket-tree_sol.qmd b/chapters/_subfiles/trees/_racket-tree_sol.qmd index 94b3fef81..36aa2f25e 100644 --- a/chapters/_subfiles/trees/_racket-tree_sol.qmd +++ b/chapters/_subfiles/trees/_racket-tree_sol.qmd @@ -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))) +``` + ::: diff --git a/chapters/trees.qmd b/chapters/trees.qmd index 57f0657a4..08227e2df 100644 --- a/chapters/trees.qmd +++ b/chapters/trees.qmd @@ -13,6 +13,8 @@ format: {{< include ../latex-macros/macros.qmd >}} +{{< include _subfiles/_racket-setup.qmd >}} + ::: {.callout-note appearance='simple' collapse=true} ## Source