-
Notifications
You must be signed in to change notification settings - Fork 40
/
bottom_view_binary_tree.py
100 lines (78 loc) · 2.14 KB
/
bottom_view_binary_tree.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
#!/usr/bin/python
# Date: 2018-11-25
#
# Description:
# Print bottom view of a binary tree.
#
# Approach:
# Combination of vertical level order and horizontal level order traversal is
# used.
# If there is only one node in particular vertical level order print that node
# otherwise if there are multiple nodes in particular vertical level order,
# print that node which comes later in horizontal level order.
#
# To simulate above logic we require both queue and hash-map, queue is used for
# horizontal level order and hash-map is used to store horizontal distance as
# key and node as value.
#
# Reference:
# https://www.youtube.com/watch?v=V7alrvgS5AI
# https://www.geeksforgeeks.org/bottom-view-binary-tree/ Method 1
#
# Complexity:
# O(N) time and space
import collections
class Node:
def __init__(self, k):
self.k = k
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
def insert(self, root, k):
if root is None:
return Node(k)
if root.k > k:
root.left = self.insert(root.left, k)
elif root.k < k:
root.right = self.insert(root.right, k)
else:
print('Duplicates not allowed in BST')
return root
return root
def inorder(self, root):
if root:
self.inorder(root.left)
print(root.k, end=' ')
self.inorder(root.right)
def bottom_view(root):
mp = {}
hd = 0 # Horizontal distance
q = collections.deque([(root, hd)])
while q:
node, hd = q.popleft()
mp[hd] = node.k # Keep on updating till last node in level order
if node.left:
q.append((node.left, hd - 1))
if node.right:
q.append((node.right, hd + 1))
# Print in vertical distance wise
for k in sorted(mp.keys()):
print(mp[k], end=' ')
print()
def main():
bst = BST()
items = [5, 10, 2, 3, 8, 12, 16]
for i in items:
bst.root = bst.insert(bst.root, i)
print('Inorder traversal:', end=' ')
bst.inorder(bst.root)
print('\nBottom view of binary tree:', end=' ')
bottom_view(bst.root)
if __name__ == '__main__':
main()
# Output
# ------
# Inorder traversal: 2 3 5 8 10 12 16
# Bottom view of binary tree: 2 8 10 12 16