# 前序遍历,中序遍历, 后序遍历
class Node():
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def judge(self):
if not self.is_fulltree():
return '__left' if self.is_lefttree() else '__empty' # 如果是左子树,返回True,如果空子树返回False
else:
return '__full' # return None 表示是满子树
def __str__(self):
return '<Node> value :{} '.format(self.val)
class BinaryTree():
def __init__(self):
self.count = 0
self.layer = 0
self.root = None
def __str__(self):
return '< BinaryTree> quantity:{} '.format(self.count)
def is_empty(self):
return self.count == 0
def traversal(self): # 层序遍历节点
if not self.is_empty():
now: Node = self.root
lst = [now]
nextl = []
while True:
# print([i.val for i in lst if i !=None])
flag = 0
for i in lst:
nextl.append(i.left)
nextl.append(i.right)
if (not i.is_fulltree()) and flag == 0:
final = i
flag = 1
# 如果全None,结束
if nextl[0] == None:
final = lst[0]
return final
# 如果非全None,返回最后一个值的父节点,打印一下nextl
if nextl[0] != None and nextl[-1] == None:
# print([i.val for i in nextl if i !=None])
return final
lst = nextl
nextl = []
def append(self, value):#添加节点
if self.is_empty():
self.root = Node(value)
self.count += 1
else:
self.count += 1
now = self.traversal()
if now.judge() == '__left':
now.right = Node(value)
now.right.father = now
if now.judge() == '__empty':
now.left = Node(value)
now.left.father = now
def sequ_tra(self): # 层序遍历
if not self.is_empty():
now: Node = self.root
lst = [now]
nextl = []
final = []
while True:
for i in lst:
nextl.append(i.left)
nextl.append(i.right)
# 下一列表全None
print([x.val for x in lst])
final += [x.val for x in lst]
if nextl[0] == None:
break
if nextl[-1] == None:
nextl = [x.val for x in nextl if x != None]
print(nextl)
final += nextl
break
lst = nextl
nextl = []
return final
def postOrder(self, t): # 后续遍历
if t is None:
return
self.postOrder(t.left)
self.postOrder(t.right)
print(t.val)
def preOrder(self, t): # 前序遍历递归实现
if t is None:
return
print(t.val)
self.preOrder(t.left)
self.preOrder(t.right)
def midOrder(self, t): # 中序遍历递归实现
if t is None:
return
self.midOrder(t.left)
print(t.val)
self.midOrder(t.right)
def queue_pre(self): # 前序遍历
myQueue = []
node = self.root
while myQueue or node:
while node:
myQueue.append(node)
node = node.left
node = myQueue.pop()
print(node)
node = node.right
def queue_mid(self): # 中序遍历
myQueue = []
node = self.root
try:
while True:
if node: # 如果当前node非 None
myQueue.append(node)
print(node)
node = node.left
else: # 如果当前是None ,弹出
node = myQueue.pop().right
except:
print('done')
def post_traversal(self): # 后序遍历非递归 用三个栈存储左右根
leftl, rootl, rightl = [], [self.root], []
while True:
cur = rootl[-1]
if cur.left is not None:
leftl.append(cur.left)
if cur.right is not None:
rightl.append(cur.right)
if rightl:
rootl.append(rightl.pop())
continue
if leftl:
rootl.append(leftl.pop())
continue
if len(leftl) == len(rightl) == 0:
print([x.val for x in rootl[::-1]])
return
''' 后序打印二叉树(非递归)
2 # 使用两个栈结构
3 # 第一个栈进栈顺序:左节点->右节点->跟节点
4 # 第一个栈弹出顺序: 跟节点->右节点->左节点(先序遍历栈弹出顺序:跟->左->右)
5 # 第二个栈存储为第一个栈的每个弹出依次进栈
6 # 最后第二个栈依次出栈
7 def postOrderTraverse(node):
8 stack = [node]
9 stack2 = []
10 while len(stack) > 0:
11 node = stack.pop()
12 stack2.append(node)
13 if node.left is not None:
14 stack.append(node.left)
15 if node.right is not None:
16 stack.append(node.right)
17 while len(stack2) > 0:
18 print(stack2.pop().val)'''
二叉树实现+前中后序遍历(递归+非递归实现)
猜你喜欢
转载自blog.csdn.net/qq_32835305/article/details/86560052
今日推荐
周排行