2
$\begingroup$

How to plot a linked list such as:

ll = {{a, b}, {{c, d}, {{e, f}, {}}}}

The expected result is a tree with the nodes and edges ordered as follows.

enter image description here

The empty / null nodes should not be displayed. The starting node can have a root label or a user defined label.

$\endgroup$
  • $\begingroup$ Sounds like you could do most of this with TreeForm if you use the right option settings. $\endgroup$ – Sjoerd Smit Dec 24 '18 at 10:52
  • $\begingroup$ As Sjoerd Smit sayd, TreeForm@list is almost that what you want... and a bit more, the empty list is displayed correctly $\endgroup$ – mgamer Dec 24 '18 at 12:15
  • $\begingroup$ Thanks @SjoerdSmit the answer uses exactly that ! I've accepted this as a correct solution. $\endgroup$ – v1j4y Dec 24 '18 at 14:28
3
$\begingroup$

As recommended in the comments, use TreeForm

$Version

(* "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" *)

ll = {{a, b}, {{c, d}, {{e, f}, {}}}};

TreeForm["root" @@ (ll /. {} :> Nothing), 
 VertexRenderingFunction -> (Text[
     If[#2 === List, "", 
      Framed[Style[#2, 18], Background -> White, 
       FrameStyle -> White]], #1] &)]

enter image description here

$\endgroup$
  • $\begingroup$ That's exactly what I was looking for ! Brilliant. $\endgroup$ – v1j4y Dec 24 '18 at 14:23
  • $\begingroup$ Perhaps this tweak is useful: tf[ex_, root_: "root"] := TreeForm[root @@ (ex /. {} :> Nothing), VertexRenderingFunction -> (Text[ If[#2 === List, "", Style[#2, 18]], #1] &)]; $\endgroup$ – Somos Dec 24 '18 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.