I can’t seem to figure out how to delete an element from the BST. This is my code
(define remove (lambda (x t)
(if (< x (car t)) (list (car t) (remove x (cadr t)) (caddr t))
(if (> x (car t)) (list (car t) (cadr t) (remove x (caddr t)))
(if (not(and (null? (cadr t)) (null? (caddr t))))
(let ((r (minimum (caddr t)))) ((remove r t) (set-car! t r)))
(list '() (cadr t) (caddr t)))))))
Minimum returns the minimum value in the tree.
If I try to delete an element that’s not a leaf, it goes into an infinite loop. How can I fix it?
I think your biggest problem lies in this section:
You are removing
rfromt, but you should really be removingrfrom the right subtree oft. I’m not sure why you are using mutability/setting, either; I think it complicates things in what could easily be a non-side effecting function. I would try something like:I also have to admit that I’m a bit confused about your intent with the last line. What are you using the empty list for?