-
Notifications
You must be signed in to change notification settings - Fork 65
/
top_view.py
57 lines (41 loc) · 1.2 KB
/
top_view.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
""" Top View of Binary Tree
"""
from collections import deque
class Node:
""" class representing node in binary tree """
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def top_view(root):
""" top view of binary tree """
if root is None:
return
level_map = {}
# queue of root and it's corresponding horizontal distance
queue = deque([(root, 0)])
while queue:
node, hd = queue.popleft()
if hd not in level_map:
print(node.data, end=" ")
level_map[hd] = node.data
if node.left is not None:
queue.append((node.left, hd-1))
if node.right is not None:
queue.append((node.right, hd+1))
def main():
""" operational function """
root = Node(2)
root.left = Node(7)
root.left.left = Node(2)
root.left.right = Node(6)
root.left.right.left = Node(5)
root.left.right.right = Node(11)
root.right = Node(5)
root.right.right = Node(9)
root.right.right.left = Node(4)
root.right.right.left.right = Node(40)
root.right.right.left.right.right = Node(46)
top_view(root)
if __name__ == "__main__":
main()