Premiumlab  

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Trees/Tree%20Representation%20Implementation%20(Lists).ipynb

 

 

http://blog.csdn.net/bone_ace/article/details/46718683

 

 

Tree implemention using list

 

def BinaryTree(r):
    return [r, [], []]
    
def insertLeft(root, newBranch):
    t = root.pop(1)
    if len(t) > 1:
        # if the left child has node t, then we push it down
        root.insert(1, [newBranch, t, []])
    else:
        root.insert(1, [newBranch, [], []])
    return root
    
def insertRight(root, newBranch):
    t = root.pop(2)
    if len(t) > 1:
        root.insert(2, [newBranch, [], t])
    else:
        root.insert(2, [newBranch, [], []])
    return root
    
def getRootVal(root):
    return root[0]
    
def setRootVal(root, newVal):
    root[0] = newVal
    
def getLeftChild(root):
    return root[1]
    
def getRightChild(root):
    return root[2]

 

posted on 2017-06-08 18:40  Premiumlab  阅读(143)  评论(0编辑  收藏  举报