classdef bstree < handle
    
    properties
        value;
        left;
        right;
    end
    
    methods
        function obj = bstree(val)
            obj.value = val;
        end
        
        function insert(self, val)
            if val <= self.value
                if isempty(self.left)
                    self.left = bstree(val);
                else
                    self.left.insert(val);
                end
            else
                if isempty(self.right)
                    self.right = bstree(val);
                else
                    self.right.insert(val);
                end
            end
        end
        
        function list = to_list(self)
            left_list = [];
            right_list = [];
            if ~isempty(self.left)
                left_list = self.left.to_list();
            end
            if ~isempty(self.right)
                right_list = self.right.to_list();
            end
            
            list = [left_list self.value right_list];
        end
        
    end
end