Binary tree class

Hello guys,

I wanted to solve an exercise which is as follows. You can look at it here (http://books.google.com/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming%20principle%20and%20practice&pg=PA517#v=onepage&q&f=true exercise no.11):

It says: "Define a Binary_tree class derived from Shape. Give the number of levels as a parameter (level == 0 means no nodes, level == 1 means one node, level ==2 means one top node with two sub-modes, level ==3 means one top node with two sub-modes each with two sub-modes, etc.). Let a node be presented as a small circle. Connect the nodes by lines (as is conventional). P.S. In computer science trees grow downward from a top node (amusingly, but logically, often called the root).".

I wrote the below code:

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();
	   }
             
}


What do you think about this code as an answer to the exercise. Is it possible to write a better one in a short time? It took than me hours!

PS: I couldn't figure out what does Mr. Stroustrup mean by that etc at the exercise! I don't know the last level of level is three or it can be infinite! But this code works until level 5 just fine.
Last edited on
Topic archived. No new replies allowed.