category_tree.rb
def add_category(category_name, parent_name)
    # First check if adding the parent/root node or a child to an existing parent
    if parent_name.nil?
      unless @root.nil?
        raise ArgumentError.new('CategoryTree root already exists!')
      end

      # Add the root node
      @root = CatNode.new(category_name)
    else
      # Get the parent CatNode in the tree
      parent_node = get_node(parent_name)

      if parent_node.nil?
        raise ArgumentError.new("CategoryTree does not contain parent '#{parent_name}'!")
      else
        # Check if this category already exists
        raise ArgumentError.new("Category '#{category_name}' already exists in the tree!") if contains?(category_name)

        # Add child to the parents child list
        parent_node.insert_child(category_name)
      end
    end
  end
# Add a new parent or child category node to the CategoryTree
Add a new parent or child category node to the CategoryTree
Add a new parent or child category node to the CategoryTree
def add_category(category_name, parent_name)
    if parent_name.nil?
      unless @root.nil?
        raise ArgumentError.new('CategoryTree root already exists!')
      end
      @root = CatNode.new(category_name)
    else
      parent_node = get_node(parent_name)
      if parent_node.nil?
        raise ArgumentError.new("CategoryTree does not contain parent '#{parent_name}'!")
      else
        raise ArgumentError.new("Category '#{category_name}' already exists in the tree!") if contains?(category_name)
        parent_node.insert_child(category_name)
      end
    end
  end
def add_category ( category_name , parent_name ) if parent_name . nil? unless @root . nil? raise ArgumentError . new ( 'CategoryTree root already exists!' ) end @root = CatNode . new ( category_name ) else parent_node = get_node ( parent_name ) if parent_node . nil? raise ArgumentError . new ( "CategoryTree does not contain parent '#{parent_name}'!" ) else raise ArgumentError . new ( "Category '#{category_name}' already exists in the tree!" ) if contains? ( category_name ) parent_node . insert_child ( category_name ) end end end
Add a new parent or child category node to the CategoryTree
Add a new parent or child category node to the CategoryTree
# First check if adding the parent/root node or a child to an existing parent # Add the root node # Get the parent CatNode in the tree # Check if this category already exists # Add child to the parents child list
category_name
parent_name