使用树布局可视化Racket中的任意树

时间:2019-02-10 22:35:55

标签: tree racket visualization

如何可视化任意树?

例如:     import gspread from oauth2client.service_account import ServiceAccountCredentials def next_available_row(wks): str_list = list(filter(None, wks.col_values(2))) # fastest return str(len(str_list)+1) scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] credentials = ServiceAccountCredentials.from_json_keyfile_name('TOOL-fsbd3df3.json', scope) gc = gspread.authorize(credentials) wks = gc.open("Log").sheet1 next_row = next_available_row(wks) wks.update_acell("B{}".format(next_row), "test") wks.update_acell("C{}".format(next_row), "test")

或使用以下命令生成的

(define T1 '(and (or x1 x2)(or x3 x4 x5)))

球拍似乎有: https://docs.racket-lang.org/pict/Tree_Layout.html

用圆圈中的功能名称和参数来可视化功能树是否足够?

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作来可视化任意大小的树:

(require pict
         pict/tree-layout)

(define (draw tree)
  (define (viz tree)
    (cond
      ((null? tree) #f)
      ((not (pair? tree))
       (tree-layout #:pict (cc-superimpose
                            (disk 30 #:color "white")
                            (text (symbol->string tree)))))
      ((not (pair? (car tree)))
       (apply tree-layout (map viz (cdr tree))
              #:pict (cc-superimpose
                      (disk 30 #:color "white")
                      (text (symbol->string (car tree))))))))
  (if (null? tree)
      #f
      (naive-layered (viz tree))))

例如,使用您提供的列表:

(define t1 '(and (or x1 x2) (or x3 x4 x5)))
(define t2 '(or (if A1 (and A1 (not (if D1 (and A0 A0) (or A0 A0)))) (or A0 A0)) D0))

enter image description here

答案 1 :(得分:0)

我认为可以改善assefamaru的答案-

  • 从单个节点渲染中分离遍历列表
  • 简化输入案例分析
    1. 如果输入为空(基本情况),则返回空树#f
    2. 否则(归纳)输入为 not null。如果输入是列表,则创建tree-layout中的op,然后将draw递归应用于args
    3. 否则(归纳)输入为 not null,输入为 not 列表。创建原子tree-layout的{​​{1}}

上面的编号行对应于下面的注释-

a

enter image description here