function T = prune_kill_kids(T,position)
% PRUNE_KILL_KIDS converts the current node (the last element of the 
%	position vector) to the appropriate leaf node.  All corresponding
%	children of the new leaf node are removed from the tree T.
%
%	T = prune_kill_kids(T,position)
%
%	T : matrix representing decision tree T, generated by MSM-T
%	position: vector representing order in which T is visited

% n is the dimension of the points in sets A, B.
global n;

% index of the node to be made a leaf is the last entry in position

leaf_index = size(position,2);
leaf_position = position(leaf_index);

if (leaf_position ~= -1) 	% then this isn't a leaf and must be made one

	% if the left child isn't a leaf, make it a leaf
	if (T(n+2,leaf_position) ~= -1)
		T = prune_kill_kids(T,[position,T(n+2,leaf_position)]);
	end

	% if the right child isn't a leaf, make it a leaf
	if (T(n+3,leaf_position) ~= -1)
		T = prune_kill_kids(T,[position,T(n+3,leaf_position)]);
	end

	% by the time we reach here, both children of leaf_position are
	% made leaves

	% fix the parent
	parent_index = leaf_index - 1;
	parent_position = position(parent_index);

	% NOTE: 1 of the 2 if statements must be satisfied, only 1.
	if (T(n+2,parent_position) == leaf_position)
		T(n+2,parent_position) = -1;
	end;
	if (T(n+3,parent_position) == leaf_position)
		T(n+3,parent_position) = -1;
	end;

	% remove the leaf_node from T
	new_column_set = [1:(leaf_position - 1),(leaf_position + 1): size(T,2)];
	T = T(:,new_column_set);	% T is shortened by 1 column

	% must reindex T

	for j = 1:(size(T,2))
		if (T(n+2,j) > leaf_position)
			T(n+2,j) = T(n+2,j) - 1;
		end
		if (T(n+3,j) > leaf_position)
			T(n+3,j) = T(n+3,j) - 1;
		end
	end % for
end
return

% NOTE: if leaf_position was a leaf, nothing is done

	




