遍历二叉树-递归-非递归-morris

# import os
# if __name__ == "__main__":
#     print("hello ali")

class treenode():
    def __init__(self):
        self.val = None
        self.left = None
        self.right = None


class tree():
    def __init__(self, seq):
        self.seq = seq
        self.length = len(seq)
        self.allocmaxRoot = self.length // 2
        self.root = self.alloc(0)

    def alloc(self, index):
        if index < self.length:
            node = treenode()
            node.val = self.seq[index]
            node.left = self.alloc(2*index + 1)
            node.right = self.alloc(2*index + 2)
            return node
        else:
            return None
    
    # print node value in Before recursive style
    def recursiveBefore(self, root):
        if not root:
            return
        print(root.val)
        self.recursiveBefore(root.left)
        self.recursiveBefore(root.right)
    
    # print node value in Middle recursive style
    def recursiveMiddle(self, root):
        if not root:
            return
        
        self.recursiveMiddle(root.left)
        print(root.val)
        self.recursiveMiddle(root.right)

    # print node value in After recursive style
    def recursiveAfter(self, root):
        if not root:
            return
        
        self.recursiveAfter(root.left)
        self.recursiveAfter(root.right)
        print(root.val)

    # print node value in an None Before recursive style
    def printBefore(self, root):
        stack1 = []
        
        tmp = root
        while tmp:
            print(tmp.val)
            stack1.append(tmp)
            tmp = tmp.left
        
        while stack1:
            tmp = stack1.pop()
            tmp = tmp.right
            while tmp:
                print(tmp.val)
                stack1.append(tmp)
                tmp = tmp.left
    
    # print node value in an None Middle recursive style
    def printMiddle(self, root):
        stack1 = []

        tmp = root
        while tmp:
            stack1.append(tmp)
            tmp = tmp.left
        
        while stack1:
            tmp = stack1.pop()
            print(tmp.val)
            tmp = tmp.right
            while tmp:
                stack1.append(tmp)
                tmp = tmp.left
        

    # print node value in an None After recursive style
    def printAfter(self, root):
        stack1 = []
        stack2 = []

        tmp = root
        while tmp:
            stack1.append(tmp)
            stack2.append(tmp)
            tmp = tmp.right
        
        while stack1:
            tmp = stack1.pop()
            tmp = tmp.left

            while tmp:
                stack1.append(tmp)
                stack2.append(tmp)
                tmp = tmp.right
        
        while stack2:
            tmp = stack2.pop()
            print(tmp.val)
    
    # print node value in Before morris style
    def morrisPrintBefore(self, root):
        cur = root
        while cur:
            
            if not cur.left:
                print(cur.val)
                cur = cur.right
            else:
                
                sright = cur.left
                while sright.right and sright.right != cur:
                    sright = sright.right
                
                if not sright.right:
                    print(cur.val)
                    sright.right = cur
                    cur = cur.left
                else:
                    sright.right = None
                    cur = cur.right
                
    # print node value in Middle morris style
    def morrisPrintMiddle(self, root):
        cur = root
        while cur:
            if not cur.left:
                print(cur.val)
                cur = cur.right
            else:
                sright = cur.left
                
                while sright.right and sright.right != cur:
                    sright = sright.right
                
                if sright.right == None:
                    sright.right = cur
                    cur = cur.left
                else:
                    print(cur.val)
                    sright.right = None
                    cur = cur.right
                
    # only print node val in right
    def printRight(self, root):
        p = root
        while p:
            print(p.val)
            p = p.right

    # reverse tree in right
    def reverseRight(self, root):
        if not root or not root.right:
            return root
        
        p = root
        q = root.right
        p.right = None
        while q:
            l = q.right
            q.right = p
            p = q
            q = l
        
        return p
    

    # print node value in After morris style
    def morrisPrintAfter(self, root):
        cur = root

        while cur:
            if not cur.left:
                
                cur = cur.right
            else:
                sright = cur.left
                
                while sright.right and sright.right != cur:
                    sright = sright.right
                

                if not sright.right:
                    sright.right = cur
                    cur = cur.left
                else:
                    sright.right = None

                    self.reverseRight(cur.left)
                    self.printRight(sright)
                    self.reverseRight(sright)
                    

                    cur = cur.right

        t = self.reverseRight(root)
        self.printRight(t)
        self.reverseRight(t)

        


        






if __name__ == "__main__":
    seq = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
    T = tree(seq)
    root = T.root
    # T.recursiveBefore(root)
    # T.recursiveMiddle(root)
    # T.recursiveAfter(root)
    # T.printBefore(root)
    # T.printMiddle(root)
    # T.printAfter(root)
    # T.morrisPrintBefore(root)
    # T.morrisPrintMiddle(root)
    T.morrisPrintAfter(root)


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值