Professional Documents
Culture Documents
_____________________________________________
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 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 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
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 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 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);
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 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;
mirror_tree(root->left);
mirror_tree(root->right);
}
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");
mirror_tree(root);
printf("inOrder: ");
inOrder(root);
printf("\n");
printf("postOrder: ");
postOrder(root);
printf("\n");
return 0;
}