Question on API to do a Graphical Tree Representation...


I was needing to review some Graduate Computer Science course material and thought a good way to do it would be to implement some of the items myself in code (a lot of the material is presented as paper and pencil problems).

Is there a good C/C++ API that can display a graphical representation of a tree in memory? I'd like to find one that doesn't require a lot of mangling with MFC/Win32 (other API's would work great instead).

Specifically, I was wanting to implement an attribute grammar for a simple language, generating the associated parse tree and displaying it graphically with textual annotations. I may reuse this to practice a number of other tree/graph algorithms problems later.

Thanks in advance.
If your talking about a tree like the one shown on this page:

Abstract syntax tree

then there's no standard GUI component available to help you. You're going to have to code something using Win32 directly, or using MFC, Qt, wxWidgets, etc (none of which have a suitable stock component.) Unless you can find some open source code you can "borrow", that is.

Coding something like this in Win32 GDI calls should be pretty simple. You probably just need to use MoveToEx, LineTo, Rectangle, and DrawText. Plus change the colors of the pens. Of course, you can do more if you want it to look prettier.

(DrawText can be asked to tell you how big a rectangle is required for a piece of text. See explantion of th DT_CALCRECT flag in the MSDN entry for DrawText. You can use it tp calculate the sizes of the rectangles, then use that information to calculate their positions, and then draw them.)


PS The only Win32 tree control is the one used for the left hand side of Windows Explorer, etc; which prob isn't what you want.
Last edited on
Another thought...

If you don't want to go to the trouble of coding custrom drawing routines, you could get GraphViz to do the work for you, like here:

Tree structure generator

This project uses C#, but it should be easy enough to borrow the general idea.


Example from: Writing a translator in a day, with Dhaka

Graphviz - Graph Visualization Software

The DOT Language
Topic archived. No new replies allowed.