0% found this document useful (0 votes)
7 views6 pages

AP (Exp6) Mrigaank

Uploaded by

MRIGAANK JASWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

AP (Exp6) Mrigaank

Uploaded by

MRIGAANK JASWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 6 (A)

Student Name: Mrigaank Jaswal UID: 22BCS14681


Branch: CSE Section/Group: KRG_IOT-2B
Semester: 5 Date of Performance: 13-09-2024
Subject Name: APLab-1 Subject Code: 22CSP-314

1. Title: Tree : Top View.

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

Print the Top View:


• Traverse the map in order of horizontal distances and print the
corresponding node values.

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) {}
};

void topView(Node* root) {


if (!root) return;
map<int, int> topNodes; // Map for horizontal distances
queue<pair<Node*, int>> q; // Queue for BFS
q.push({root, 0}); // Start with root at HD = 0

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

if (curr->left) q.push({curr->left, hd - 1});


if (curr->right) q.push({curr->right, hd + 1});
}
for (auto it : topNodes) // Print top view
cout << it.second << " ";
cout << endl;
}

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.

8. Time Complexity: O(n)

9. Space Complexity: O(n)


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 5 (B)

1. Title: Binary Search Tree Insertion.

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) {}
};

Node* insert(Node* root, int val) {


if (!root) return new Node(val);
if (val < root->val)
root->left = insert(root->left, val);
else if (val > root->val)
root->right = insert(root->right, val);
return root;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

void preOrder(Node* root) {


if (!root) return;
cout << root->val << " ";
preOrder(root->left);
preOrder(root->right);
}

int main() {
int n, val;
cin >> n; // Read the number of elements
Node* root = nullptr;

while (n-- && cin >> val) {


root = insert(root, val); // Insert each value into the BST
}

preOrder(root); // Perform preorder traversal


cout << endl; // Print a newline at the end

return 0;
}
6. Output:

7. Learning Outcomes:

• Learn to insert values into a Binary Search Tree.


• Practice performing a preorder tree traversal (root, left, right).
• Apply recursion for tree operations like insertion and traversal.

8. Time Complexity: O(n*logn)

9. Space Complexity: O(n)

You might also like