Code:-
#include<stdio.h>
#include<stdlib.h>
void preOrder(int *a,int i,int n){
if(i>=n) return;
printf("%d ",a[i]);
preOrder(a,2*i+1,n);
preOrder(a,2*i+2,n);
}
void inOrder(int *a,int i,int n){
if(i>=n) return;
inOrder(a,2*i+1,n);
printf("%d ",a[i]);
inOrder(a,2*i+2,n);
}
void postOrder(int *a,int i,int n){
if(i>=n) return;
postOrder(a,2*i+1,n);
postOrder(a,2*i+2,n);
printf("%d ",a[i]);
}
void main(){
int n,i;
printf("Enter total no. of node: ");
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
printf("Enter value (Level by level): ");
for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("Pre-Order: "); preOrder(a,0,n); printf("\n");
printf("In-Order: "); inOrder(a,0,n); printf("\n");
printf("Post-Order: "); postOrder(a,0,n); printf("\n");
}
Output:-
Time-Complexity:-
Worst Case: O(log(N))
Avg. Case: O(log(N))
Best Case: O(log(N))
0 Comments