I need help in racket pl
Using this BINTREE definition, write a (higher-order) function called treemap
that takes in a numeric function f and a binary tree, and returns a
tree with the same shape but using f(n) for values in its leaves. For
example, here is a test case:
(test (tree-map add1 (Node (Leaf 1) (Node (Leaf 2)
(Leaf 3))))
=> (Node (Leaf 2) (Node (Leaf 3) (Leaf 4))))
What I did is:
[Node BINTREE BINTREE]
[Leaf Number])
>
>
(define(treemap f tree)
[(Leaf Number) f(Number)]
[(Node tree BTREE) treemap(Node)]))
Replace
[(Leaf Number) f(Number)]
with
[(Leaf Number) (Leaf f(number))]
and the Node will return a node and active the functions on its arguments.
Related
Please disregard my broken English as i’m not a native speaker.
I’m looking for the best way to find the kth smallest element in a BST, I’ve thought of ways like appending the tree to a list and traverse that list, but that takes too much time O(n)
I’ve also considered deleting elements from the tree then find the smallest but that also takes more time.
What is the best algorithm to approach this problem?
As scheme is a functional programming language, the solution must be recursive. I’ve tried to look for an answer but most of the answers in C or Java would use some kind of an iterative format.
Thank you for your help,
My function should look like
(Define (kth-smallest T k)
...)
If the BST is ordered and balanced, such that the leaf not most left is the lowest value. Then the nth lowest value would be the nth value you iterate over in an in order tree traversal.
So finding the lowest kth value in a balanced tree is between O(log n) and O(2n). O(n) it is then.
My implementation would create a helper that takes a continuation as well as k and the tree. The default continuation can be (lambda (v) #f) and thus if you want the 30th smallest in a 10 node tree it will call that and you get #f back. The second you find the deepest node and it's k is zero instead of calling the continuation it evaluates to the value of the current node.
If you were to delete the lowest value k times you'd have O(k * log n) ~ O(n log n) > O(n)
Good luck
If the size of the left subtree is larger than k, we look in the left sub-tree, if it is smaller we look at the right subtree with the new k: k - size-of-left-subtree - 1, otherwise (in the equal case) we return the value at the current node. This is performed in O(lg n) time.
#lang racket
(struct no-info ())
(define NONE (no-info))
; [BST X] is one of:
; - (node NONE NONE NONE 0)
; - (node BST BST X Number)
(struct node [left right val size])
(define leaf (node NONE NONE NONE 0))
; examples
(define lt (node (node (node leaf leaf 10 1) (node leaf leaf 24 1) 15 3) leaf 29 4))
(define rt (node (node leaf leaf 77 1) (node leaf (node leaf leaf 99 1) 95 2) 89 4))
(define t (node lt rt 63 9))
; 63
; / \
; 29 89
; / / \
; 15 77 95
; / \ \
; 10 24 99
; node val: 10 15 24 29 63 77 89 95 99
; rank: 1 2 3 4 5 6 7 8 9
; kth-smallest : BST -> X
; the value of the `k`th smallest node in `t`
(define (kth-smallest t k)
(define (kth-smallest-h t k)
(cond [(equal? (node-size t) 1) (node-val t)]
[else (let ([s (node-size (node-left t))])
(cond [(> s k) (kth-smallest-h (node-left t) k)]
[(< s k) (kth-smallest-h (node-right t) (sub1 (- k s)))]
[else (node-val t)]))]))
(if (or (<= k 0) (> k (node-size t)))
(error "k out of range")
(kth-smallest-h t (sub1 k))))
(map (λ (x) (kth-smallest t x)) '(1 2 3 4 5 6 7 8 9))
; => '(10 15 24 29 63 77 89 95 99)
A node in this tree is a list of three items (name left right), where name is a string, and left and right are the child trees i feel like i have gotten off track is there an easy way to write this with just (define(insert name left right))
(define tree
(lambda (node word)
(cond
((null? node) (make-tree word))
((string=? word (tree-word node))
(set-tree-count! node (+ (tree-count node) 1))
node)
((string<? word (tree-word node))
(set-tree-left! node (tree (tree-left node) word))
node)
(else
(set-tree-right! node (tree (tree-right node) word))
node))))
There's no need to use mutation operations, in general in Scheme we avoid them, in this case in particular is easy (and recommended) to build a new tree as we go. And, why the count? this problem has nothing to do with adding numbers. Also notice that the definition (insert name left right) doesn't make much sense, we want to insert a word starting from a tree's root node; left and right aren't useful as parameters. Let's start again from scratch.
(define (insert node word)
(cond ((null? node) (make-tree word '() '()))
((string=? word (tree-word node)) node)
((string>=? word (tree-word node))
(make-tree (tree-word node)
(tree-left node)
(insert (tree-right node) word)))
(else
(make-tree (tree-word node)
(insert (tree-left node) word)
(tree-right node)))))
I am trying to write the algorithm for inorder traversal for a binary tree using RACKET/DR. RACKET
(define (print-records node number)
(cond
[(not (empty? node-left))(print-records (node-left node) number )]
*Do This before moving to next IF clause*
[(not (empty? node-right))(print-records(node-right node) number)]
))
I am trying to follow the following algorithm
InOrder(node)
if node is null return
InOrder(node.left)
Print(node)
InOrder(node.Right)
My problem is that through COND I can execute one expression and it will skip the rest. I tried to add two expressions under one it did not work either e.g ((a)(b)). I also tried to make a helper procedure but that did not work either.
You're using cond in a wrong way. Notice that you have to recursively traverse the left part of the tree, then visit the current node and then recursively traverse the right part of the tree - they're not mutually exclusive alternatives, the three steps need to be performed in precisely that order. Try something like this instead:
(define (print-records node number)
(unless (empty? (node-left node))
(print-records (node-left node) number))
(print (node-value node)) ; replace this line with the actual printing code
(unless (empty? (node-right node))
(print-records (node-right node) number)))
Some explanations:
(unless <condition> <body>) is just shorthand for (cond ((not <condition>) <body>)).
The real work of the traversal is done between the two recursive calls, in this case I wrote (print (node-value node)) as an example, replace that line with the actual printing code for the current node's value.
It's not clear what do you intend to do with the number parameter, as it is it's just being passed around, unused.
Walking a binary-tree is a very general operation. You can make a general procedure and then specialize it with the function to apply to each node.
(define (walker node function)
(unless (empty? node)
(walker (node-left node) function)
(function node)
(walker (node-right node) function)))
Note: it is nice to check for empty? at the beginning of the function.
(define (print-records node number)
(walker node (compose print node-value))) ; ignore number, it seems.
You could also work this as:
(define (walking-with function)
(letrec ((walker (lambda (node)
(unless (empty? node)
(walker (node-left node))
(function node)
(walker (node-right node))))))
walker))
(define print-records-for (walking-with (compose print node-value)))
(print-records-for node)
> ...
Write n analogous function (maptree f t) that returns a tree made by applying the function f to each entry of the binary tree t. (Just like map) Since the tree is a data abstraction, only the followings are allowed to operate on trees:(entry t) (right-branch t) (left-branch t) (make=tree entry left right), and (empty-tree? t). You may use the predefined constant the-empty-tree.
Example:
(define tree
(make-tree 10 (make-tree 5 the-empty-tree the-empty-tree)
(make-tree 12 the-empty-tree the-empty-tree)))
tree
(10 (5 () ())(12 () ()))
(maptree square tree)
(100 (25 () ())(144 () ()))
Like this:
(define (maptree func tree)
(if (empty-tree? tree)
the-empty-tree
(make-tree (func (entry tree))
(maptree func (left-branch tree))
(maptree func (right-branch tree)))))
When a data structure is recursively defined, then a function's implementation may be naturally recursive. In this case, perform the function on one tree item and then recurse on the left and right subtrees.
This seems like a homework question. A good starting point would be to make your way down the tree recursively while rebuilding the tree. Check to see if the left and right are empty, and if not call your map function again on each branch.
Start at the top (the first entry) and make a new tree, with the function f applied to entry and a check if the left and right branches are the-empty-tree or not. If not, call the function again on each branch.
I am having trouble with binary search trees and turning them into lists.
(define-struct node (key val left right))
;; A binary search tree (bst) is either
;; empty, or
;; a structure (make-node k v l r), where
;; k is a number (the key),
;; v is a string (the value),
;; l is a bst, where every key in l is less than k, and
;; r is a bst, where every key in r is greater than k.
Can anybody give me hints on how to approach this question?
Create a function bst that consumes a binary search tree and returns a list of all the strings in the value field of the binary search tree nodes and the list must be in descending order based on the key values in the binary search tree.
;;Examples: (bst (make-node 4 "James" (make-node 2 "Kien" empty empty)
;;(make-node 5 "Jack" empty (make-node 11 "Cole" empty empty)))) => (list "Cole" "Jack" "James" "Kien")
Thanks!
Basically, you have to visit all the nodes in the tree using a reverse in-orden traversal (right subtree / value / left subtree), while at the same time creating a list with the answer. Something along these lines:
(define (tree->list tree)
(if (empty? tree)
empty
(append (tree->list (node-right tree))
(cons (node-val tree)
(tree->list (node-left tree))))))
It works as expected:
(define bst
(make-node 4 "James"
(make-node 2 "Kien" empty empty)
(make-node 5 "Jack" empty
(make-node 11 "Cole" empty empty))))
(tree->list bst)
=> (list "Cole" "Jack" "James" "Kien")
(define (tree->list tree)
(if(leaf? tree)
(list (node tree))
(cond((right-branch-only? tree)(append (list(node tree))
(tree->list (right-branch tree))))
((left-branch-only? tree)(append (list(node tree))
(tree->list (left-branch tree))))
(else(append (list (node tree))
(append (tree->list (left-branch tree))
(tree->list (right-branch tree))))))))