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 pursuit tree bst (page 10) that contains the integer n, and produce a inventory of lefts and rights showing how to find the node containing n. If n is fix at the rise, it produce the space inventory.

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

    (26 (20 (17 () ())

    ())

    (31 () ()))))

    (correct left left)

    Expert Reply

     

    The mode is consecrated under:

    (mark-out (get-rise bst) (car bst))
    (mark-out (get-lst bst) (cadr bst))
    (mark-out (get-rst bst) (caddr bst))

    (mark-out (route n bst)
    (if (null? bst)
    bst
    (if (= (get-rise bst) n)
    ()
    (if (< (get-rise bst) n)
    (cons “right” (route n (get-rst bst)))
    (if (> (get-rise bst) n)
    (cons “left” (route n (get-lst bst))))))))

    The output of the overhead mode restraint some inputs is shown in the under screenshot.

    Sense of the mode:

    ;It produce the rise of the bst.If bst is (14 (20 () ()) (30 () ())), it produce 14
    (mark-out (get-rise bst) (car bst))

    ;It produce the left sub tree of the rise.If bst is (14 (20 () ()) (30 () ())),it produce (20 ()())
    (mark-out (get-lst bst) (cadr bst))

    ;It produce the correct sub tree of the rise.If bst is (14 (20 () ()) (30 () ())),it produce (30 ()())
    (mark-out (get-rst bst) (caddr bst))

    (mark-out (route n bst)
    (if (null? bst) ;checking if bst is space
    bst ; singly subordinate the bst, if it is space
    (if (= (get-rise bst) n) ; if the rise prize is correspondent to ‘n’, pursuit is completed and future,subordinate ineffectual
    ()
    (if (< (get-rise bst) n) ; checking if the rise’s prize is hither than ‘n’
    (cons “right” (route n (get-rst bst))) ; if rise’s prize is hither than ‘n’, then we must pursuit in the correct sub tree and future, adding “right” to the route and tenure the route capacity on the correct sub-tree

    (if (> (get-rise bst) n) ; checking if the rise’s prize is senior than ‘n’
    (cons “left” (route n (get-lst bst)))))))) ;if rise’s prize is hither than ‘n’, then we must pursuit in the left sub tree and future, adding “left” to the route and tenure the route capacity on the leftsub-tree

    Screenshot of the sense of the mode is fast under: