1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <Simple_window.h>
#include <iostream>
class Binary_tree : public Shape
{
public:
Binary_tree(Point, int);
void add_point(Point p_) { add(p_);}
void draw_lines() const ;
private:
int level;
};
//*******************
Binary_tree::Binary_tree(Point p, int le):level(le)
{
add(Point(p));
int dis_x_r = 150, dis_x_l = 150, dis_y = 40;
int w1 = 2, k = 1, l = 1, m = 1, scale = 63;
for(int i=1; i<level-1; i++)
w1 *= 2;
for(int j=1; j<w1; j++)
{
while(m<l)
{
k *= 2;
m++;
}
if(j == k) {
if(j > 1)
{
dis_x_r -= scale;
dis_x_l -= scale;
scale -= 6;
}
l++; }
add(Point(point(number_of_points()-j).x-dis_x_l, point(number_of_points()-j).y+dis_y));
add(Point(point(number_of_points()-j-1).x+dis_x_r, point(number_of_points()-j-1).y+dis_y));
}
}
//********************************
void Binary_tree::draw_lines() const
{
int k = 1, w2 = 2;
for(int i=1; i<level-1; i++)
w2 *= 2;
for(int j=w2-1; j>0; j--)
{
fl_arc (point(number_of_points()-j-w2).x, point(number_of_points()-j-w2).y,6,6,0,360);
fl_line(point(number_of_points()-j-w2).x, point(number_of_points()-j-w2).y, point(number_of_points()-j-w2+k).x,
point(number_of_points()-j-w2+k).y);
fl_arc (point(number_of_points()-j-w2+k).x, point(number_of_points()-j-w2+k).y,6,6,0,360);
fl_line(point(number_of_points()-j-w2).x, point(number_of_points()-j-w2).y, point(number_of_points()-j-w2+k+1).x,
point(number_of_points()-j-w2+k+1).y);
fl_arc (point(number_of_points()-j-w2+k+1).x, point(number_of_points()-j-w2+k+1).y,6,6,0,360);
k++;
}
}
//*************************
int main()
{
int lev;
cout <<" Please enter the level of binary tree: ";
cin >> lev;
Simple_window win(Point(100,100), 1000, 600, "Binary_tree");
Point p(500,50);
if(lev == 0)
{
Text t(Point(400,50),"There is no node!");
win.attach(t);
win.wait_for_button();
}
else if(lev == 1)
{
Circle c(Point(p),3);
c.set_style(Line_style(Line_style::solid,1));
win.attach(c);
win.wait_for_button();
}
else {
Binary_tree bt(p,lev);
win.attach(bt);
win.wait_for_button();
}
}
|