AP (Exp6) Mrigaank
AP (Exp6) Mrigaank
Experiment 6 (A)
2. Aim: Given a pointer to the root of a binary tree, print the top view of the binary
tree. The tree as seen from the top the nodes, is called the top view of the tree.
3. Objective: To print the top view of a binary tree by identifying and displaying
the nodes visible when viewed from above, using level-order traversal and
tracking horizontal distances.
4. Algorithm:
Initialization:
• Use a queue to perform level-order traversal (BFS).
• Use a map (or dictionary) to store the first node encountered at each
horizontal distance.
Enqueue Root:
• Start by enqueuing the root node with a horizontal distance of 0.
Level-order Traversal:
• While the queue is not empty:
o Dequeue a node and check its horizontal distance (HD).
o If the HD is not in the map, store the node's value at that HD (since it's
the first node encountered at that distance).
o Enqueue the left child of the current node with HD - 1.
o Enqueue the right child of the current node with HD + 1.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Implementation/Code
#include <iostream>
#include <map>
#include <queue>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(NULL), right(NULL) {}
};
while (!q.empty()) {
auto [curr, hd] = q.front();
q.pop();
if (topNodes.find(hd) == topNodes.end()) // First node at this HD
topNodes[hd] = curr->data;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
root->right->right = new Node(7);
cout << "Top view of the binary tree: ";
topView(root);
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Output:
7. Learning Outcomes:
• Learn the structure of a binary tree and how nodes are organized
hierarchically.
• Gain familiarity with level-order traversal (BFS) using a queue to
explore nodes level by level.
• Understand how to use a map (or dictionary) to store key-value pairs,
specifically to track the first node at each horizontal distance.
Experiment 5 (B)
2. Aim: You are given a pointer to the root of a binary search tree and values to be
inserted into the tree. Insert the values into their appropriate position in the binary
search tree and return the root of the updated binary tree. You just have to complete the
function.
3. Objective: Return the root of the binary search tree after inserting the value into the
tree.
4. Algorithm:
1. Insert each value into the binary search tree at its correct position.
2. Create a new node if the position in the tree is empty.
3. Traverse the tree in pre-order (root, left, right) to get the output.
4. Handle all input values, building and then printing the tree.
5. Implementation/Code
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
Node *left, *right;
Node(int v) : val(v), left(nullptr), right(nullptr) {}
};
int main() {
int n, val;
cin >> n; // Read the number of elements
Node* root = nullptr;
return 0;
}
6. Output:
7. Learning Outcomes: