Solved

# problem with draving a binary tree

Posted on 2003-02-22
Medium Priority
218 Views
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
0
Question by:3ler
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 7
• 5
• 4
• +3

LVL 49

Expert Comment

ID: 7999712
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

LVL 1

Author Comment

ID: 7999838
yes the n increses for every level..
But the problem is that the leaves cover each other....
0

LVL 101

Expert Comment

ID: 8000062
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

LVL 1

Author Comment

ID: 8000300
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

LVL 101

Expert Comment

ID: 8000978
Try drawk(k,600,0,0)

mlmcc
0

LVL 49

Expert Comment

ID: 8001389
Well, if it's a left leaf you decrease x, and a right leaf you increase x.
0

LVL 101

Expert Comment

ID: 8002776
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

LVL 1

Author Comment

ID: 8003354
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

LVL 101

Expert Comment

ID: 8004177
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

LVL 1

Author Comment

ID: 8007871
that does not work..
i'm lost here guys..
0

LVL 101

Expert Comment

ID: 8018288
How tall a tree are you trying to show?  At some point the screen is too narrow to show that many leaves.

mlmcc
0

LVL 1

Author Comment

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

0

LVL 15

Expert Comment

ID: 8021488
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

LVL 5

Accepted Solution

Kocil earned 200 total points
ID: 8062701
You need another parameter, the width,

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

LVL 15

Expert Comment

ID: 8062794
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

LVL 1

Author Comment

ID: 8065872
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

LVL 15

Expert Comment

ID: 8065917
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

LVL 1

Author Comment

ID: 8066065
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

Expert Comment

ID: 9314380
3ler:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 49

Expert Comment

ID: 9316749
Points to mlmcc
0

LVL 15

Expert Comment

ID: 9324139
split kocil, mlmcc
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introducing Priority Question, our latest feature.
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediafâ€¦
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increasedâ€¦
###### Suggested Courses
Course of the Month9 days, 20 hours left to enroll