Reputation: 741
Hi I am new to Racket using it for a binary tree structure.
Using the following structure
(define-struct human(age hight))
I have created the following object/variable/human
(define James(make-human 10 50))
If I have a node in a binary tree structure
(define-struct node (left human right))
How can I compare a different object's hight (say Michael) with James given that James is within the node, so for an example:
(define (insert-human-into-tree human node)
(cond
[(empty? node)(make-node empty human empty)]
[(<= human-hight( **node-human-hight**))
I need to know how to access the hight field of the human object which will be within the node ( node-human-hight).
Upvotes: 1
Views: 91
Reputation: 70275
Your insert-human-into-tree
is going to need a bit more beyond the comparison to actually insert a new node. It will look something like this (which also answers your prime question):
(define (insert-human-into-tree human node)
(if (empty? node)
(make-node empty human empty)
(let ((other (node-human node)))
(cond ((< (human-height other) (human-height human))
(insert-human-into-tree human (node-left node))) ; go left
((> (human-height other) (human-height human))
(insert-human-into-tree human (node-right node))) ; go right
(else ;; same height
...)))))
This will get you started.
Upvotes: 1
Reputation: 236170
Use the accessor procedures in the struct, for example:
(define-struct human(age hight))
(define james (make-human 10 50))
(define-struct node (left human right))
(define anode (make-node null james null))
; access the human in the node, and then the height of that human
(human-hight (node-human anode))
=> 50
... And it's spelled "height", not "hight". So, to answer the question, the comparison would look like this:
(<= (human-hight (node-human node)) (human-hight human))
Upvotes: 3