The Islamia University of Bahawalpur
Faculty of Engineering
BS Cyber Security and Digital Forensics 3rd semester
Lab # 7 Binary Tree Traversals
Course: Data Structures and Algorithms lab Date:
Submitted by: Muhammad zameer malik Roll No. F23BINCE1M04084
Objectives
The purpose of this lab session is to understand the implementation of traversal algorithms for binary
trees.
Introduction
The binary tree – a tree in which each node has at most two descendants – is very often encountered in
applications. The two descendants are usually called the left and right children.
Figure 1: Binary Tree
Binary Tree Traversals
There are three kinds of traversals: preorder, inorder, and postorder.
Preorder Traversal Algorithm:
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Inorder Traversal Algorithm:
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Postorder Traversal Algorithm:
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
Tasks:
1. Write a C++ program to create a binary tree and traverse the binary tree in
a) Preorder
b) Inorder and
c) Postorder
code : #include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
Node* newNode(int data) {
Node* node = new Node();
node->data = data;
node->left = nullptr;
node->right = nullptr;
return node;
}
void preorderTraversal(Node* node) {
if (node == nullptr) {
return;
}
cout << node->data << " ";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
void inorderTraversal(Node* node) {
if (node == nullptr) {
return;
}
inorderTraversal(node->left);
cout << node->data << " ";
inorderTraversal(node->right);
}
void postorderTraversal(Node* node) {
if (node == nullptr) {
return;
}
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << " ";
}
int main() {
// Create the root node
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
cout << "Preorder traversal: ";
preorderTraversal(root);
cout << endl;
cout << "Inorder traversal: ";
inorderTraversal(root);
cout << endl;
cout << "Postorder traversal: ";
postorderTraversal(root);
cout << endl;
return 0;
}
Output:
2: Write C++ program to insert your contact number in such a way that inorder traversal of binary search tree gives
your exact contact number. Also draw its logical representation.
Code: #include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
Node* newNode(int data) {
Node* node = new Node();
node->data = data;
node->left = nullptr;
node->right = nullptr;
return node;
}
Node* insert(Node* root, int data) {
if (root == nullptr) {
return newNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
void inorderTraversal(Node* root) {
if (root == nullptr) {
return;
}
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}
int main() {
Node* root = nullptr;
int contactNumber[] = {7, 4, 2, 5, 9, 3, 6, 1, 8, 0};
int n = sizeof(contactNumber) / sizeof(contactNumber[0]);
for (int i = 0; i < n; i++) {
root = insert(root, contactNumber[i]);
}
cout << "Inorder traversal of the BST: ";
inorderTraversal(root);
cout << endl;
return 0;
}
Output:
Logical representation
7
/\
4 9
/\ \
2 5 8
/\ \
1 3 0
\
6