From: ·········@math.ufl.edu
Subject: Already-written code for printing small trees?
Date: 
Message-ID: <1194317643.803724.160880@y42g2000hsy.googlegroups.com>
I seek code that prints, in ascii-art, small binary trees.
E.g, the 3-leaf tree  ( B . (A . C))  might print something like

	      C
	     /
	    /
	   o
	  / \              [Root is on the left.]
	 /   \
	*     A
	 \
	  \
	   B

or
                       *      [Root is on top.]
                      / \
                     /   \
                    /     \
                   B       o
                          / \
                         /   \
                        /     \
                       A       C


I have seen posted here some nice car-cdr diagrams that
look like they were automatically generated.  The feature I
seek is that same-depth nodes are aligned, either
horizontally or vertically.

[Initially, my hope is to use such for printing the trees
of small Huffman codes.]

--
Prof. Jonathan LF King   Mathematics dept, Univ. of Florida

From: Alexander Schmolck
Subject: Re: Already-written code for printing small trees?
Date: 
Message-ID: <yfsy7dby9r1.fsf@gmail.com>
·········@math.ufl.edu writes:

> I seek code that prints, in ascii-art, small binary trees.
> E.g, the 3-leaf tree  ( B . (A . C))  might print something like
>
> 	      C
> 	     /
> 	    /
> 	   o
> 	  / \              [Root is on the left.]
> 	 /   \
> 	*     A
> 	 \
> 	  \
> 	   B
>
> or
>                        *      [Root is on top.]
>                       / \
>                      /   \
>                     /     \
>                    B       o
>                           / \
>                          /   \
>                         /     \
>                        A       C
>
>
> I have seen posted here some nice car-cdr diagrams that
> look like they were automatically generated.  The feature I
> seek is that same-depth nodes are aligned, either
> horizontally or vertically.
>
> [Initially, my hope is to use such for printing the trees
> of small Huffman codes.]

How about this?

Here's the output:

    CL-USER> (left-ascii-tree '(b . (a . c))))))


      ,B
    `+
     |  ,A
      `+
        `C

Here's the code:

    (defun left-ascii-tree (tr &key (out t))
      "Given a tree of cons cells TR create a simple and compact ascii tree to OUT
      (either a stream or an adjustable string; default: stdout)"
      (labels ((pad (from-below top-pad bot-pad)
                 (reverse (if from-below (cons "," top-pad) (cons "`" bot-pad))))
               (pr (tr top-pad bot-pad from-below)
                 (etypecase tr
                   (cons (pr (car tr) (cons "  " top-pad) (cons " |" top-pad) t)
                         (format out "~{~a~}+~%" (pad from-below top-pad bot-pad))
                         (pr (cdr tr) (cons " |" bot-pad) (cons "  " bot-pad)
                         nil))
                   (atom (format out "~{~a~}~a~%" (pad from-below top-pad bot-pad)
                   tr)))))
        (pr tr nil nil nil)))

you can easily change it so that it prints "left to right upwards" rather than
"left to right downwards".


cheers,

'as
From: ·········@math.ufl.edu
Subject: Re: Already-written code for printing small trees?
Date: 
Message-ID: <1194368812.065201.161090@50g2000hsm.googlegroups.com>
On Nov 6, 5:00 am, Alexander Schmolck <··········@gmail.com>
wrote:  ...

> How about this?

Thank you very much --this looks like what the Good Doctor
ordered (my kids were watching Star Trek last night).

> Here's the output:
>
>     CL-USER> (left-ascii-tree '(b . (a . c))))))
>
>       ,B
>     `+
>      |  ,A
>       `+
>         `C

I will play with the code.  I take it that `labels' is
used, rather than `flet', because `pr' recursively calls
itself, and calls `pad', yes?
                Thanks again.       --JK
From: Dimiter "malkia" Stanev
Subject: Re: Already-written code for printing small trees?
Date: 
Message-ID: <5psibeFsuhenU1@mid.individual.net>
>     (defun left-ascii-tree (tr &key (out t))
>       "Given a tree of cons cells TR create a simple and compact ascii tree to OUT
>       (either a stream or an adjustable string; default: stdout)"
>       (labels ((pad (from-below top-pad bot-pad)
>                  (reverse (if from-below (cons "," top-pad) (cons "`" bot-pad))))
>                (pr (tr top-pad bot-pad from-below)
>                  (etypecase tr
>                    (cons (pr (car tr) (cons "  " top-pad) (cons " |" top-pad) t)
>                          (format out "~{~a~}+~%" (pad from-below top-pad bot-pad))
>                          (pr (cdr tr) (cons " |" bot-pad) (cons "  " bot-pad)
>                          nil))
>                    (atom (format out "~{~a~}~a~%" (pad from-below top-pad bot-pad)
>                    tr)))))
>         (pr tr nil nil nil)))

That's a beautiful code! Thanks!!