Given a list of strings, we can use an FBST to track which strings follow which other strings in the list.
Create a function compute-followers that consumes los, and produces a FBST.
Every string in los should appear as a key exactly once in the FBST. The value associated with the key should be
a list of the strings in los that occur immediately after each occurrence of that key.
so, for example, the last string in the list will be the key at the root of the tree.
The strings within each val field can be in any order, but the val fields must not contain any duplicates.
(define-struct fnode (key val left right))
;; An FNode is a (make-fnode Str (listof Str) FBST FBST)
;; A Follower Binary Search Tree (FBST) is one of:
;; * empty
;; * (make-fnode Str (listof Str) FBST FBST)
;; Where:
;; * every key in left is less than key
;; * every key in right is greater than key
this is what i have done so far, but "tree" is not defined in "compute-followers" function...actually i'd like to produce a boolean in (mem? fnode-val (fnode-val tree)) and then i can go back to what i defined in that local function... but i dont know how to improve this... can anyone help me to solve this problem?
(define (insert-bst key val assoc tree)
(cond [(empty? tree)(make-fnode key val empty empty)]
[(string=? key (fnode-key tree))
(make-fnode key val (fnode-left tree)(fnode-right tree))]
[(string<? key (fnode-key tree))
(make-fnode (fnode-key tree)(fnode-val tree)
(insert-bst assoc (fnode-left tree))
(fnode-right tree))]
[else (make-fnode (fnode-key tree)(fnode-val tree)
(fnode-left tree)
(insert-bst assoc (fnode-right tree)))]))
(define (compute-followers los)
(local [(define (mem? val fval-tree)
(cond [(member? val fval-tree) fval-tree]
[else (append fval-tree (list val))]))]
(cond
[(empty? los) empty]
[(empty? (rest los))
(insert-bst (first los) empty (mem? fnode-val (fnode-val tree))
(compute-followers (rest los)))]
[else (insert-bst (first los) (second los)
(mem? fnode-val (fnode-val tree)) (compute-followers (rest los)))])))
Then what should i do in the next step in order to produce like that:
(check-expect (compute-followers (list "b" "a" "d"))
(make-fnode "d" empty (make-fnode "a" (list "d") empty
(make-fnode "b" (list "a") empty empty)) empty))
(check-expect (compute-followers (list "f" "f" "f" "f" "f"))
(make-fnode "f" (list "f") empty empty))