zoukankan      html  css  js  c++  java
  • 二叉树的实现

    def BinarTree(r):
        return [r,[],[]]
    
    def insertLeft(root,newBrach):
        t=root.pop(1)
        if len(t)>1:
            root.inset(1,[newBrach,t,[]])
        else:
            root.insert(1,[newBrach,[],[]])
        return root
    
    def insertRight(root,newBrach):
        t=root.pop(2)
        if len(t)>1:
            root.insert(2,[newBrach,[],t])
        else:
            root.insert(2,[newBrach,[],[]])
        return root
    
    def getRootVal(root):
        return root[0]
    
    def setRootVal(root,newVal):
        root[0]=newVal
    
    def getLeftChild(root):
        return root[1]
    
    def getRihtChild(root):
        return root[2]
    
    tree=BinarTree('a')
    insertLeft(tree,'b')
    insertRight(tree,'c')
    insertLeft(getLeftChild(tree),'d')
    insertRight(getLeftChild(tree),'e')
    insertLeft(getRihtChild(tree),'f')
    print(tree)
    

    def count(root):
        if root==[]:
            return 0
        else:
            n1=count(root[1])
            n2=count(root[2])
            n=1+n1+n2
            return n
    
    tree=['a',
            ['b',
                ['d',[],[]],
                ['e',[],[]],
            ],
            ['c',
                ['f',[],[]],
                []
            ]
        ]
    sum=count(tree)
    print(sum)
    
  • 相关阅读:
    jsp转向
    什么是 XDoclet?
    tomcat中的几点配置说明
    mysql5问题
    POJ 3734 Blocks
    POJ 2409 Let it Bead
    HDU 1171 Big Event in HDU
    POJ 3046 Ant Counting
    HDU 2082 找单词
    POJ 1286 Necklace of Beads
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/9111571.html
Copyright © 2011-2022 走看看