Return returns nothing in recursive function

Matthew Warren Matthew.Warren at Digica.com
Tue Oct 17 11:31:23 EDT 2006


Hallo people,

I have the following code that implements a simple recursive tree like
structure.

The trouble is with the GetTreeBranch function, the print statement
prints a valid value but the return immediatley afterward doesn't return
anything.

Can anyone help me with why?


Thanks,

Matt.

Code and output follow;

'''
dirtree

A simple implementation of a filesystem-like tree
datastructure.
'''

def MakeNewTree(rootname='root'):
    '''MakeNewTree

    Creates an empty tree with a 'root' node.
    If the parameter rootname is not given, it
    defaults to 'root'.
    '''
    return [{rootname:[]}]

def DoThingsToTree(path,value,tree,delete=False):
    '''_DoThingsToTree

    You should not use this function.
    '''
    if type(path)==type(''):
        path=path.lstrip('/').rstrip('/').split('/')
    for item in tree:
        if type(item)==type({}) and item.has_key(path[0]):
            if len(path)==1:
                if not delete:
                    try:
                        item[path[0]].append(value)
                    except KeyError:
                        item[path[0]]=value
                else:
                    if value is not None:
                        del(item[path[0]][value])
                    else:
                        del(tree[tree.index(item)])
                break
            else:
                _DoThingsToTree(path[1:],value,item[path[0]],delete)

def GetTreeBranch(path,tree):
    if type(path)==type(''):
        path=path.lstrip('/').rstrip('/').split('/')
    for item in tree:
        if type(item)==type({}) and item.has_key(path[0]):
            if len(path)==1:
                print 'returning',tree[tree.index(item)]
                return tree[tree.index(item)]
            else:
                GetTreeBranch(path[1:],item[path[0]])        

def AddItemToTree(path,value,tree):
    '''AddITemToTree

    Add an item onto a 'branch' of the tree.
    path    should be a '/' separated string that
            defines a path to the branch required.
    value   an object to put onto the branch
    tree    the tree to operate on.
    '''
    DoThingsToTree(path,value,tree)
    
def AddBranchToTree(path,branch,tree):
    '''AddBranchToTree

    Add a new branch to the tree.
    path    should be a '/' separated string that
            defines a path to the branch required.
    branch  an object used as the branch identifier
            (usually a string)
    tree    the tree to operate on
    '''
    DoThingsToTree(path,{branch:[]},tree)
                    
def DelItemFromTree(path,index,tree):
    '''DelItemFromTree

    Remove an item from the tree.
    path    should be a '/' separated string that
            defines a path to the branch required.
    index   the numeric index of the item to delete
            on the branch to delete. items are indexed
            in the order they are added, from 0
    '''
    DoThingsToTree(path,index,tree,delete=True)
    

def DelBranchFromTree(path,tree):
    '''DelBranchFromTree

    Remove a branch and all items and subbranches.
    path    should be a '/' separated string that
            defines a path to the branch required.
    tree    The tree to delete the branch from.
    '''
    DoThingsToTree(path,None,tree,delete=True)


OUTPUT;

>>> from dirtree import *
>>> MyTree=MakeNewTree('Matt')
>>> AddBranchToTree('Matt','Good',MyTree)
>>> AddBranchToTree('Matt','Bad',MyTree)
>>> AddItemToTree('Matt/Good','Computers',MyTree)
>>> AddItemToTree('Matt/Bad','Drinking',MyTree)
>>> a=GetTreeBranch('Matt/Bad',MyTree)
returning {'Bad': ['Drinking']}
>>> a
>>>


This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer. 

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com



More information about the Python-list mailing list