Binary tree inorder traversal Racket - algorithm

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

Related

Taking a name and adding it to a binary tree structure

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

Confused about binary search trees (Scheme)

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

Tree sorting function in Scheme

Im struggling with this problem for over 2 days now and still somehow I cannot solve it.
I have to write a function in SCHEME that takes a list in a tree and displays items in sorted order.
The way I define trees is '(6 (left... ) (right...))
My function to choose a tree:
(define (tree-sort tree)
(cond ((null? tree) '())
((> (car tree) (cadr tree))
(tree-sort (cadr tree)))
(else
(tree-sort (caddr tree))))
)
So I guess I should also have a function that sorts the most indepth list?
I really dont get it and this is the last time I will ever have to deal with scheme. I have never used stackoverflow so please excuse me if the formating is wrong.
Kindly thank you!
Now that you clarified that the tree is already sorted, then you're looking for an in-order traversal of the tree, which returns a sorted list of the elements - I'm assuming that you're interested in a list as the output, because of the base case shown in the question. Try something like this:
(define (tree-sort tree)
(if (empty-tree? tree)
'()
(append (tree-sort (left-subtree tree))
(list (value tree))
(tree-sort (right-subtree tree)))))
Use the appropriate procedures for testing if the tree is empty and for accessing each node's value, left and right subtrees. The above procedure will return a sorted list with the trees' values.

Binary trees in scheme

How would I count how many nodes a tree has?
;;; A Binary is one of:
;;; - Number
;;; - (make-node BT BT)
(define-struct node (left right))
(define tree1 (make-node (make-node 10 9)
(make-node 3
(make-node 1 5))))
(define (how-many? nd)
(cond
[(number? nd)....]
[(node? n)
(..... (how-many? (node-left nd))
(how-many? (node-right nd)))]))
so for tree1 I should get
(check-expect (how-many? tree1) 5)
I think I got the template right. If it's a number, you should return 1. But if it's a node, what type of function should I put in the dotted lines?
This is somewhat similar to how you'd process a list. The difference? that now you'll go to the "left" and to the "right" of each node, instead of going just to the "rest". Other than that, the same ideas apply:
(define (how-many? nd)
(cond
[(number? nd) <???>] ; if it's a leaf, then how many numbers does it have?
[(node? nd)
(<???> (how-many? (node-left nd)) ; how do we combine the results?
(how-many? (node-right nd)))]))
You got the base case right, as I can see in the question. The recursive case is simple! remember, how did we combine the results when we were adding the elements in a list? here's the same thing, it's just that we combine the results from the left and from the right subtrees.

Finding all paths in a directed graph with cycles

I am working on a problem which requires finding all paths between two nodes in a directed graph. The graph may have cycles.
Notice that this particular implementation approach is an iterative DFS.
Several approaches I've considered are as follows -
BFS does not appear to have a way to neatly manage this kind of pathing relationships between nodes.
I don't see an easy mechanism for a DFS recursive algorithm to pass up the path when the terminating node is found. (Likely enough it could be done, if I implement a Maybe monad kind of thing).
Creating a GRAPH-PARENT routine. That would add a decent amount of churn (& bugs) in the existing code.
Abstractly, what needs to happen is a tree needs to be generated, with the start node as root, and all leafs are the terminating nodes. Each path from leaf to root is a legal path. That is what a recursive DFS would trace out.
I'm reasonably sure it can be done here, but I don't see exactly how to do it.
I've defined a protocol for this algorithm where GRAPH-EQUAL and GRAPH-NEXT can be defined for arbitrary objects.
The debug node type is a SEARCH-NODE, and it has the data accessor SEARCH-NODE-DATA.
(defun all-paths (start end)
(let ((stack (list start))
(mark-list (list start)) ;I've chosen to hold marking information local to all-paths, instead of marking the objects themselves.
(all-path-list '())) ; Not used yet, using debug statements to think about the problem
(do () ;; intializing no variables
;; While Stack still has elements
((not stack))
(let ((item (pop stack)))
;; I'm looking at the item.
(format t "I: ~a~%" (search-node-data item))
(cond ((graph-equal item end)
(format t "*Q: ~a~%" (loop for var in stack collect (search-node-data var)))
;;Unmark the terminal node so we can view it it next time.
(setf mark-list (remove item mark-list))))
(loop for next in (graph-next item)
do
(cond ((not (in next mark-list :test #'graph-equal))
;; mark the node
(push next mark-list)
;;Put it on the stack
(push next stack))))))))
See A Very General Method for Computing Shortest Paths for an algorithm that can return all paths in a graph (even when there are cycles) as regular expressions over the alphabet of edges in finite time (assuming a finite graph).
You need to pass the path list (mark-list) along with the nodes, since that is part of the state. I've renamed it path in this code:
(defun all-paths (start end)
(let ((stack (list '(start (start)))) ; list of (node path) elements
(all-path-list '()))
(do ()
((not stack))
(let ((item (pop stack)))
(let ((node (first item))
(path (second item)))
(format t "I: ~a~%" (search-node-data node))
(cond ((graph-equal node end)
(format t "*Q: ~a~%"
(loop for var in path
collect (search-node-data var)))))
(loop for next in (graph-next node)
do
(cond ((not (in next path :test #'graph-equal))
(push (list next (cons next path)) stack)))))))))

Resources