I'm trying to define a tree using Racket's #plai language, though I'm struggling a bit when it comes to creating the tree through an "add-node" function.
The define-type I'm using goes like this:
(define-type BBT
[empty]
[leaf (elem number?)]
[node (elem number?) (left BBT?) (right BBT?)])
And I'd like to construct a tree that looks like this:
(node 3 (empty) (node 5 (leaf 4) (leaf 6)))
Following this call:
(add-node 6 (add-node 4 (add-node 5 (add-node 3 (empty)))))
So far, I've tried the following:
(define (add-node n tree)
(match tree
[(empty) (node n (empty) (empty))]))
And that works for an empty tree, as it just adds a single node with empty trees at both sides, but I haven't found a way to construct a tree like the example I shared above.
Any tips or suggestions as to what should I do? I know I should use recursion when it catches the "node" case, but so far I haven't come up with a successful attempt.
Thanks!