forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Binary_tree_post_order.py
117 lines (84 loc) · 2.95 KB
/
Binary_tree_post_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Post Order Binary tree traversal follows the pattern:
Left Child -> Right Child -> Root
"""
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class Tree:
def __init__(self):
self.root = None
# Function to insert nodes into tree
def insert(self, array_of_nodes):
# Queue to create the tree
queue = []
self.root = Node(int(array_of_nodes.pop(0)))
queue.append(self.root)
size_of_queue = len(queue)
# Logic for nodes insertion
while array_of_nodes:
while size_of_queue:
root = queue.pop(0)
if root:
value = array_of_nodes.pop(0)
if value == 'null':
root.left = None
else:
root.left = Node(int(value))
queue.append(root.left)
value = array_of_nodes.pop(0)
if value == 'null':
root.right = None
else:
root.right = Node(int(value))
queue.append(root.right)
size_of_queue -= 1
size_of_queue = len(queue)
# Function to perform Post order traversal on our tree
def postorder_traversal(self, root):
if not root:
return []
# This will be our main list that contains the traversed tree
self.traversed = []
# This list will help in the creation of traversed list
helper_stack = [root]
# Keeping track of visited nodes
left_visited_nodes = set()
right_visited_nodes = set()
# Main logic for post order traversal
while helper_stack:
# Temporary list of nodes to check visited nodes
temp = helper_stack[-1]
# Checking left childs
while temp.left and temp not in left_visited_nodes:
helper_stack.append(temp.left)
left_visited_nodes.add(temp)
temp = temp.left
# Checking right childs
if temp.right and temp not in right_visited_nodes:
right_visited_nodes.add(temp)
helper_stack.append(temp.right)
temp = temp.right
else:
temp = helper_stack.pop()
self.traversed.append(temp.value)
return self.traversed
def main():
user_defined_tree = list(
input("Enter the elements of your tree: ").split(','))
postorder_tree = Tree()
postorder_tree.insert(user_defined_tree)
print("Your post order traversed tree:")
# Final traversed tree
tree = postorder_tree.postorder_traversal(postorder_tree.root)
print(tree)
main()
# Sample Input - Output:
# First sample:
# Input: 1,2,3,null,null,4,5
#Output: [2,4,5,3,1]
# Second sample:
# Input: 2,3,4,5,6,1,7
# Output: 5,6,3,1,7,4,2