problem with draving a binary tree

ok some school project.. Have to benchmark performance in AVL and BST (Ordinary tree). Done that but now i have to draw the tree in graphics mode.
ok i can draw the tree with no problem but the branches(leaves) cover each other...
Here is the sub thats suppose to do that:


procedure drawk(k:kaz;x,y,n:integer);
var s:string;
begin
if k<>NIL then
begin
  drawk(k^.l,x-(x div 2),y+20,n+1);
  str(k^.v,s);
  outtextXY(x,y,s);
  drawK(k^.d,x+(x div 2),y+20,n+1);
end;
end;
..
x,y are position on the screen.
n is the level

help guys :)

tnx
LVL 1
3lerAsked:
Who is Participating?
 
KocilConnect With a Mentor Commented:
You need another parameter, the width,
instead of level (n).

procedure drawk(k:kaz;x,y,width:integer);
var s:string;
begin
if k<>NIL then
begin
 str(k^.v,s);
 outtextXY(x,y,s);
 drawk(k^.l,x-(width div 2),y+20, width div 2);
 drawK(k^.d,x+(width div 2),y+20, width div 2);
end;
end;


call this from main

drawk(k, getmaxx(), 10, getmaxx() / 2);

chaeers
0
 
dbruntonCommented:
Are you increasing n for each level?  To go downwards.

Plus your x and y will change for each leaf.

Assuming you are starting at top of screen in the middle.
As you go left decrease x.  If you go right increase x.
If you go down increase y.  If you go up decrease y.
0
 
3lerAuthor Commented:
yes the n increses for every level..
But the problem is that the leaves cover each other....
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mlmccCommented:
In which way are they overlapping?  I am assuming they are over lapping up & down.

Try using y + 40 or y+ 50

Check what the form measurements are in.  If they are twips 144 twips per inch - 20 would be about 7 lines per inch.  Maybe too small

mlmcc
0
 
3lerAuthor Commented:
no the leaves on the same level are drawn one over another horiznotaly. try doing a trace of the procedure if the input is drawk(k,50,0,0); They start to overlap on the secnond level..
0
 
mlmccCommented:
Try drawk(k,600,0,0)

mlmcc
0
 
dbruntonCommented:
Well, if it's a left leaf you decrease x, and a right leaf you increase x.
0
 
mlmccCommented:
The appropriate increases and decreases are being done.  I don't think he is changing enough vertically nor is starting in the middle of the screen.

mlmcc
0
 
3lerAuthor Commented:
the vertical changes are ok. bu the values overlap horizontaly..
ex.

                     5
               2          9
           1       3 8         10

the 3 and the 8 overlap...

i have no clue what to do..
0
 
mlmccCommented:
You need to make the tree more vertical
Try

begin
 drawk(k^.l,x-(x div 4),y+20,n+1);
 str(k^.v,s);
 outtextXY(x,y,s);
 drawK(k^.d,x+(x div 4),y+20,n+1);
end;

mlmcc
0
 
3lerAuthor Commented:
that does not work..
i'm lost here guys..
0
 
mlmccCommented:
How tall a tree are you trying to show?  At some point the screen is too narrow to show that many leaves.


mlmcc
0
 
3lerAuthor Commented:
it 50 integers up to 400 ... that means something about 8 levels..
it shoudl fit i think.. but :S

0
 
VGRCommented:
you can "reserve" space on the screne for a full binary tree, like dividing the width of the screen by "number of leaves" (2^(levels-1)), and minorate this to keep some pixels/spaces around, and then just "center" the tree's root. Then begin drawing downwards as explained above.

This should work easily.

Other solution if you lack space : alternate colours, so that you don't need spacing between leaves.
0
 
VGRCommented:
it seems that you just repeated what I wrote above :D
(no offense intended, none taken please)
3ler, if you've problems writing the code I suggested, I can make an effort and give you pseudo-code
0
 
3lerAuthor Commented:
hmn this still doesn't work. it only writes ot aonly a dozen of the 50 elements of the tree...
but they don't overlap..
0
 
VGRCommented:
see : if you have TOO MANY tree leaves for the width of your screen to accomodate that number, there is NO ALGORITHM that can solve your problem. It becomes a PHYSICAL problem of "lack of space" !!!
0
 
3lerAuthor Commented:
well i know but this prog that Kocil wrote writes the tree in the top rigt of the sreen and the rest is empty...
hey just forget it guys.. i already gave the report to the teacher and he didn't say a thing about drawng it so..

tnx anyways--
em i dont who who to give points to but since kocil gave me the code...

tnx

bye
0
 
CleanupPingCommented:
3ler:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
dbruntonCommented:
Points to mlmcc
0
 
VGRCommented:
split kocil, mlmcc
0
All Courses

From novice to tech pro — start learning today.