Creating category tree programmatically in magento
devm.com.np
<?php
function addCategories($categoryName)
{
    
$categoryNames = array($categoryName);
    return 
traverseCategoryTree($categoryNames$parentCategoryId);
}

function 
traverseCategoryTree($categoryNames,$parentId)
{    
    
$name array_shift($categoryNames);
    
    if(
is_null($name))
        return 
$parentId;

    
$parentCategory Mage::getModel('catalog/category')->load($parentId);

    
$category Mage::getModel('catalog/category')->getCollection(); 
    
$category->addFieldToFilter('level',$parentCategory->getLevel()+1);
            ->
addAttributeToFilter('name',$name);
    
$category $category->getFirstItem();
        
    if(!
$category->getId())
    {            
        
$category->setName($name);
        
$category->setStoreId($this->_storeId);
        
$category->setIsActive(1);
        
$category->setIsAnchor(1);            
        
        
$category->setPath($parentCategory->getPath());              
        
$category->save();
    }    
    
    return 
traverseCategoryTree($categoryNames,$category->getId());        
}
?>