0% found this document useful (0 votes)
20 views9 pages

Ads La2 - Binary Tree Node Functions

The document provides a C code implementation for various binary tree operations, including creating a binary tree, inserting elements, calculating height, counting nodes and leaf nodes, finding minimum and maximum values, printing leaf nodes, and generating a mirror image of the tree. It includes detailed explanations of each function and their recursive logic. The main function demonstrates the usage of these operations with user input for tree construction.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Ads La2 - Binary Tree Node Functions

The document provides a C code implementation for various binary tree operations, including creating a binary tree, inserting elements, calculating height, counting nodes and leaf nodes, finding minimum and maximum values, printing leaf nodes, and generating a mirror image of the tree. It includes detailed explanations of each function and their recursive logic. The main function demonstrates the usage of these operations with user input for tree construction.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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 *left;

struct node *right;

};
struct node *createnode(int data)

struct node *n;

n = (struct node*)malloc(sizeof(struct node));

n->data = data;

n->left=NULL;

n->right=NULL;

return n;

int height (struct node *node)

if (node == NULL)

return 0;

else

int leftHeight = height (node->left);

int rightHeight = height (node->right);

if (leftHeight > rightHeight)

return (leftHeight + 1);

else

return (rightHeight + 1);

static int count = 0;

int countnodes(struct node *root)

if(root != NULL)
{

countnodes(root->left);

count++;

countnodes(root->right);

return count;

unsigned int getLeafCount(struct node* node)

if(node == NULL)

return 0;

if(node->left == NULL && node->right==NULL)

return 1;

else

return getLeafCount(node->left)+

getLeafCount(node->right);

int findMax(struct node* root)

if (root == NULL)

return INT_MIN;

int res = root->data;

int lres = findMax(root->left);

int rres = findMax(root->right);

if (lres > res)

res = lres;

if (rres > res)

res = rres;

return res;
}

int findMin(struct node* root)

if (root == NULL)

return INT_MAX;

int res = root->data;

int lres = findMin(root->left);

int rres = findMin(root->right);

if (lres < res)

res = lres;

if (rres < res)

res = rres;

return res;

void printLeafNodes(struct node *root)

if (root==NULL)

return;

if (root->left==NULL && root->right==NULL)

printf("%d ", root->data);

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)

printf(" %d ", root->data);

return;

printNodesAtLevel(root->left, currentLevel+1, level);

printNodesAtLevel(root->right, currentLevel+1, level);

void levelOrderTraversal(struct node* root)

for(int i = 0; i < H; i++)

printf("%d. ", i+1);

printNodesAtLevel(root, 0, i);

printf("\n");

struct node* getMirrorBinaryTree(struct node *root)

if(root == NULL)

return NULL;

struct node* newNode = createnode(root->data);

newNode->right = getMirrorBinaryTree(root->left);
newNode->left = getMirrorBinaryTree(root->right);

return newNode;

void inOrderTraversal(struct node *nodeptr)

if(nodeptr != NULL)

inOrderTraversal(nodeptr->left);

printf("%d ", nodeptr->data);

inOrderTraversal(nodeptr->right);

int main()

int a[5];

printf("Enter 5 integers:");

for(int i=0; i<5;i++)

scanf("%d", &a[i]);

printf("\nYour tree looks like:\n\n %d \n %d %d \n %d %d", a[0],a[1],a[2],a[3],a[4]);

struct node *p0=createnode(a[0]);

struct node *p1=createnode(a[1]);

struct node *p2=createnode(a[2]);

struct node *p3=createnode(a[3]);

struct node *p4=createnode(a[4]);

p0->left=p1;

p0->right=p2;

p1->left=p3;
p1->right=p4;

printf ("\n\nHeight of tree: %d", H=height(p0));

printf("\nNumber of nodes: %d", countnodes(p0));

printf("\nNumber of leaf nodes: %d", getLeafCount(p0));

printf("\nMaximum element is: %d", findMax(p0));

printf("\nMinimum element is: %d", findMin(p0));

printf("\nLeaf nodes are: ");

printLeafNodes(p0);

printf("\n Nodes as per their level:\n");

levelOrderTraversal(p0);

struct node *mirror = getMirrorBinaryTree(p0);

printf("Normal tree inorder form: ");

inOrderTraversal(p0);

printf("\nMirror tree inorder form: ");

inOrderTraversal(mirror);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy