Skip to content
Snippets Groups Projects

Pretty print binary tree in Racket

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Gustav Lahmann

    Function to pretty print binary trees as defined in set.scm in Übung 4 Aufgabe 4 Deklarative Programmierung

    Edited
    print-binary-tree.rkt 897 B
    #lang racket
    (require racket/include)
    (require pict)
    (require pict/tree-layout)
    (require pict/color)
    (require racket/format)
    
    (include "set.scm")
    
    (define (liste->baum lst)
      (define (rek-lst-bm lst bm)
        (if (empty? lst)
            bm
            (rek-lst-bm (cdr lst) (adjoin-set (car lst) bm)))
        )
      (rek-lst-bm lst the-empty-tree)
    )
    
    (define (print-tree bm)
      (define (print-entry num left right)
        (tree-layout #:pict(cc-superimpose (gray(filled-ellipse 20 20)) (text (~a num))) left right)
      )
      (define (print-empty-entry)
        ; make the lines end at the top of squares
        (tree-layout #:pict(cbl-superimpose (blank 20) (gray (rectangle 10 10))))
      )
      (define (gen-tree bm)
        (if (empty-tree? bm)
        (print-empty-entry)
        (print-entry (entry bm) (gen-tree (left-branch bm)) (gen-tree (right-branch bm))))
      )
      (naive-layered (gen-tree bm))
    )
    
    (print-tree (liste->baum '(5 2 6 4 5)))
    set.scm 1.16 KiB
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment