-
Notifications
You must be signed in to change notification settings - Fork 39
/
tree_inorder_traversal_iterative.py
89 lines (77 loc) · 2.01 KB
/
tree_inorder_traversal_iterative.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
#!/usr/bin/python
# Date: 2018-11-24
#
# Description:
# Write an iterative program to do inorder traversal of binary tree.
#
# Motivation:
# Recursive programs may cause stack overflow if recursion depth reaches a
# certain limit.
#
# Approach:
# Take a stack and push all nodes traversing left nodes once we reach leaf print
# that node and traverse right sub tree till node.
#
# Reference:
# https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
#
# Complexity:
# O(N) Time and Space
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def inorder(root):
stack = []
current = root
while True:
if current is not None: # Traverse whole left sub tree
stack.append(current)
current = current.left
else:
if not stack: # current is None and stack is empty - BREAK
break
else:
current = stack.pop()
print(current.data, end=' ')
current = current.right # We are done with left and root, traverse right sub tree
print()
# Above implementation also works but this is simpler than above function
def inorder_simple(root):
node = root
stack = []
while True:
while node:
stack.append(node)
node = node.left
if not stack:
break
node = stack.pop()
print(node.data, end=' ')
node = node.right
print()
def inorder_simple2(root):
stack = []
node = root
while node or stack:
while node:
stack.append(node)
node = node.left
node = stack.pop()
print(node.data, end=' ')
node = node.right
print()
def main():
root = Node(10)
root.left = Node(5)
root.left.left = Node(3)
root.left.right = Node(8)
root.right = Node(15)
root.right.left = Node(12)
root.right.right = Node(18)
inorder(root) # 3 5 8 10 12 15 18
inorder_simple(root) # 3 5 8 10 12 15 18
inorder_simple2(root) # 3 5 8 10 12 15 18
if __name__ == '__main__':
main()