# 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)
遍历二叉树-递归-非递归-morris
最新推荐文章于 2022-07-08 20:30:00 发布