mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Adding function for actual level order traversal (#699)
This commit is contained in:
parent
301493094e
commit
6f283336ae
@ -2,6 +2,7 @@
|
|||||||
This is pure python implementation of tree traversal algorithms
|
This is pure python implementation of tree traversal algorithms
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import queue
|
import queue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -46,6 +47,7 @@ def build_tree():
|
|||||||
node_found.right = right_node
|
node_found.right = right_node
|
||||||
q.put(right_node)
|
q.put(right_node)
|
||||||
|
|
||||||
|
|
||||||
def pre_order(node):
|
def pre_order(node):
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
@ -53,6 +55,7 @@ def pre_order(node):
|
|||||||
pre_order(node.left)
|
pre_order(node.left)
|
||||||
pre_order(node.right)
|
pre_order(node.right)
|
||||||
|
|
||||||
|
|
||||||
def in_order(node):
|
def in_order(node):
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
@ -82,22 +85,43 @@ def level_order(node):
|
|||||||
if node_dequeued.right:
|
if node_dequeued.right:
|
||||||
q.put(node_dequeued.right)
|
q.put(node_dequeued.right)
|
||||||
|
|
||||||
#iteration version
|
|
||||||
|
def level_order_actual(node):
|
||||||
|
if not isinstance(node, TreeNode) or not node:
|
||||||
|
return
|
||||||
|
q = queue.Queue()
|
||||||
|
q.put(node)
|
||||||
|
while not q.empty():
|
||||||
|
list = []
|
||||||
|
while not q.empty():
|
||||||
|
node_dequeued = q.get()
|
||||||
|
print(node_dequeued.data, end=" ")
|
||||||
|
if node_dequeued.left:
|
||||||
|
list.append(node_dequeued.left)
|
||||||
|
if node_dequeued.right:
|
||||||
|
list.append(node_dequeued.right)
|
||||||
|
print()
|
||||||
|
for node in list:
|
||||||
|
q.put(node)
|
||||||
|
|
||||||
|
|
||||||
|
# iteration version
|
||||||
def pre_order_iter(node):
|
def pre_order_iter(node):
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
stack = []
|
stack = []
|
||||||
n = node
|
n = node
|
||||||
while n or stack:
|
while n or stack:
|
||||||
while n: #start from root node, find its left child
|
while n: # start from root node, find its left child
|
||||||
print(n.data, end=" ")
|
print(n.data, end=" ")
|
||||||
stack.append(n)
|
stack.append(n)
|
||||||
n = n.left
|
n = n.left
|
||||||
#end of while means current node doesn't have left child
|
# end of while means current node doesn't have left child
|
||||||
n = stack.pop()
|
n = stack.pop()
|
||||||
#start to traverse its right child
|
# start to traverse its right child
|
||||||
n = n.right
|
n = n.right
|
||||||
|
|
||||||
|
|
||||||
def in_order_iter(node):
|
def in_order_iter(node):
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
@ -111,22 +135,24 @@ def in_order_iter(node):
|
|||||||
print(n.data, end=" ")
|
print(n.data, end=" ")
|
||||||
n = n.right
|
n = n.right
|
||||||
|
|
||||||
|
|
||||||
def post_order_iter(node):
|
def post_order_iter(node):
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
stack1, stack2 = [], []
|
stack1, stack2 = [], []
|
||||||
n = node
|
n = node
|
||||||
stack1.append(n)
|
stack1.append(n)
|
||||||
while stack1: #to find the reversed order of post order, store it in stack2
|
while stack1: # to find the reversed order of post order, store it in stack2
|
||||||
n = stack1.pop()
|
n = stack1.pop()
|
||||||
if n.left:
|
if n.left:
|
||||||
stack1.append(n.left)
|
stack1.append(n.left)
|
||||||
if n.right:
|
if n.right:
|
||||||
stack1.append(n.right)
|
stack1.append(n.right)
|
||||||
stack2.append(n)
|
stack2.append(n)
|
||||||
while stack2: #pop up from stack2 will be the post order
|
while stack2: # pop up from stack2 will be the post order
|
||||||
print(stack2.pop().data, end=" ")
|
print(stack2.pop().data, end=" ")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print("\n********* Binary Tree Traversals ************\n")
|
print("\n********* Binary Tree Traversals ************\n")
|
||||||
|
|
||||||
@ -147,6 +173,10 @@ if __name__ == '__main__':
|
|||||||
level_order(node)
|
level_order(node)
|
||||||
print("\n******************************************\n")
|
print("\n******************************************\n")
|
||||||
|
|
||||||
|
print("\n********* Actual Level Order Traversal ************")
|
||||||
|
level_order_actual(node)
|
||||||
|
print("\n******************************************\n")
|
||||||
|
|
||||||
print("\n********* Pre Order Traversal - Iteration Version ************")
|
print("\n********* Pre Order Traversal - Iteration Version ************")
|
||||||
pre_order_iter(node)
|
pre_order_iter(node)
|
||||||
print("\n******************************************\n")
|
print("\n******************************************\n")
|
||||||
|
Loading…
Reference in New Issue
Block a user