Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've historically found writing code for manipulating tree structures awkward; and a primary reason for it is that when things go wrong, visualising why is hampered by the lack of a mechanism for creating convenient, compact and readable dumps of the structures.

Data::Dump, Data::Dumper et al. do a perfectly fine job of dumping the structure in terms of the arrays (or hashes) that hold the tree, but are pretty useless when if comes to visualising the tree structure itself: levels; depth; width; changes from one iteration to the next etc. And I usually find myself resorting to manually manipulating a dump in order to work out what is going on.

For example, a not very complicated, array-based binary tree might get dumped like this:

[ [ [[[[["a", "b"], "c"], ["d", "e"]], [[["f", "g"], "h"], [["i", +"j"], ["k", ["l", "m"]]]]], ["n", [[["o", "p"], "q"], ["r", "s"]]]], ["t", ["u", "v"]] ], [["w", ["x", "y"]], "z"] ]

Try working out how deep; or wide; or which nodes are at the same level; from that. Not to mention seeing what changed between this dump and the previous -- when (for example) re-balancing the tree.

With hand manipulating this you can come up with something like:

Which allows those questions to be answered, but it is hardly concise or convenient; and makes comparing multiple iterations of changes very awkward.

The same tree can be represented this way:

___________/\_________ +___ ________________/\____________ +__/\_ _____________/\______________ /\ / +\ \ ________/\________ ___/\___ / \ / + \ z __/\__ _____/\____ / __/\__ t /\ w + /\ /\ \ /\ __/\__ n /\ \ u v +x y / \ /\ / \ / /\ / \ /\ /\ c d e /\ h /\ / \ /\ q r s a b f g i j k /\ o p l m

which is far nicer.

I've spent the last couple of days trying to code this but haven't yet succeeded. Any offers?

(NOTE: I'm looking for a compact dump to a terminal; not presentation quality graphics here. Ie. I'm not looking for GraphViz or similar, so please don't tell me about those.)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


In reply to Challenge: Dumping trees. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-19 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found