-1

lets say we have a binary tree structure:

(define-struct tree-node (left right node))    

I'm having trouble with creating a binary search tree. My main problem is, I don't know how to insert a node into a tree or to overwrite left or right in make-tree-node structures.
Lets say we are at tree length 8 and I want to insert a new node if left or right is empty. My problem here is how do I go that deep? Is that possible recursively and if yes how do I do that?

can only use the intermidiate level.

Alex Knauth
  • 8,133
  • 2
  • 16
  • 31
bran11
  • 1
  • 4
  • 1
    What code do you have so far? – Mark Mucha Nov 04 '17 at 23:12
  • If you're creating a binary search tree, insertions are based on ordinal comparison of node values – *not* based on tree length, or whether a left/right is empty – Mulan Nov 04 '17 at 23:32
  • @naomik I know that the insertions are based on ordinal comparison of node values but what when all the nodes are "full" and lets say we have the length of 8. Don't we have to compare the node values and depending on that find our way all the way down and create a new leaf/node? Mark, unfortunately I don' have an idea, because of my problems listed above... – bran11 Nov 04 '17 at 23:42

1 Answers1

1

The design recipe from How to Design Programs answers this question. Section 19.5 specifically addresses Binary Search trees. However, the material that will help you most here is probably the stuff in sections 8 and 9. To pull a few threads out for you: First, you need a data definition for a binary search tree. Then, you need to write test cases for a variety of cases, including the base case (inserting into an empty node). Then, you need to follow the template associated with your data definition.

John Clements
  • 16,895
  • 3
  • 37
  • 52
  • How do you insert into an empty node? . The materials/links that you provided me with are dealing with lists not realy with structures and how you insert overwirte an emty node or a field in a structure. – bran11 Nov 05 '17 at 00:20
  • 2
    You don't "overwrite" it; you just return the new one. For instance: how do you insert the number 3 into an empty list? – John Clements Nov 05 '17 at 00:33
  • Thank you! I would do that with (cons 3 '()). For a list it is easy because you have cons and it is "one dimensional". A structure is "n-dimentional".But I have an idea. Please tell me what you think of it. First I want to pick random root. Then I want to write test cases for a variety of cases and call them depending on the cases from (make-tree-node random-node) ( calls a function.) And this way building the tree. What do you think of it? I think local would be useful... – bran11 Nov 06 '17 at 14:21
  • The way you do it for lists is basically just the way you do it for binary search trees. The shape doesn't matter, much. I'm alarmed by your mention of a "random root." A BST has only one root... Let me once again suggest that you create a data definition for a BST, write test cases, and follow the design recipe. – John Clements Nov 06 '17 at 16:37
  • (define example-tree (make-tree-node (make-tree-node empty empty 20) (make-tree-node empty empty 22) 21)) --> This is an example tree. How am I supposed to treat structures like lists? I can't do (cons make-tree node...(meke-tree-node...)) I already read the links that you provided me with but I can't find something that is helpful to me. The only way that I can think of that builds something inside a list is to call a function, which is in the field left and right of a the structure. – bran11 Nov 06 '17 at 17:20
  • 'cons' is exactly like 'make-tree-node'. Each one makes a new node; cons makes a nonempty list, make-tree-node makes a nonempty tree. Are you developing an insertion function? I suggest a data definition for a BST, test cases, and following the design recipe. Sorry to sound like a broken record :). – John Clements Nov 07 '17 at 03:33