0% found this document useful (0 votes)
21 views4 pages

Tree Programs

The document contains C code snippets for various binary tree operations including counting nodes, finding minimum and maximum elements, mirroring the tree, performing level order traversal, summing leaf nodes, and checking if two trees are identical. Each function is defined with placeholders for the implementation details. The code relies on a 'binary.h' header for struct definitions and function declarations.
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)
21 views4 pages

Tree Programs

The document contains C code snippets for various binary tree operations including counting nodes, finding minimum and maximum elements, mirroring the tree, performing level order traversal, summing leaf nodes, and checking if two trees are identical. Each function is defined with placeholders for the implementation details. The code relies on a 'binary.h' header for struct definitions and function declarations.
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/ 4

Tree Count Nodes

#include "binary.h"

// Count the Nodes

int countNode(struct node * root){

// Fill Your Code

static int count=0;

if(root!=NULL)

countNode(root->left);

count++;

countNode(root->right);

return count;

Tree Minimum Node


#include "binary.h"

// Find Minimum Element

struct node *a=NULL;

struct node *b=NULL;

int getMin(struct node * root){

// Fill Your Code

b=a;

a=root;

if(a==NULL)

return b->data;
getMin(root->left);

Tree Maximum Node


#include "binary.h"

// Find Maximum Element

struct node*a=NULL,*b=NULL;

int getMax(struct node * root){

// Fill Your Code

a=b;

b=root;

if(b==NULL)return a->data;

getMax(root->right);

Tree Mirror
#include "binary.h"

// Convert to Mirror

void mirror(struct node * root){

// Fill Your Code

if(root==NULL)

return;

struct node *a=root->left;

root->left=root->right;

root->right=a;

mirror(root->left);
mirror(root->right);

Tree Levelorder
#include "binary.h"

// Levelorder Operation

int height(struct node * root){

// Fill your code here

if(root==NULL)return 0;

int lh=height(root->left);

int rh=height(root->right);

return (lh>rh)?lh+1:rh+1;

void printLevelOrder(struct node * root,int l){

//Fill your code here

if(root==NULL)return ;

if(l==1)printf("%d ",root->data);

else if(l>1){

printLevelOrder(root->left,l-1);

printLevelOrder(root->right,l-1);

void levelorder(struct node * root){

//Fill your code here


int h=height(root);

for(int i=1;i<=h;i++)printLevelOrder(root,i);

BST - Sum of Leaf Node


#include "binary.h"

// Sum Of Leaf Node

int sumOfLeaf(struct node * root){

// Fill Your Code

if(root==NULL)

return 0;

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

return root->data;

return sumOfLeaf(root->left)+sumOfLeaf(root->right);

BST - Identical
#include "binary.h"

// Two trees identical or not

int isIdentical(struct node * root1, struct node * root2){

// Fill Your Code

if(root1==NULL && root2==NULL)

return 1;

if(root1==NULL || root2==NULL)

return 0;

return (root1->data==root2->data)&& isIdentical(root1->left,root2->left)&& isIdentical(root1-


>right,root2->right);

You might also like