Discussions related to Visual Prolog
ahmednadi
Active Member
Posts: 37
Joined: 15 Sep 2009 14:06

How can I print a tree?

Unread post by ahmednadi »

Dear Sir;
Could you help me to print a tree to a text file?
If I have a tree, I want to convert to string to be able to print to a file.
Regards;
AHMED NADY
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

It is not completely clear what you mean by a tree or what your expect from the print.

In its simplest form you can simply write the tree to the file:

Code: Select all

class predicates     writeTree : (string Filename, tree Tree). clauses     writeTree(Filename, Tree) :-         S = outputStream_file::createUtf8_bom(Filename), % there are lots of text format choices         S:write(Tree),         S:close().
Regards Thomas Linder Puls
PDC
ahmednadi
Active Member
Posts: 37
Joined: 15 Sep 2009 14:06

Unread post by ahmednadi »

Dear Mr. Thomas Linder Puls;

Thank you very much for your prompt action.

I want to write the tree to be in understandable shape or form.

How can I draw a tree with its leaves to appear as a hierarchy form.

Regards;

AHMED NADY
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

Write and draw are two quite different things. Text files can only contain characters. It is possible to write "boxes" and the like using special characters but the result will often be disappointing.
Regards Thomas Linder Puls
PDC
ahmednadi
Active Member
Posts: 37
Joined: 15 Sep 2009 14:06

Unread post by ahmednadi »

Dear Sir;
Thank you.
How can I draw the tree in a pic file?
Regards;
AHMED NADY
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

That is rather complex and large task, so a mail can only give hints. There are two things you have to solve:
  • How to create a picture file
  • How to draw a tree
The obvious choice for creating the picture file (in Visual Prolog) is gdiplus. This will provide you with means for drawing boxes, ellipsis, curves, lines, etc and write text in absolute positions, and for rendering the result into various image formats.

When drawing binary trees a relatively obvious algorithm is to draw the root in the top middle of the image and then split the image vertically in two halfs, recursively drawing the left sub tree in the left part and the right sub tree in the right part connecting left and then connect the sub trees o the root.

You will how ever soon realize that the trees often look bad. If you should draw a tree by hand you would for example not place the root in the middle if one of the sub trees were empty, because that would leave the image half empty.

Better algorithms will first calculate the size of the two sub images and the placement of their root (recursively) and from that decide the placement of the two sub graphs and the root.

Handling graphs that does not fit the image (which typically have a max size) is a large problem on its own.[/list]
Regards Thomas Linder Puls
PDC
David Snook
Active Member
Posts: 36
Joined: 6 Feb 2003 0:01

Unread post by David Snook »

I don't know if this will help or not but if I understand what you're trying to do you can create/expand the tree in an invisible window and use the following to obtain and save an image file of the tree;

Code: Select all

PIC = vpi::pictGetfromWin(Win,RCT), vpi::pictSave(PIC,"picName"),
Although this will give you the image file it may be best to find the equivalent in GDIPlus as I suspect the vpi::pict routines will eventually become obsolete.

Regards,

David Snook
Post Reply