-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBinary_Tree_Left_View.py
135 lines (118 loc) · 2.97 KB
/
Binary_Tree_Left_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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'''
The Left View of a Binary Tree depicts the nodes that are visible
when the tree is viewed from the left side of it. At every level
there would be exactly one node that will appear in left view which
would be the first node of that level.
'''
import queue
# Node of Binary Tree storing data, level,
# left and right child information
class Node:
def __init__(self, val):
self.data = val
self.level = 0
self.left = None
self.right = None
# Function to print Left View of Binary Tree
def leftView(root):
if root is None:
return
# initialising variables
q = queue.Queue()
q.put(root)
root.level = 0
mp = {}
# variable to store level of nodes
level = 0
# asigning level to each node of Binary Tree
# storing first node of same level in a map
# with key as level
while (not q.empty()):
# extract and remove the node from the front of queue
temp = q.get()
level = temp.level
# make key as level and data as value for map
if level not in mp:
mp[level] = temp.data
# when left child exists, assign level to it
# and push it to the queue
if temp.left is not None:
temp.left.level = level + 1
q.put(temp.left)
# when right child exists, assign level to it
# and push it to the queue
if temp.right is not None:
temp.right.level = level + 1
q.put(temp.right)
'''
Map mp contains:
[0] -> 1
[1] -> 2
[2] -> 4
[3] -> 8
'''
print("Left View of Binary Tree: ")
# Iterate over the map keys i.e 0, 1, 2, 3
for key, value in enumerate(sorted(mp)):
print(mp[value], end = " ")
# Driver Code
m = {}
# Input number of edges
n = int(input())
root = None
'''
The input format is that we get node1 which is the parent,
node2 which is the child, and then direction 'L' or 'R' which
tells us whether it is a left or right child of node1. Example:
Input:
3
1 2 L
1 3 R
2 4 L
This means there are 3 edges
2 is the left child of 1,
3 is the right child of 1,
4 is the left child of 2.
'''
for i in range(0, n):
node1, node2, direction = input().split(' ')
node1 = int(node1)
node2 = int(node2)
if node1 not in m:
parent = Node(node1)
m[node1] = parent
if root == None:
root = parent
else:
parent = m[node1]
child = Node(node2)
if direction == 'L':
parent.left = child
else:
parent.right = child
m[node2] = child
# call to leftView function
leftView(root)
'''
Input:
8
1 2 L
1 3 R
2 4 L
2 5 R
3 6 L
3 7 R
5 8 L
6 9 R
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
/ \
8 9
Output:
Left View of Binary Tree:
1 2 4 8
'''