Link to home
Start Free TrialLog in
Avatar of 3ler
3ler

asked on

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
Avatar of dbrunton
dbrunton
Flag of New Zealand image

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.
Avatar of 3ler
3ler

ASKER

yes the n increses for every level..
But the problem is that the leaves cover each other....
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
Avatar of 3ler

ASKER

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..
Try drawk(k,600,0,0)

mlmcc
Well, if it's a left leaf you decrease x, and a right leaf you increase x.
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
Avatar of 3ler

ASKER

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..
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
Avatar of 3ler

ASKER

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


mlmcc
Avatar of 3ler

ASKER

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Kocil
Kocil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of 3ler

ASKER

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..
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" !!!
Avatar of 3ler

ASKER

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
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.
Points to mlmcc
split kocil, mlmcc