function [correct]=classify_vectors_tree(xx,Mat,tree_array, debug_flag)
    %diffVec = max(Mat)-min(Mat)+0.0001;
    %diffVec = diffVec(2:size(Mat,2)).^2;
correct = 0;
for i=1:size(xx,1),
    Ind=1;
    Level=1;
    if debug_flag,
        str=[sprintf('%6.4f ', xx(i,2:size(Mat,2)))];
        disp([sprintf('Test %d: class %d %s',i,xx(i,1),str)]);
    end
    while (1),
        cnode = tree_array(Ind);
        if debug_flag,
            disp([sprintf('  Node at level %d (position %d)', Level, Ind)]);
        end
        if cnode >= 1,
            if debug_flag,
                disp([sprintf('   Closest leaf node found with %d entries', cnode-1)]);
            end
            closest_dist = -1;
            class = 0;
            for j=1:(cnode-1),
                k=tree_array(Ind+j);
                
                str=[sprintf('%6.4f ', Mat(k,2:size(Mat,2)))];
                %dist = sum(((xx(i,2:size(Mat,2))-Mat(k,2:size(Mat,2))).^2)./diffVec);
                dist = sum(((xx(i,2:size(Mat,2))-Mat(k,2:size(Mat,2))).^2));
                if closest_dist < 0,
                    closest_dist = dist;
                    class = round(Mat(k,1));
                else
                    if closest_dist > dist,
                        closest_dist = dist;
                        class = round(Mat(k,1));  
                    end
                end
                if debug_flag,
                    disp([sprintf('\tTraining %d: class %d %s distance %8.6f',k,Mat(k,1),str, dist)]);
                end
            end
            if class==round(xx(i,1)),
                correct=correct+1;
                str=['correct'];
            else
                str=['wrong'];
            end
            disp([sprintf('%d /%d is classified as class %d, which is %s', i, size(xx,1), class, str)]);
            break;
        end
        splitInd=round(tree_array(Ind+3));
        Level=Level+1;
        if xx(i,splitInd) > tree_array(Ind+4),
            Ind=round(tree_array(Ind+2));
        else
            Ind=round(tree_array(Ind+1));
        end
    end
end

return;
