Program No.
06
Assigned On: - 28.03.2025
Evaluated On: - 03.04.2025
Submitted By:
Name: - Arham Usmani
Roll No. 23DCS006
Computer Engineering
University Polytechnic
Faculty of Engineering and Technology
Jamia Millia Islamia
1. About the Program
Q. Write a program in c to check whether the elements of array entered are of
binary tree or binary search tree.
2. Algorithm
Input: Array arr, size.
Binary Tree Check: Always return "Binary Tree" (any array is a binary tree in
level-order representation).
Binary Search Tree Check:
o If size <= 1, return "Binary Search Tree".
o For each "parent" node in the array:
o Check if its left child (if exists) is smaller than the parent. If not, return "Not
Binary Search Tree".
o Check if its right child (if exists) is larger than the parent. If not, return "Not
Binary Search Tree".
o If all checks pass, return "Binary Search Tree".
Output: Print whether the array is a Binary Tree and/or a Binary Search Tree
based on the checks.
[Link]: -
#include <stdio.h>
#include <stdbool.h>
bool isBinaryTree (int arr [], int size) {
if (size <= 0) {
return true;
}
return true;
}
bool isBinarySearchTree (int arr [], int size) {
if (size <= 1) {
return true;
}
for (int i = 0; i < size / 2; i++) {
int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;
if (leftChild < size && arr[leftChild] > arr[i]) {
return false;
}
if (rightChild < size && arr[rightChild] < arr[i]) {
return false;
}
}
return true;
}
int main() {
int arr[] = {4, 2, 5, 1, 3};
int size = sizeof(arr) / sizeof(arr[0]);
if (isBinaryTree(arr, size)) {
printf("Given array represents a Binary Tree\n");
} else {
printf("Given array does not represent a Binary Tree\n");
}
if (isBinarySearchTree(arr, size)) {
printf("Given array represents a Binary Search Tree\n");
} else {
printf("Given array does not represent a Binary Search Tree\n");
}
return 0;
}
[Link]: -