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

C Program for Binary Tree Operations

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

C Program for Binary Tree Operations

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include<stdlib.h>
#include <stdio.h>
struct Node
{
struct Node* left;
int data;
struct Node* right;

};

struct Node*newNode(int item)


{
struct Node* temp
= (struct Node*)malloc(sizeof(struct Node));
temp->data= item;
temp->left = temp->right = NULL;
return temp;
}

struct Node*insert(struct Node* root,int key)


{
if (root == NULL)
return newNode(key);

if (key < root->data)


root->left = insert(root->left, key);
else if(key > root->data)
root->right = insert(root->right, key);
return root;
}

int height(struct Node* root)


{
int h;
if(root==NULL)
{
return -1;
}
int lh=height(root->left);
int rh=height(root->right);
if(lh>rh)
{
h=lh+1;
}
else
{
h=rh+1;
}
return h;

}
void ismirror(struct Node* root)
{
if(root==NULL)
return;
ismirror(root->left);
ismirror(root->right);
struct Node* temp=root->right;
root->right=root->left;
root->left=temp;
}

void inorder(struct Node* root)


{
if(root)
{
inorder(root->left);
printf("%d : ",root->data);
inorder(root->right);
}
}
void preorder(struct Node* root)
{
if(root)
{
printf("%d : ",root->data);
preorder(root->left);

preorder(root->right);
}
}
void postorder(struct Node* root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
printf("%d : ",root->data);
}
}
int add(struct Node* root)
{
if(root==NULL)
{
return 0;

}
int lsum=add(root->left);
int rsum=add(root->right);
int sum=lsum+rsum+root->data;
return sum;
}

int sumtree(struct Node* root)


{
if(root->data==add(root->left)+add(root->right))
{
if((sumtree(root->left)&&sumtree(root->right)))
{
return 1;
}
else
{
return 0;
}
}
}
int max(int a,int b)
{
if(a>b)
{
return a;
}
else
return b;
}
int diameter(struct Node* tree)
{
if (tree == NULL)
return 0;
int lheight = height(tree->left);
int rheight = height(tree->right);
int ldiameter = diameter(tree->left);
int rdiameter = diameter(tree->right);
return max((lheight + rheight + 2), max(ldiameter, rdiameter));

int main()
{
struct Node* root = NULL;
root=insert(root,50);
root=insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root,80);
printf("Inorder :");
inorder(root);
printf("\nPreorder :");
preorder(root);
printf("\nPostorder :");
postorder(root);
printf("\nHeight :%d",height(root));
printf("\nDiameter :%d",diameter(root));
if(sumtree(root))
{
printf("\nSum Tree");
}
else
{
printf("\nNot a Sum Tree");
}

ismirror(root);
printf("\nMirror Completed :- Inorder is: ");
inorder(root);
return 0;
}

You might also like