I'm new in Racket language and I bumped into an issue. I'm now trying to implement Binary Search Tree using cons(list).
This is a simple BST I tried to make:
For this BST, if I convert this into Racket list, this can be one possibility: '(6, (4, (), ()), (7, (), ())
To produce this format of list, I used this code:
(define x3 (cons 6 (cons (cons 4 (cons null (cons null null)))
(cons 7 (cons null (cons null null)))
And for this code, the result is below:
'(6 (4 () ()) 7 () ())
As you see, this is not what I wanted. The second child node 7 () () is not inside the bracket, which means it doesn't exist as an list. It acts as individual 3 elements, not single list. So I changed a bit:
(define x2 (cons 6 (cons (cons (cons 4 (cons null (cons null null))) null)
(cons (cons 7 (cons null (cons null null))) null)
)
)
)
And for this 2nd code, the result is as following:
'(6 ((4 () ())) (7 () ()))
And this is not what I wanted either. The first child node ((4 () ())) now is inside and inside the list. So I did the final try, and the code is like this:
(define x3 (cons 6 (cons (cons 4 (cons null (cons null null)))
(cons (cons 7 (cons null (cons null null))) null)
)
)
)
And the result looks like this:
'(6 (4 () ()) (7 () ()))
Now it works! The question is, why these things happen? I kind of realized that the rule is different between the last element of list and the other when cons is used, but after all, aren't they same kind of list?