python二叉树递归总结

我胡汉三又回来了。我,屡战屡败,屡败屡战,发挥小强精神,绝不气馁!

1.装饰器:这篇写的很好,对装饰器的执行顺序有了全面的认知。
https://blog.csdn.net/qq_26442553/article/details/82226657
2、python build binary tree:

class BinTreeNode(object):
    def __init__(self,data,left = None,right = None):
        self.data = data
        self.left = left
        self. right = right

class Stack(object):
    def __init__(self):
        self.items =[]
    def push(self,value):
        self.items.append(value)
    def pop(self):
        return self.items.pop()
    def isempty(self):
        l = len(self.items)
        if l ==0:
            return True
        else:
            return False
class BinTree(object):
    def __init__(self,root=None):
        self.root = root

    def preordertree(self,root):
        if not isinstance(root, BinTreeNode):
            return None
        if root is not None:
            print(root.data)
            self.preordertree(root.left)   # 函数递归要self.函数名
            self.preordertree(root.right)
    def pre_order(self,root):              #非递归方式实现前序遍历
        stack = Stack()
        while(root is not None or not stack.isempty()):
            if root is not None:
                print(root.data)
                if root.right is not None:
                    stack.push(root.right)
                root = root.left
            else:
                root = stack.pop()
    def mid_order(self,root):       #非递归方式实现中序遍历,栈里面记录根节点
        stack = Stack()
        while(root is not None or not stack.isempty()):
            if root is not None:
                stack.push(root)
                root = root.left
            else:
                root = stack.pop()
                print(root.data)
                root = root.right
    def post_order(self,root):
        stack = Stack()
        s =[]
        while root is not None or not stack.isempty():
            if root is not None:
                s.append(root.data)
                #print(root.data)
                if root.left is not None:
                    stack.push(root.left)
                root = root.right
            else:
                root = stack.pop()
        return s[::-1]



    def middleorder(self, root):
        if root is not None:
            self.middleorder(root.left)
            print(root.data)
            self.middleorder(root.right)

    def postorder(self,root):
        if root is not None:
            self.postorder(root.left)
            self.postorder(root.right)
            print(root.data)
    @classmethod
    def build_bintree(cls,node_list):
        node_dict ={} #建立一个保存所有节点的字典,用data作为key标识
        for node_data in node_list:
            data = node_data['data']
            node_dict[data]= BinTreeNode(data)
        for node_data in node_list:
            data = node_data['data']
            node = node_dict[data]
            node.left = node_dict.get(node_data['left'])
            node.right = node_dict.get(node_data['right'])
            if node_data['isroot'] is True:
                cls.root = node
        return cls.root

if __name__ == "__main__":
    s=[{'data':'A','left':'B','right':'C','isroot':True},{'data':'B','left':'D','right':'E','isroot':False},{'data':'C','left':'G','right':None,'isroot':False},{'data':'D','left':None,'right':None,'isroot':False},{'data':'E','left':'M','right':'F','isroot':False},{'data':'G','left':None,'right':None,'isroot':False},{'data':'M','left':None,'right':None,'isroot':False},{'data':'F','left':None,'right':None,'isroot':False}]
    btree = BinTree()
    btree.root = BinTree.build_bintree(s)
    btree.postorder(btree.root)
    #btree.middleorder(btree.root)
    s = btree.post_order(btree.root)
    print(s)


最值得吐槽的就是二叉树的后序遍历了8,写了好久都不对,然后突然灵光一现,想到这种方法。嘻嘻。当然啦,作为真诚的学习之心,我决定还是学习一下别人正经的做法:(记录一下之后学 今日甚是乏累)
1.https://blog.csdn.net/u012877472/article/details/49401751 第三种方法
2.https://www.cnblogs.com/rain-lei/p/3705680.html 明天学一下这个

你可能感兴趣的:(小白学习笔记)