Prasad Gade PR-5
Prasad Gade PR-5
UID: 2024301006
Exp.No.: 5
Problem Statement:
1-Creation of binary tree and display using any one traversal
2- counting no. of nodes in a binary tree
3- counting no of leaf nodes in a binary tree
4- counting height of a given node in a binary tree
5- create an Arithmetic expression tree from given postfix expression
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
struct Node *findNode(struct Node *root, int value) // finds the node whose height is to be
found
{
if (root == NULL)
return NULL;
if (root->data == value)
return root;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int totalNodes = sizeof(arr) / sizeof(arr[0]);
printf("Preorder : ");
preorderTraversal(root);
printf("\n");
printf("Postorder : ");
postorderTraversal(root);
printf("\n");
printf("Inorder : ");
inorderTraversal(root);
printf("\n");
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
int calcHeightOf = 1;
printf("Total number of nodes: %d\n", countNodes(root));
printf("Total number of leaf nodes: %d\n", countLeafNodes(root));
printf("Height of the %d: %d\n", calcHeightOf, calculateHeightOfNode(root, calcHeightOf));
return 0;
}
//POST ORDER TRAVERSAL TO EXPRESSION TREE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct Node
{
char data;
struct Node *lchild;
struct Node *rchild;
};
struct Stack
{
int top;
int size;
struct Node **array;
};
int main()
{
char postfix[] = "AB+C*DE/F+G*H-/";
printf("In-order : ");
inOrderTraversal(root);
printf("\n");
printf("Pre-order : ");
preOrderTraversal(root);
printf("\n");
printf("Post-order : ");
postOrderTraversal(root);
printf("\n");
return 0;
}
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
Output:
Created a binary tree with 15 nodes for this example and performed all traversals on it, counted
the total number of nodes, counted number of leaf nodes, found height of any given node.
Handwritten Assignment:
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
Conclusion:
Implemented Binary Tree data structure and performed operations such as displaying
preorder traversal, counting the number of nodes in the tree, counting no of leaf nodes in the
tree, counting the height of the any node of the tree. Created expression tree from a given
post-order traversal.