-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
zig_zag_traversal.py
89 lines (77 loc) · 2.31 KB
/
zig_zag_traversal.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
# Given a binary Tree
# 1
# / \
# 2 3
# / \ / \
# 4 5 6 7
# Print zig-zag level order traversal
# 1 3 2 4 5 6 7
#
class TreeNode:
def __init__(self, data):
"""Representation of a binary tree node"""
self.data = data
self.left = None
self.right = None
class Tree:
def __init__(self):
"""Reprentation of a binary tree"""
self.root = None
def _print_level(self, node, level, zig):
"""Utility function to print the level, level starts with 1"""
if node is None:
return
if level == 1:
print(node.data, end=' ')
return
else:
if zig:
self._print_level(node.left, level-1, zig)
self._print_level(node.right, level-1, zig)
else:
self._print_level(node.right, level-1, zig)
self._print_level(node.left, level-1, zig)
def _height(self, node):
"""Gets the height of the tree from node"""
if node is None:
return 0
else:
return max(self._height(node.left), self._height(node.right)) + 1
def print_zig_zag(self):
"""Prints the zig zag level order"""
if self.root is None:
return
h = self._height(self.root)
zig = True
for i in range(1, h+1):
self._print_level(self.root, i, not zig)
print()
def in_order(self):
"""In order traversal of the tree"""
current = self.root
stack = []
done = False
while not done:
if current is not None:
stack.append(current)
current = current.left
elif stack:
current = stack.pop()
print(current.data, end=' ')
current = current.right
else:
done = True
print()
if __name__ == "__main__":
tree = Tree()
tree.root = TreeNode(1)
tree.root.left = TreeNode(2)
tree.root.right = TreeNode(3)
tree.root.left.left = TreeNode(4)
tree.root.left.right = TreeNode(5)
tree.root.right.left = TreeNode(6)
tree.root.right.right = TreeNode(7)
print("In order:")
tree.in_order()
print("Zig zag level order:")
tree.print_zig_zag()