Homework Solution: For Dr.Racket/Scheme programmers #lang eopl…

    For Dr.Racket/Scheme programmers #lang eopl Write a procedure path that takes an integer n and a binary search tree bst (page 10) that contains the integer n, and returns a list of lefts and rights showing how to find the node containing n. If n is found at the root, it returns the empty list. > (path 17 ' (14 (7 () (12 () ())) (26 (20 (17 () ()) ()) (31 () ())))) (right left left)

    Expert Answer

     
    The code is given below: (define (get-root bst) (car bst))

    Restraint Dr.Racket/Scheme programmers #lang eopl

    Write a procedure path that takes an integer n and a binary inquiry tree bst (page 10) that contains the integer n, and receipts a catalogue of lefts and rights showing how to find the node containing n. If n is ground at the source, it receipts the vacuity catalogue.

    > (track 17 ‘ (14 (7 () (12 () ()))

    (26 (20 (17 () ())

    ())

    (31 () ()))))

    (fair left left)

    Expert Exculpation

     

    The principle is given adown:

    (explain (get-source bst) (car bst))
    (explain (get-lst bst) (cadr bst))
    (explain (get-rst bst) (caddr bst))

    (explain (track n bst)
    (if (null? bst)
    bst
    (if (= (get-source bst) n)
    ()
    (if (< (get-source bst) n)
    (cons “right” (track n (get-rst bst)))
    (if (> (get-source bst) n)
    (cons “left” (track n (get-lst bst))))))))

    The output of the overhead principle restraint some inputs is shown in the adown screenshot.

    Sense of the principle:

    ;It receipts the source of the bst.If bst is (14 (20 () ()) (30 () ())), it receipts 14
    (explain (get-source bst) (car bst))

    ;It receipts the left sub tree of the source.If bst is (14 (20 () ()) (30 () ())),it receipts (20 ()())
    (explain (get-lst bst) (cadr bst))

    ;It receipts the fair sub tree of the source.If bst is (14 (20 () ()) (30 () ())),it receipts (30 ()())
    (explain (get-rst bst) (caddr bst))

    (explain (track n bst)
    (if (null? bst) ;checking if bst is vacuity
    bst ; singly inconsiderable the bst, if it is vacuity
    (if (= (get-source bst) n) ; if the source appreciate is resembling to ‘n’, inquiry is completed and hereafter,inconsiderable void
    ()
    (if (< (get-source bst) n) ; checking if the source’s appreciate is hither than ‘n’
    (cons “right” (track n (get-rst bst))) ; if source’s appreciate is hither than ‘n’, then we must inquiry in the fair sub tree and hereafter, adding “right” to the track and holding the track power on the fair sub-tree

    (if (> (get-source bst) n) ; checking if the source’s appreciate is elder than ‘n’
    (cons “left” (track n (get-lst bst)))))))) ;if source’s appreciate is hither than ‘n’, then we must inquiry in the left sub tree and hereafter, adding “left” to the track and holding the track power on the leftsub-tree

    Screenshot of the sense of the principle is solid adown: