Ads La2 - Binary Tree Node Functions
Ads La2 - Binary Tree Node Functions
Name: Om Lohade
Class: IT – C
PRN: 12320123
Roll no.: SEDA-3
C code for creating BT/BST and inserting element of user choice, finding height,
count nodes, leaf nodes, minimum and maximum of nodes, mirror image, print
leaf nodes, print nodes level wise.
Explanation:
1. Height of Tree (height):
This function calculates the height of the binary tree, i.e., the maximum depth
of the tree. It recursively finds the height of the left and right subtrees and
returns the maximum of the two plus one.
2. Count Nodes (countnodes):
This function recursively counts the total number of nodes in the binary tree. It
increments the count for each non-null node while traversing the tree.
3. Count Leaf Nodes (getLeafCount):
The function recursively counts the number of leaf nodes in the binary tree. A
leaf node is a node without any children (both left and right pointers are NULL).
4. Find Maximum and Minimum (findMax, findMin):
These functions find the maximum and minimum elements, respectively, in the
binary tree using a recursive approach. The maximum and minimum are
calculated by recursively finding the maximum and minimum values in the left
and right subtrees and comparing them with the current node's value.
5. Print Leaf Nodes (printLeafNodes):
This function prints the leaf nodes of the binary tree. It recursively traverses
the tree and prints the value of each leaf node.
6. Print Nodes Level-wise (levelOrderTraversal):
This function prints the nodes of the binary tree level-wise. It uses a recursive
helper function (printNodesAtLevel) to print nodes at a specific level for each
level of the tree.
7. Create Mirror Image (getMirrorBinaryTree):
The function creates a mirror image of the binary tree using recursion. It
recursively swaps the left and right subtrees for each node, effectively
mirroring the tree.
8. Inorder Traversal (inOrderTraversal):
This function performs an inorder traversal of the binary tree. It recursively
visits the left subtree, processes the current node, and then recursively visits
the right subtree.
Code:
#include <malloc.h>
#include <limits.h>
#include <stdio.h>
int H;
struct node
int data;
};
struct node *createnode(int data)
n->data = data;
n->left=NULL;
n->right=NULL;
return n;
if (node == NULL)
return 0;
else
else
if(root != NULL)
{
countnodes(root->left);
count++;
countnodes(root->right);
return count;
if(node == NULL)
return 0;
return 1;
else
return getLeafCount(node->left)+
getLeafCount(node->right);
if (root == NULL)
return INT_MIN;
res = lres;
res = rres;
return res;
}
if (root == NULL)
return INT_MAX;
res = lres;
res = rres;
return res;
if (root==NULL)
return;
return;
if (root->left!=NULL)
printLeafNodes(root->left);
if (root->right!=NULL)
printLeafNodes(root->right);
}
void printNodesAtLevel(struct node* root, int currentLevel, int level)
if(root == NULL)
return;
if(currentLevel == level)
return;
printNodesAtLevel(root, 0, i);
printf("\n");
if(root == NULL)
return NULL;
newNode->right = getMirrorBinaryTree(root->left);
newNode->left = getMirrorBinaryTree(root->right);
return newNode;
if(nodeptr != NULL)
inOrderTraversal(nodeptr->left);
inOrderTraversal(nodeptr->right);
int main()
int a[5];
printf("Enter 5 integers:");
scanf("%d", &a[i]);
p0->left=p1;
p0->right=p2;
p1->left=p3;
p1->right=p4;
printLeafNodes(p0);
levelOrderTraversal(p0);
inOrderTraversal(p0);
inOrderTraversal(mirror);
return 0;