function draw_tree(T,xmin,xmax,ystart,position)
% DRAW_TREE recursively generates the plot for the decision tree T.
%
%	draw_tree(T,xmin,xmax,ystart,position)
%
%	T: matrix representing tree
%	xmin: current min of the area we are working on
%	xmax: current max of the area we are working on
%	ystart: current y value of area we are working on
%	position: position in T (column index of T)

global count;

m = size(T,1);
n = m - 4;

% draw node

xnode = (xmin + xmax)/2;
ynode = ystart;

plot(xnode,ynode,'wo');

% label this node

text(xnode+0.2,ynode+0.2,num2str(count));
count = count+1;

% draw leftchild arc
%	determine the points for the left child

xleft = (xmin + xnode)/2;
yleft = ynode - 1;

xl = [ xleft:(xnode-xleft)/100:xnode ];
yl = [ yleft:(ynode-yleft)/100:ynode ];

plot( xl , yl , 'r' );

% draw rightchild arc

xright = (xnode + xmax)/2;

xr = [ xnode:(xright-xnode)/100:xright ];
yr = [ ynode:-(ynode-yleft)/100:yleft ];

plot( xr, yr, 'r' );

% determine if the left child is a leaf or not

if (T(n+2,position) ~= -1)

	draw_tree(T,xmin,xnode,yleft,T(n+2,position));

else

	% draw the left leaf node and label it to be A
	
	plot( xleft, yleft, 'wo');
	text( xleft-0.2, yleft-0.2, num2str(count) );
	text( xleft-0.2, yleft-0.5, 'A');
	count = count + 1;
end

% determin if the right child is a leaf or not

if (T(n+3,position) ~= -1)

	draw_tree(T,xnode,xmax,yleft,T(n+3,position));

else

	% draw the right leaf node

	plot( xright, yleft, 'wo' );
	text( xright-0.2,yleft-0.2,num2str(count) );
	text( xright-0.2,yleft-0.5,'B');
	count = count+1;
end

return



