|
| 1 | +/* |
| 2 | + The Left View of a Binary Tree depicts the nodes that are visible |
| 3 | + when the tree is viewed from the left side of it. At every level |
| 4 | + there would be exactly one node that will appear in left view which |
| 5 | + would be the first node of that level. |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <bits/stdc++.h> |
| 9 | +using namespace std; |
| 10 | +#define mkp make_pair |
| 11 | + |
| 12 | +// Node of Binary Tree storing data, level, |
| 13 | +// left and right child information |
| 14 | +struct Node |
| 15 | +{ |
| 16 | + int data; |
| 17 | + int level; |
| 18 | + Node *left, *right; |
| 19 | + Node(int val) |
| 20 | + { |
| 21 | + data = val; |
| 22 | + left = NULL; |
| 23 | + right = NULL; |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +// Function to print Left View of Binary Tree |
| 28 | +void leftView(Node *root) |
| 29 | +{ |
| 30 | + if (root == NULL) |
| 31 | + return; |
| 32 | + |
| 33 | + // initialising variables |
| 34 | + queue<Node *> q; |
| 35 | + q.push(root); |
| 36 | + root -> level = 0; |
| 37 | + map<int, int> mp; |
| 38 | + |
| 39 | + // variable to store level of nodes |
| 40 | + int level; |
| 41 | + |
| 42 | + // asigning level to each node of Binary Tree |
| 43 | + // and replacing nodes of same level in a map with |
| 44 | + // key as level so as to obtain the Left View |
| 45 | + while (!q.empty()) |
| 46 | + { |
| 47 | + // extract the node at the front of queue |
| 48 | + Node *temp = q.front(); |
| 49 | + level = temp -> level; |
| 50 | + |
| 51 | + // make key as level and data as value for map |
| 52 | + if(mp.find(level) == mp.end()) |
| 53 | + mp[level] = temp -> data; |
| 54 | + |
| 55 | + // remove the extract node from queue |
| 56 | + q.pop(); |
| 57 | + |
| 58 | + // when left child exists, assign level to it, |
| 59 | + // and push it to the queue |
| 60 | + if (temp -> left != NULL) |
| 61 | + { |
| 62 | + temp -> left -> level = level + 1; |
| 63 | + q.push(temp -> left); |
| 64 | + } |
| 65 | + |
| 66 | + // when right child exists, assign level to it, |
| 67 | + // and push it to the queue |
| 68 | + if (temp -> right != NULL) |
| 69 | + { |
| 70 | + temp -> right -> level = level + 1; |
| 71 | + q.push(temp -> right); |
| 72 | + } |
| 73 | + } |
| 74 | + /* |
| 75 | + Map mp contains: |
| 76 | + [0] : {1} |
| 77 | + [1] : {2} |
| 78 | + [2] : {4} |
| 79 | + [3] : {8} |
| 80 | + */ |
| 81 | + |
| 82 | + cout << "Left View of Binary Tree: " << endl; |
| 83 | + map<int, int> :: iterator it; |
| 84 | + // Iterate over the map keys i.e 0, 1, 2, 3 |
| 85 | + for (it = mp.begin(); it != mp.end(); it++) |
| 86 | + cout << it -> second << " "; |
| 87 | +} |
| 88 | + |
| 89 | +// Driver Function |
| 90 | +int main() |
| 91 | +{ |
| 92 | + map<int, Node *> m; |
| 93 | + // Input number of edges |
| 94 | + int n; |
| 95 | + cin >> n; |
| 96 | + Node *root = NULL; |
| 97 | + |
| 98 | + /* |
| 99 | + Input Format: |
| 100 | + 3 |
| 101 | + 1 2 L |
| 102 | + 1 3 R |
| 103 | + 2 4 L |
| 104 | + This means there are 3 edges |
| 105 | + 2 is the left child of 1, |
| 106 | + 3 is the right child of 1, |
| 107 | + 4 is the left child of 2. |
| 108 | + */ |
| 109 | + for (int i = 0; i < n; i++) |
| 110 | + { |
| 111 | + int node1, node2; |
| 112 | + char direction; |
| 113 | + cin >> node1 >> node2 >> direction; |
| 114 | + Node *parent, *child; |
| 115 | + if (m.find(node1) == m.end()) |
| 116 | + { |
| 117 | + parent = new Node(node1); |
| 118 | + m[node1] = parent; |
| 119 | + if (root == NULL) |
| 120 | + root = parent; |
| 121 | + } |
| 122 | + else |
| 123 | + parent = m[node1]; |
| 124 | + child = new Node(node2); |
| 125 | + if (direction == 'L') |
| 126 | + parent -> left = child; |
| 127 | + else |
| 128 | + parent -> right = child; |
| 129 | + m[node2] = child; |
| 130 | + } |
| 131 | + |
| 132 | + // call to leftView function |
| 133 | + leftView(root); |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +/* |
| 138 | + Input: |
| 139 | + 8 |
| 140 | + 1 2 L |
| 141 | + 1 3 R |
| 142 | + 2 4 L |
| 143 | + 2 5 R |
| 144 | + 3 6 L |
| 145 | + 3 7 R |
| 146 | + 5 8 L |
| 147 | + 6 9 R |
| 148 | + |
| 149 | + 1 |
| 150 | + / \ |
| 151 | + 2 3 |
| 152 | + / \ / \ |
| 153 | + 4 5 6 7 |
| 154 | + / \ |
| 155 | + / \ |
| 156 | + 8 9 |
| 157 | +
|
| 158 | + Output: |
| 159 | + Left View of Binary Tree: |
| 160 | + 1 2 4 8 |
| 161 | +*/ |
0 commit comments