Write an efficient function that takes a binary tree as input and displays the elements of tree level by level, but from last level to first. What are the time and space complexities of your solution?
Input:
3
Output: 5 1 6 8 4 7 3
code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define QUEUESIZE 20
struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
} *root=NULL;
//queue functions
int *queue[QUEUESIZE];
int rear=-1,front=-1;
void enque(struct treenode *root)
{
if(rear<QUEUESIZE)
{
queue[++rear]=root;
}
else printf("queue overflow");
}
int deque()
{
int i;
if(front!=rear)
i=queue[++front];
else printf("underfolw");
return i;
}
int isqempty()
{
if(front==rear)
return 1;
return 0;
}
struct treenode * buildtree()
{
struct treenode *temp1,*temp2,*temp3,*temp4,*temp5,*temp6;
root=(struct treenode *)malloc(sizeof(struct treenode));
temp1=(struct treenode *)malloc(sizeof(struct treenode));
temp2=(struct treenode *)malloc(sizeof(struct treenode));
temp3=(struct treenode *)malloc(sizeof(struct treenode));
temp4=(struct treenode *)malloc(sizeof(struct treenode));
temp5=(struct treenode *)malloc(sizeof(struct treenode));
temp6=(struct treenode *)malloc(sizeof(struct treenode));
root->data=4;
root->left=temp1;
temp1->data=5;
root->right=temp2;
temp2->data=6;
temp1->left=temp3;
temp3->data=7;
temp3->left=temp3->right=NULL;
temp1->right=temp4;
temp4->data=8;
temp4->left=NULL;
temp4->right=NULL;
temp2->left=temp5;
temp5->data=6;
temp2->right=temp6;
temp6->data=10;
temp5->left=temp5->right=temp6->left=temp6->right=NULL;
printf("\n tree buildedsuccessfull");
return root;
}
//no nodes
int noofnodes(struct treenode *root)
{
if(root==NULL) return 0;
return noofnodes(root->left)+noofnodes(root->right)+1;
}
//
void printfrombottam(struct treenode *root)
{
int *array[QUEUESIZE],i=0,j=0;
struct treenode *k;
array[j++]=root;
//k=noofnodes(root);
while(i<j)
{
if(root->right!=NULL)
array[j++]=root->right;
if(root->left!=NULL)
array[j++]=root->left;
root=array[++i];
}
for(i=j-1;i>=0;i--)
{
k=array[i];
printf("%d ",k->data);
}
}//end
//main
void main()
{
struct treenode *root;
int i=1;
while(i)
{
printf("\n enter 1 to build elements");
printf("\n enter 2 print elements in level wise");
scanf("%d",&i);
switch(i)
{
case 1:
root=buildtree();
break;
case 2:
printfrombottam(root);
break;
}
}
}
Input:
3
/ \
4 7
/ \ / \
5 1 6 8
code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define QUEUESIZE 20
struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
} *root=NULL;
//queue functions
int *queue[QUEUESIZE];
int rear=-1,front=-1;
void enque(struct treenode *root)
{
if(rear<QUEUESIZE)
{
queue[++rear]=root;
}
else printf("queue overflow");
}
int deque()
{
int i;
if(front!=rear)
i=queue[++front];
else printf("underfolw");
return i;
}
int isqempty()
{
if(front==rear)
return 1;
return 0;
}
struct treenode * buildtree()
{
struct treenode *temp1,*temp2,*temp3,*temp4,*temp5,*temp6;
root=(struct treenode *)malloc(sizeof(struct treenode));
temp1=(struct treenode *)malloc(sizeof(struct treenode));
temp2=(struct treenode *)malloc(sizeof(struct treenode));
temp3=(struct treenode *)malloc(sizeof(struct treenode));
temp4=(struct treenode *)malloc(sizeof(struct treenode));
temp5=(struct treenode *)malloc(sizeof(struct treenode));
temp6=(struct treenode *)malloc(sizeof(struct treenode));
root->data=4;
root->left=temp1;
temp1->data=5;
root->right=temp2;
temp2->data=6;
temp1->left=temp3;
temp3->data=7;
temp3->left=temp3->right=NULL;
temp1->right=temp4;
temp4->data=8;
temp4->left=NULL;
temp4->right=NULL;
temp2->left=temp5;
temp5->data=6;
temp2->right=temp6;
temp6->data=10;
temp5->left=temp5->right=temp6->left=temp6->right=NULL;
printf("\n tree buildedsuccessfull");
return root;
}
//no nodes
int noofnodes(struct treenode *root)
{
if(root==NULL) return 0;
return noofnodes(root->left)+noofnodes(root->right)+1;
}
//
void printfrombottam(struct treenode *root)
{
int *array[QUEUESIZE],i=0,j=0;
struct treenode *k;
array[j++]=root;
//k=noofnodes(root);
while(i<j)
{
if(root->right!=NULL)
array[j++]=root->right;
if(root->left!=NULL)
array[j++]=root->left;
root=array[++i];
}
for(i=j-1;i>=0;i--)
{
k=array[i];
printf("%d ",k->data);
}
}//end
//main
void main()
{
struct treenode *root;
int i=1;
while(i)
{
printf("\n enter 1 to build elements");
printf("\n enter 2 print elements in level wise");
scanf("%d",&i);
switch(i)
{
case 1:
root=buildtree();
break;
case 2:
printfrombottam(root);
break;
}
}
}
No comments:
Post a Comment