简体   繁体   中英

Binary tree to list in post-order in racket

Hi I am trying to transfer a binary tree to a list but crossing it in post-order.

(define (postorden arbol)
  (if (null? arbol) 
    ('()) (append (append (postorden (car (cdr arbol))) 
                          (postorden (car (cddr arbol)))) 
                  (list (car arbol)))))

But I get this error:

cdr: contract violation expected: pair? given: 'null

But for example with this tree: '((1 2) ((7 10) ((2 4) null null)) ((6 8) ((10 13) null null) null))

performed the cdr and get: (((7 10) ((2 4) null null)) ((6 8) ((10 13) null nul) null)) and then I make the car to this outcome: '((7 10) ((2 4) null null)) which if it is a pair. So I do not understand what is wrong.

Thank you in advance.

An if expression has the for (if e0 e1 e2) where e0,e1,and,e2 are expressions. In your program you have:

(if e0
    ('() ...)
    e2)

This means that if e0 is true, then ('() ...) is evaluated.

Here '() is the empty list also known as null .

The expression ('() ...) will attempt to call '() with the result of ... , and thus give an error.

Fix this error, and hopefully your other errors disappears - if not show us what you've and ask again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM