Can anyone help me with my base cases for the rotation of a binary search tree left and right? I tried writing the right rotation function as:
(define (right-rotate T)
(make-tree (car (left T))
(left (left T))
(make-tree(value T) (right (left T)) (right T))))
but this gives me a call to an empty list somewhere. Does this code look correct for a right rotation? Also, what could my base cases be here?
You really need to provide more information, such as what is your representation of a 'tree' and how is a tree missing its 'left' or 'right' child defined and handled.
(define (make-tree value left right)
`(TREE ,value ,left ,right))
(define value cadr)
(define right caddr)
(define left cadddr)
;; How is an 'empty' tree denoted?
(define empty 'EMPTY)
(define (is-empty? tree)
(eq? tree empty))
(define (make-leaf value)
(make-tree value empty empty))
;; Now you have the basis for a solution; here is a start.
(define (right-rotate tree)
(if (is-empty? tree)
empty
(let ((l (left tree)))
(if (is-empty? l)
<something>
(make-tree (value l)
(left l)
(make-tree (value tree) (right l) (right tree)))))))
Related
I've just started learning about trees and heaps and I'm unsure about how to go about writing the test case. These codes are from my lesson slides. Although they give codes, they sadly don't provide test cases of said codes, so I am confused on how I would call it.
I've tried test cases such as any regular integers like 5, and I've also tried going about it with lists, but I run into errors and it just doesn't seem right as I know from diagrams that heaps are like trees with its roots being the smallest value and with its subheaps.
(define (value H)
(car H))
(define (weight H)
(cdr H))
(define (create-heap vw-pair left-child right-child)
(list vw-pair left-child right-child))
(define (h-min heap)
(car heap))
(define (left heap)
(cadr heap))
(define (right heap)
(caddr heap))
(define (insert vw-pair heap)
(cond ((null? heap) (create-heap vw-pair '() '()))
((< (weight vw-pair) (weight (h-min heap)))
(create-heap vw-pair (right heap) (insert (h-min heap) (left heap))))
(else
(create-heap (h-min heap) (right heap) (insert vw-pair (left heap))))))
(define (insert-list-of-pairs vw-pair-list heap)
(if (null? vw-pair-list)
heap
(insert-list-of-pairs (cdr vw-pair-list) (insert (car vw-pair-list) heap))))
(define (remove-min heap)
(define (combine-heaps h1 h2)
(cond ((null? h1) h2)
((null? h2) h1)
((< (cdr (h-min h1)) (cdr (h-min h2)))
(create-heap (h-min h1) h2 (combine-heaps (left h1) (right h1))))
(else
(create-heap (h-min h2)
h1
(combine-heaps (left h2) (right h2))))))
(combine-heaps (left heap) (right heap)))
Your test cases should explain exactly what you want to do.
They are the way to explain, using code, the intended use for the functions you write.
For your specific case, I obviously can't help you because that's exactly what's missing from your code: the meaning it should have.
But I can still explain how to write unit tests in Racket:
;; This is a function you would write.
;; It does something, but it's not completely obvious
;; how to use it.
(define (find type basket)
(let ([obj (assq type basket)])
(and obj
(cadr obj))))
;; By creating a test module, you add code that describes
;; how to use the functions in this file.
(module+ test
(require rackunit)
;; This is some sample data.
;; It's useful to understand what kind of data
;; your functions are expected to process.
(define basket '((bread baguette)
(fruit ananas)
(fruit banana)
(vegetable carrot)
(misc fork&knife)))
;; Here we call the function and show the expected result.
;; It's now clear how to use it.
(check-equal? (find 'fruit basket) 'ananas)
(check-equal? (find 'vegetable basket) 'carrot)
(check-false (find 'fruit '()))
)
You can run those tests by using raco:
> raco test myfile.rkt
raco test: (submod "myfile.rkt" test)
3 tests passed
i'm attempting to count duplicate in a tree. i'm attaching a picture for a better illustration. I'm on the wrong track i have no where to go.
Here is what i did
(define (arbre-insere valeur arbre)
(cond ((null? arbre) (list (cons valeur 1) '() '()))
((< valeur(car arbre))
(list (cons (car arbre) count)
(arbre-insere valeur (cadr arbre))
(caddr arbre)))
(> valeur(car arbre) (list cons ((car arbre) count) (cadr arbre)
(arbre-insere valeur (caddr arbre) )))
(else
)
))][1]
Here is a sketch, where ... and stuff in <...> is meant to be filled out by you.
(define leaf '())
; leaf? : tree -> boolean
; return #t if the tree is a leaf,
; #f otherwise
(define (leaf? tree)
(null? leaf?))
; value : tree -> element
; return the root element of the tree
(define (value tree)
...)
; count : tree -> integer
; return the count of the root element of tree
(define (count tree)
...)
; left : tree -> tree
; return the left subtree of tree
(define (left tree)
...)
; right : tree -> tree
; return the right subtree of tree
(define (right tree)
...)
; make-node : value integer tree tree
; construct tree from a value and count,
; left is a tree whose elements are smaller than value
; right is a tree whose elements are greater than value
(define (make-node value count left right)
(list left (cons value count) right))
; tree-insert : value tree -> tree
(define (tree-insert v t)
(cond
[(leaf? t) (make-tree v 1 leaf leaf)]
[(= v (value t)) (make-tree v <old-count+1> (left t) (right t))]
[(< v (value t)) (make-tree v (make-node (value t) (count t)
(insert-tree v (left t)) r))]
[(> v (value t)) <???>]
[else (error 'tree-insert "an unexpected error occurred")]))
;; A [BT X] is one of
;; - 'leaf
;; - (make-node X [BT X] [BT X])
(define-struct node (val left right))
;; interpretation: represents a node with a value
;; a left and right subtree
(define (tree-sum tree)
(cond
[(symbol=? tree 'leaf) ...] ;; the value remains same
[(node? tree)
(+ (tree-sum (node-val tree))
(tree-sum (node-left tree))
(tree-sum (node-right tree)))]))
not quite sure if i'm on the right track.
Normally, A pair tree is either
a leaf (not a pair), or
a pair, whose car and cdr values are pair-trees.
recursive summing procedure will have to deal with these two cases:
a numbers, i.e., leaves of a tree of numbers, and
pairs, in which case it should sum the left and right subtrees, and add those sums together.
Therefore,
(define (pair-tree-sum pair-tree)
(cond
[(number? pair-tree)
pair-tree]
[else
(+ (pair-tree-sum (car pair-tree))
(pair-tree-sum (cdr pair-tree)))]))
You can split the function into two parts - one that converts the tree into a list of numbers, and another that just uses foldl with that list, and foldl has the advantage of automatically doing the summation as an accumulator.
(define (tree->list tree)
(if (pair? tree)
(append (tree->list (car tree))
(tree->list (cdr tree)))
(list tree)))
(define (tree-sum tree)
(foldl + 0 (tree->list tree)))
You first need to check if the tree is empty, then if its not a list, else you call the function sum to the car of the list, and add to the cdr of the list.
(define (sum tree)
(cond [(empty? tree) 0]
[(not (list? tree)) (if (number? tree) tree 0)]
[else (+ (sum (first tree)) (sum (rest tree)))]))
You are provided with the following definition for binary tries
(define-struct leaf ())
;; interpretation: represents a leaf on a BT, a node with no children
(define-struct node (word left right))
;; interpretation: represents a node on a BT with a word, a left and a right
;; subtree
;; A BinaryTree (BT) is one of
;; - (make-leaf)
;; - (make-node String BT BT)
Design the program bt->los that consumes a BT tree and returns the elements of the tree as a list of strings. At each node your function should
1.process the left sub tree
2.process the word at this node
3.process the right sub tree
;; bt->los: tree -> los
;; consumes a BT tree and returns the elements of the tree as a list of strings.
(define (bt->los tree)
(cond
[(leaf? tree) ""]
[(node? tree)
(append (bt->los(list (node-left tree)))
(list (node-word tree))
(bt->los (list (node-right tree))))]))
I'm stuck here. Should have missed something. Don't we need recursion here?
Output should be like,
(define bt-abc (make-node "b"
(make-node "a" (make-leaf) (make-leaf))
(make-node "c" (make-leaf) (make-leaf))))
(bt->los bt-abc) (cons "a" (cons "b" (cons "c" empty)))
You are very close here. Just a couple of mistakes.
(define (bt->los tree)
(cond
[(leaf? tree) empty]
[(node? tree)
(append (bt->los (node-left tree))
(list (node-word tree))
(bt->los (node-right tree)))]))
First, you are constructing a list of string. Because it is a list, your base case should be empty. not "". Second, each node, already represents a BT, there is no need to list it. And bt->los returns
a list. With this simple changes, it works as expected for you test case.
I have a problem that I need to complete:
We can represent a nonempty binary tree by list (root left_subtree
right_subtree) and the empty tree by the empty list. Each binary
search tree with integer labels can be considered representing a set
of integers. Write a function which, given a set of integers S as a
bst and an integer x, returns both the set of all the integers less
than x and that of all the integers greater than x as bst’s. Use a
pair rather than a list to represent the resulting sets.
I'm very new to Scheme. I've built trees using SML as well as prolog, but can't seem to get a hold of what I need to do for Scheme. Could anyone help me out and guide me towards this goal? Is my tree suppose to just look like this?
(list value left right)
Yes, that's one way to define a tree. Just so long as you make a proper ADT and use it consistently, it shouldn't matter after you've defined it.
(define (make-tree root left right)
(list root left right))
(define (right tree)
(caddr tree))
(define (left tree)
(cadr tree))
(define (root tree)
(car tree))
(define (empty-tree? tree)
(null? tree))
(define empty-tree '())
(define (leaf? tree)
(and (empty-tree? (left tree)) (empty-tree? right tree)))
(define (split-tree-at tree x)
(let ((less ...)
(more ...))
(cons less more))
(define (in-tree-below-x tree x) ;;assuming a sorted tree
(cond ((empty-tree? tree) (empty-tree))
((>= (root tree) x)
(in-tree-below (left tree) x))
(else (make-tree (root tree)
(left tree)
(in-tree-below-x (right tree) x)))))