Note that there are some explanatory texts on larger screens.

plurals
  1. POBinary Search Tree Scheme
    primarykey
    data
    text
    <p>My friend and I are currently working on creating a Binary Search Tree in Scheme. We cannot get it to save what we have inserted. My professor said we are to use set-car! (cdr ( for the left subtree somehow, but I don't know where exactly to put it in. We are supposed to use set-car! (cddr ( for the right subtree also. </p> <p>We have done all of this so far correctly, but we just need help making it save our inserted nodes. </p> <p>Code: </p> <pre><code>(define make-empty-BST '()) ;create function to see if BST is empty (define (emptyBST? BST) (null? BST)) ;make non-empty BST with explicit list implementation (define (make-BST root left-subtree right-subtree) (list root left-subtree right-subtree)) ;helper get root function (define (getRoot BST) (car BST)) ;helper get left subtree function (define (getLeftsubTree BST) (cadr BST)) ;(car (cdr tr)) ;helper get right subtree function (define (getRightsubTree BST) (caddr BST)) ;(car (cdr (cdr tr))) ;Checks to see if a leaf is empty (define (emptyleaf? BST) (and (null? (getLeftsubTree BST)) (null? (getRightsubTree BST)))) ;inserts an item into the BST (define (BST-insert BST item) (cond ((emptyBST? BST) ;if empty, create a new root with given item - use empty lists for left and right subtrees (make-BST item make-empty-BST make-empty-BST)) ((&lt; item (getRoot BST)) ;if item less than root, add to left subtree (make-BST (getRoot BST) (BST-insert (getLeftsubTree BST) item) ;recursion (getRightsubTree BST))) ((&gt; item (getRoot BST)) (make-BST (getRoot BST) (getLeftsubTree BST) (BST-insert (getRightsubTree BST) item))) (else BST))) ; it's already in BST, do nothing </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload