简体   繁体   English

二叉搜索树方案

[英]Binary Search Tree Scheme

My friend and I are currently working on creating a Binary Search Tree in Scheme. 我和我的朋友当前正在在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. (cdr(对于左子树,以某种方式,但是我不知道确切地放在哪里。我们应该使用set-car!(cddr(对于右子树。

We have done all of this so far correctly, but we just need help making it save our inserted nodes. 到目前为止,我们已经正确地完成了所有这些操作,但是我们只需要帮助使其保存我们插入的节点即可。

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))
    ((< item (getRoot BST)) ;if item less than root, add to left subtree
     (make-BST (getRoot BST)
               (BST-insert (getLeftsubTree BST) item) ;recursion
               (getRightsubTree BST)))                                     
    ((> item (getRoot BST))                                         
     (make-BST (getRoot BST)
           (getLeftsubTree BST)
           (BST-insert (getRightsubTree BST) item)))
    (else BST)))  ; it's already in BST, do nothing

Since this sounds like an assignment, I don't want to provide the exact code that you need, but I'll show two functions that could be said to replace the nth element of list. 由于这听起来像是一项任务,所以我不想提供所需的确切代码,但是我将展示两个可以用来代替list的第n个元素的函数。 The first will be analogous to yours, in that it returns a new list and doesn't modify the input list. 第一个与您的类似,因为它返回一个新列表,并且不修改输入列表。 The second will modify the input list. 第二个将修改输入列表。

(define (replace-nth n element list)
  ;; return a new list like list, but whose 
  ;; nth element is element
  (if (= n 0)
      (cons element (cdr list))
      (cons (car list) (replace-nth (- n 1) element (cdr list)))))

(let ((l (list 1 2 3 4 5 6)))
  (display (replace-nth 3 'x l)) ; prints (1 2 3 x 5 6)
  (display l))                   ; prints (1 2 3 4 5 6)

The first returns a new list, but doesn't modify the input list. 第一个返回一个新列表,但不修改输入列表。 It creates a new list using cons applied to part of the old list and some new value. 它使用应用于部分旧列表的cons和一些新值来创建新列表。 This is similar to what you're doing when you insert by creating a new tree that has the new value. 这类似于您通过创建具有新值的新树进行插入时所执行的操作。 The tree that you've passed in won't have it, but the tree will. 您传入的树没有它,但是树会。

(define (set-nth! n element list)
  ;; set the nth element of list to be element
  (if (= n 0)
     (set-car! list element)
     (set-nth! (- n 1) element (cdr list))))

(let ((l (list 1 2 3 4 5 6)))
  (display (set-nth! 4 'x l)) ; prints #<void>
  (display l))                ; prints (1 2 3 4 x 6)

The second modifies the list that is passed to it. 第二个修改传递给它的列表。 Its return value isn't quite so important, because the structure passed into it actually gets modified. 它的返回值不是很重要,因为传递给它的结构实际上已被修改。 This is more like what you'd want to do with your insert function. 这更像是您要对插入函数执行的操作。 You need to recurse until you get to the correct node in the tree, and the set its left or right child to be a new tree containing just the new element. 您需要递归直到到达树中的正确节点,并将其左或右子级设置为仅包含新元素的新树。

You've provided 'get' procedures, why not start by providing a 'set' procedure? 您已经提供了“获取”过程,为什么不从提供“设置”过程开始呢? Doing so works towards completing your 'tree/node abstraction' and gives you a base set of functions to try to use in your 'insert' procedure. 这样做有助于完成“树/节点抽象”,并为您提供了一组基本功能,可尝试在“插入”过程中使用。

(define (setLeftsubTree BST left) 
  (set-car! (cdr BST) left))

(define (setRightsubTree BST right) 
  (set-car! (cddr BST) right))

Now, in your insert code, when you want to 'go left' but there is no left, call setLeftsubTree with a newly created leaf node. 现在,在您的insert代码中,当您想“左移”但没有左移时,请使用新创建的叶节点调用setLeftsubTree。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM