You are on page 1of 12

___________________________________________________________________________________

_____________________________________________

Q1...................................................
ANS-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

struct TREE{
int val;
int hei;
struct TREE *left;
struct TREE *right;
};
typedef struct TREE Tree;

int height_update(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
root->hei=1+(l>r?l:r);
return root->hei;
}

Tree* insert(Tree *root,int val){


if(root==NULL){
Tree *t=(Tree*)malloc(sizeof(Tree));
t->val=val;
t->hei=1;
t->left=NULL;
t->right=NULL;
return t;
}else{
if(root->left==NULL){
root->left=insert(root->left,val);
}else if(root->right==NULL){
root->right=insert(root->right,val);
}else{
if(root->left->hei > root->right->hei){
root->right=insert(root->right,val);
}else{
root->left=insert(root->left,val);
}
}
}
return root;
}

Tree* delete(Tree *root,Tree *del_node){


if(root==NULL) return NULL;
if(root==del_node){
Tree *cur=root->left;
if(root->right==NULL) {
free(root);
return cur;
}else{
cur=root->right;
while(cur->left!=NULL){
cur=cur->left;
}
cur->left=root->left;
cur=root->right;
free(root);
return cur;
}
}else{
root->left=delete(root->left,del_node);
root->right=delete(root->right,del_node);
}
}

void preOrder(Tree *root){


if(root==NULL) return;
printf("%d ",root->val);
preOrder(root->left);
preOrder(root->right);
}

void inOrder(Tree *root){


if(root==NULL) return;
inOrder(root->left);
printf("%d ",root->val);
inOrder(root->right);
}

void postOrder(Tree *root){


if(root==NULL) return;
postOrder(root->left);
postOrder(root->right);
printf("%d ",root->val);
}

void store_even_node(Tree* root,int *ind,Tree* arr[]){


if(root==NULL) return;
if(root->val%2==0){
arr[*ind]=root;
*ind=*ind+1;
}
store_even_node(root->left,ind,arr);
store_even_node(root->right,ind,arr);
}

int main(){

Tree *root=NULL;

for(int i=1;i<=10;i++){
root=insert(root,i);
height_update(root);
}

//Tree Formed...
// 1
// / \
// 2 3
// / \ / \
// 4 6 5 8
// / / /
// 7 10 9

printf("PreOrder: ");
preOrder(root);
printf("\n");

printf("inOrder: ");
inOrder(root);
printf("\n");

printf("postOrder: ");
postOrder(root);
printf("\n");

Tree* arr[4];
int ind=0;
store_even_node(root,&ind,arr);

for(int i=0;i<ind;i++){
delete(root,arr[i]);
}

height_update(root);

printf("preOrder: ");
preOrder(root);
printf("\n");

printf("inOrder: ");
inOrder(root);
printf("\n");

return 0;
}

Q2..................................................................
ANS-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

struct TREE{
int val;
int hei;
struct TREE *left;
struct TREE *right;
};
typedef struct TREE Tree;

int height_update(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
root->hei=1+(l>r?l:r);
return root->hei;
}

Tree* insert(Tree *root,int val){


if(root==NULL){
Tree *t=(Tree*)malloc(sizeof(Tree));
t->val=val;
t->hei=1;
t->left=NULL;
t->right=NULL;
return t;
}else{
if(root->left==NULL){
root->left=insert(root->left,val);
}else if(root->right==NULL){
root->right=insert(root->right,val);
}else{
if(root->left->hei > root->right->hei){
root->right=insert(root->right,val);
}else{
root->left=insert(root->left,val);
}
}
}
return root;
}

int height(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
return 1+(l>r?l:r);
}

int main(){

Tree *root=NULL;
for(int i=1;i<=10;i++){
root=insert(root,i);
height_update(root);
}

//Tree Formed...
// 1
// / \
// 2 3
// / \ / \
// 4 6 5 8
// / / /
// 7 10 9

printf("Heigh of tree: %d\n",height(root));

return 0;
}
Q3.........................................................................
ANS-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

struct TREE{
int val;
int hei;
struct TREE *left;
struct TREE *right;
};
typedef struct TREE Tree;

int height_update(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
root->hei=1+(l>r?l:r);
return root->hei;
}

Tree* insert(Tree *root,int val){


if(root==NULL){
Tree *t=(Tree*)malloc(sizeof(Tree));
t->val=val;
t->hei=1;
t->left=NULL;
t->right=NULL;
return t;
}else{
if(root->left==NULL){
root->left=insert(root->left,val);
}else if(root->right==NULL){
root->right=insert(root->right,val);
}else{
if(root->left->hei > root->right->hei){
root->right=insert(root->right,val);
}else{
root->left=insert(root->left,val);
}
}
}
return root;
}

int height(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
return 1+(l>r?l:r);
}

void print_data(Tree *root,int lev){


if(root==NULL) return;;
if(lev==1){
printf("%d ",root->val);
}else{
print_data(root->left,lev-1);
print_data(root->right,lev-1);
}
}

void printLevelOrder(Tree *root){


int height_root=height(root);
for(int i=1;i<=height_root;i++){
print_data(root,i);
}
}

int main(){

Tree *root=NULL;
for(int i=1;i<=10;i++){
root=insert(root,i);
height_update(root);
}

//Tree Formed...
// 1
// / \
// 2 3
// / \ / \
// 4 6 5 8
// / / /
// 7 10 9

printLevelOrder(root);

return 0;
}

Q4...................................................................
ANS-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

struct TREE{
int val;
int hei;
struct TREE *left;
struct TREE *right;
};
typedef struct TREE Tree;

int height_update(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
root->hei=1+(l>r?l:r);
return root->hei;
}
Tree* insert(Tree *root,int val){
if(root==NULL){
Tree *t=(Tree*)malloc(sizeof(Tree));
t->val=val;
t->hei=1;
t->left=NULL;
t->right=NULL;
return t;
}else{
if(root->left==NULL){
root->left=insert(root->left,val);
}else if(root->right==NULL){
root->right=insert(root->right,val);
}else{
if(root->left->hei > root->right->hei){
root->right=insert(root->right,val);
}else{
root->left=insert(root->left,val);
}
}
}
return root;
}

void count(Tree *root,int *leaf,int *non_leaf){


if(root==NULL) return;
if(root->left==NULL && root->right==NULL){
*leaf=*leaf+1;
}else{
*non_leaf=*non_leaf+1;
}
count(root->left,leaf,non_leaf);
count(root->right,leaf,non_leaf);
}

int main(){

Tree *root=NULL;
for(int i=1;i<=10;i++){
root=insert(root,i);
height_update(root);
}

//Tree Formed...
// 1
// / \
// 2 3
// / \ / \
// 4 6 5 8
// / / /
// 7 10 9

int leaf=0;
int non_leaf=0;
count(root,&leaf,&non_leaf);

printf("No of leaf Nodes is: %d\n",leaf);


printf("No of non leaf Nodes is: %d\n",non_leaf);
insert(root,100);
height_update(root);

insert(root,101);
height_update(root);

leaf=0;
non_leaf=0;
count(root,&leaf,&non_leaf);
printf("No of leaf Nodes is: %d\n",leaf);
printf("No of non leaf Nodes is: %d\n",non_leaf);

return 0;
}

Q5.................................................................................
ANS-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

struct TREE{
int val;
int hei;
struct TREE *left;
struct TREE *right;
};
typedef struct TREE Tree;

int height_update(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
root->hei=1+(l>r?l:r);
return root->hei;
}

Tree* insert(Tree *root,int val){


if(root==NULL){
Tree *t=(Tree*)malloc(sizeof(Tree));
t->val=val;
t->hei=1;
t->left=NULL;
t->right=NULL;
return t;
}else{
if(root->left==NULL){
root->left=insert(root->left,val);
}else if(root->right==NULL){
root->right=insert(root->right,val);
}else{
if(root->left->hei > root->right->hei){
root->right=insert(root->right,val);
}else{
root->left=insert(root->left,val);
}
}
}
return root;
}

void print_zigzag(int dir,int n,Tree* node[]){


int mx=2*n;
Tree *new_node[mx];
int ind=0;
if(dir==1){
for(int i=0;i<n;i++){
printf("%d ",node[i]->val);
if(node[i]->left!=NULL){
new_node[ind++]=node[i]->left;
}
if(node[i]->right!=NULL){
new_node[ind++]=node[i]->right;
}
}
}else{
for(int i=n-1;i>=0;i--){
printf("%d ",node[i]->val);
}
for(int i=0;i<n;i++){
if(node[i]->left!=NULL){
new_node[ind++]=node[i]->left;
}
if(node[i]->right!=NULL){
new_node[ind++]=node[i]->right;
}
}
}
print_zigzag((dir+1)&1,ind,new_node);
}

int main(){

Tree *root=NULL;
for(int i=1;i<=10;i++){
root=insert(root,i);
height_update(root);
}

//Tree Formed...
// 1
// / \
// 2 3
// / \ / \
// 4 6 5 8
// / / /
// 7 10 9

Tree *node[1];
node[0]=root;
print_zigzag(1,1,node);
return 0;
}

Q6....................................................................
ANS-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

struct TREE{
int val;
int hei;
struct TREE *left;
struct TREE *right;
};
typedef struct TREE Tree;

int height_update(Tree *root){


if(root==NULL) return 0;
int l=height_update(root->left);
int r=height_update(root->right);
root->hei=1+(l>r?l:r);
return root->hei;
}

Tree* insert(Tree *root,int val){


if(root==NULL){
Tree *t=(Tree*)malloc(sizeof(Tree));
t->val=val;
t->hei=1;
t->left=NULL;
t->right=NULL;
return t;
}else{
if(root->left==NULL){
root->left=insert(root->left,val);
}else if(root->right==NULL){
root->right=insert(root->right,val);
}else{
if(root->left->hei > root->right->hei){
root->right=insert(root->right,val);
}else{
root->left=insert(root->left,val);
}
}
}
return root;
}

void mirror_tree(Tree *root){


if(root==NULL) return;
Tree *cur=root->left;
root->left=root->right;
root->right=cur;

mirror_tree(root->left);
mirror_tree(root->right);
}

void preOrder(Tree *root){


if(root==NULL) return;
printf("%d ",root->val);
preOrder(root->left);
preOrder(root->right);
}

void inOrder(Tree *root){


if(root==NULL) return;
inOrder(root->left);
printf("%d ",root->val);
inOrder(root->right);
}

void postOrder(Tree *root){


if(root==NULL) return;
postOrder(root->left);
postOrder(root->right);
printf("%d ",root->val);
}

int main(){

Tree *root=NULL;
for(int i=1;i<=10;i++){
root=insert(root,i);
height_update(root);
}

//Tree Formed...
// 1
// / \
// 2 3
// / \ / \
// 4 6 5 8
// / / /
// 7 10 9

printf("Before Mirror: \n");

printf("PreOrder: ");
preOrder(root);
printf("\n");

printf("inOrder: ");
inOrder(root);
printf("\n");

printf("postOrder: ");
postOrder(root);
printf("\n");

mirror_tree(root);

printf("After Mirror: \n");


printf("PreOrder: ");
preOrder(root);
printf("\n");

printf("inOrder: ");
inOrder(root);
printf("\n");

printf("postOrder: ");
postOrder(root);
printf("\n");

return 0;
}

You might also like