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))

    Ce Dr.Racket/Scheme programmers #lang eopl

    Write a procedure path that takes an integer n and a binary exploration tree bst (page 10) that contains the integer n, and produce a roll of lefts and rights showing how to find the node containing n. If n is establish at the commencement, it produce the emptiness roll.

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

    (26 (20 (17 () ())

    ())

    (31 () ()))))

    (proper left left)

    Expert Exculpation

     

    The command is given adown:

    (designate (get-commencement bst) (car bst))
    (designate (get-lst bst) (cadr bst))
    (designate (get-rst bst) (caddr bst))

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

    The output of the over command ce some inputs is shown in the adown screenshot.

    Interpretation of the command:

    ;It produce the commencement of the bst.If bst is (14 (20 () ()) (30 () ())), it produce 14
    (designate (get-commencement bst) (car bst))

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

    ;It produce the proper sub tree of the commencement.If bst is (14 (20 () ()) (30 () ())),it produce (30 ()())
    (designate (get-rst bst) (caddr bst))

    (designate (track n bst)
    (if (null? bst) ;checking if bst is emptiness
    bst ; solely rebated the bst, if it is emptiness
    (if (= (get-commencement bst) n) ; if the commencement appraise is correspondent to ‘n’, exploration is completed and hereafter,rebated inoperative
    ()
    (if (< (get-commencement bst) n) ; checking if the commencement’s appraise is hither than ‘n’
    (cons “right” (track n (get-rst bst))) ; if commencement’s appraise is hither than ‘n’, then we must exploration in the proper sub tree and hereafter, adding “right” to the track and pursuit the track office on the proper sub-tree

    (if (> (get-commencement bst) n) ; checking if the commencement’s appraise is main than ‘n’
    (cons “left” (track n (get-lst bst)))))))) ;if commencement’s appraise is hither than ‘n’, then we must exploration in the left sub tree and hereafter, adding “left” to the track and pursuit the track office on the leftsub-tree

    Screenshot of the interpretation of the command is determined adown: