[Tutor] about tree input

劉權陞 01patrickliu at gmail.com
Sun Apr 22 09:46:42 EDT 2018


To deposit a nodes in the tree (adding a new node), for example: put 3: {}
in the Tree branch at branch tuple (2, 4, 1, 5) .
If don't use :
def f(T,k,v):
    m=len(T)
    if m==0:
        Tree[k]=v
    elif m==1:
        Tree[T[0]][k]=v
    elif m==2:
        Tree[T[0]][T[1]][k]=v
    elif m==3:
        Tree[T[0]][T[1]][T[2]][k]=v
    elif m==4:
        Tree[T[0]][T[1]][T[2]][T[3]][k]=v
    elif m==5:
        Tree[T[0]][T[1]][T[2]][T[3]][T[4]][k]=v
    ...

f( (2, 4, 1, 5),3,{})

Is there a dynamic approach(the node tuple can be any length)
I want to create a function like f(branch_tuple,key,value)

Tree = {1: {2: {3: {...}, 4: {...}, ...}, 3: {2: {...}, 4: {...}, ...}, 4:
{2: {...}, 3: {...}, ...}, ...}, 2: {1: {...}, 3: {...}, 4: {...}, ...}, 3:
{...}, 4: {...}, ...}

*(... indicates omission)
* (Node tuple represent one of the branches)


More information about the Tutor mailing list