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

Computer Science & Engineering Computer Science & Engineering

The student implemented two tree data structure concepts - printing the top view of a binary tree and performing an inorder traversal of a tree. For the top view problem, the student used a map and queue to store the horizontal distance and node value. For inorder traversal, they recursively traversed the left subtree, visited the root, and then the right subtree. Both implementations produced the expected output. The experiment helped the student learn about tree data structures and their applications in representing relationships between objects.

Uploaded by

lostinspace981
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)
26 views6 pages

Computer Science & Engineering Computer Science & Engineering

The student implemented two tree data structure concepts - printing the top view of a binary tree and performing an inorder traversal of a tree. For the top view problem, the student used a map and queue to store the horizontal distance and node value. For inorder traversal, they recursively traversed the left subtree, visited the root, and then the right subtree. Both implementations produced the expected output. The experiment helped the student learn about tree data structures and their applications in representing relationships between objects.

Uploaded by

lostinspace981
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 2.2

Student Name: Aditya Singh UID: 21BCS2466


Branch: CSE Section/Group: 611/B
Semester: 5th Date of Performance: 28/09/23
Subject Name: Advance Programming Lab Subject Code: 21CSP-314

1. Aim: To implement the concept of Tree Data Structure.

2. Objective: a) Given a pointer to the root of a binary tree, print


the top view of the binary tree.

b) Implement inorder traversal of a tree.

3. DBMS script and output:


a)
#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

void topView(TreeNode* root) {


if (!root) return;

map<int, int> topViewMap;


queue<pair<TreeNode*, int>> q;

q.push(make_pair(root, 0));

while (!q.empty()) {
TreeNode* node = q.front().first;
int hd = q.front().second;
q.pop();

if (topViewMap.find(hd) == topViewMap.end()) {
topViewMap[hd] = node->val;
}

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

if (node->right) {
q.push(make_pair(node->right, hd + 1));
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for (auto it = topViewMap.begin(); it != topViewMap.end();


++it) {
cout << it->second << " ";
}
cout << endl;
}

int main() {

TreeNode* root = new TreeNode(1);


root->right = new TreeNode(2);
root->right->right = new TreeNode(5);
root->right->right->left = new TreeNode(3);
root->right->right->right = new TreeNode(6);
root->right->right->left->right = new TreeNode(4);

topView(root);

return 0;
}

Result:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

b) #include <bits/stdc++.h>
using namespace std;

class Node {
public:
int data;
Node* left;
Node* right;

Node(int value) : data(value), left(nullptr), right(nullptr) {}


};

class Tree {
public:
Node* create_Node(int data) {
return new Node(data);
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Node* insert(Node* node, int data) {


if (node == nullptr) {
return create_Node(data);
}
if (data < node->data) {
node->left = insert(node->left, data);
}
else if (data > node->data) {
node->right = insert(node->right, data);
}
return node;
}

void inorder_Traversal(Node* root) {


if (root != nullptr) {
inorder_Traversal(root->left);
cout << root->data << " ";
inorder_Traversal(root->right);
}
}
};

int main() {
vector<int> numbers;
int number;
while (cin >> number) {
numbers.push_back(number);
}

Tree tree;
Node* root = tree.create_Node(numbers[0]);
for (size_t i = 1; i < numbers.size(); i++) {
tree.insert(root, numbers[i]);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

tree.inorder_Traversal(root);

return 0;
}

Result:

Learning Outcome
• Learned about trees
• Tree is a fundamental data structure that resembles an upside-
down tree with a single root and branches of child nodes
spreading out from that root.
• Graphs are used to represent relationships and connections
between various objects, and they are a fundamental concept
in various algorithms and applications.

You might also like