Wednesday, 27 December 2017

C PROGRAM DEMONSTRATING ALL TREE TRAVERSALS RECURSIVELY AND NON RECURSIVELY

#include<stdio.h>
#include<stdlib.h>
struct Tnode{
 struct Tnode*pleft;
 struct Tnode*pright;
int ch;
};
struct Tnode *stack[50];
struct Tnode *stack1[50];
int top,top1;
void push(struct Tnode* ch1);
struct Tnode* pop();
void push1(struct Tnode *ch1);
struct Tnode *pop1();
struct Tnode*root;
struct Tnode*create_node(int c);
void create();
void insert(int c);
void non_preorder();
void non_postorder();
void non_inorder();
void inorder(struct Tnode *rot);
void preorder(struct Tnode *rot);
void postorder(struct Tnode *rot);
//main function
int main(){
top=-1;root=NULL;top1=-1;
create();
printf("RECURSIVE INORDER TRAVERSAL IS:\n");
inorder(root);
printf("\n");
printf("RECURSIVE PREORDER TRAVERSAL IS:\n");
preorder(root);
printf("\n");
printf("RECURSIVE POSTORDER TRAVERSAL IS:\n");
postorder(root);
printf("\n");
non_preorder();
printf("\n");
non_postorder();
printf("\n");
non_inorder();
 return 0;}

struct Tnode*create_node(int c){
 struct Tnode*pnew=(struct Tnode*)malloc(sizeof(struct Tnode));
 if(pnew){
 pnew->ch=c;
 pnew->pleft=NULL;
  pnew->pright=NULL;}
 return pnew;}
 void insert(int c){
  struct Tnode *pnew,*ptrav,*pa;
  pnew=create_node(c);
  if(!pnew) printf("no memeory\n");
  else{
   if(!root)
   root=pnew;
   else{
    pa=root;ptrav=root;
    while(ptrav){
     pa=ptrav;
     if(c<(ptrav->ch)){
     ptrav=ptrav->pleft;}
     else{
     ptrav=ptrav->pright;}}
    if(c<(pa->ch)){
    pa->pleft=pnew;}
    else{
    pa->pright=pnew;}}return;}}
 void create(){
 int k;
  int i,n;
  printf("enter number of values\n");scanf("%d",&n);
  for(i=0;i<n;i++) {
  scanf("%d",&k);
  insert(k);
  }}
 void push(struct Tnode *ch1){
  top++;
  stack[top]=ch1;
  return;
 }
struct Tnode *pop(){
  struct Tnode* k;
  if(top<0){
  k=NULL;return k ;}
  else
  {

    k=stack[top];
   top--;
   return k;
  }
 }
   void push1(struct Tnode *ch1){//thi function is for stack1 operations
  top1++;
  stack1[top1]=ch1;
  return;
 }//push1 end

struct Tnode *pop1(){
  struct Tnode* k;
  if(top1<0){
  k=NULL;return k ;}
  else
  {
    k=stack1[top1];
   top1--;
   return k;
  }}
//RECURSIVE FUNCTIONS
 void inorder(struct Tnode *rot){
  if(rot){
 inorder(rot->pleft);
   printf("%d\t",rot->ch);
   inorder(rot->pright);}
  }

   void preorder(struct Tnode *rot){
  if(rot){
  printf("%d\t",rot->ch);
  preorder(rot->pleft);
  preorder(rot->pright);}
  }

   void postorder(struct Tnode *rot){
  if(rot){
  postorder(rot->pleft);
   postorder(rot->pright);
   printf("%d\t",rot->ch);}
  }
//NON RECURSIVE FUNCTIONS
void non_preorder(){
char l; 
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
 printf("NON RECURSIVE PREORDER IS:\n");
 while(top>=0){
ptr=pop(); 
   printf("%d\t",ptr->ch);
 if((ptr->pright)!=NULL){
 ptemp=ptr->pright;
 push(ptemp);}
 if((ptr->pleft)!=NULL){
 ptemp=ptr->pleft;
 push(ptemp);
}
}
}}

void non_postorder(){
char l; 
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
 printf("NON RECURSIVE POST ORDER:\n");
 while(top>=0){
          ptr=pop();
  push1(ptr);
 if((ptr->pleft)!=NULL){
 ptemp=ptr->pleft;
 push(ptemp);}
 if((ptr->pright)!=NULL){
 ptemp=ptr->pright;
 push(ptemp);}
}
}
while(top1>=0){
 ptemp=pop1();
 printf("%d\t",ptemp->ch);
}
}

void non_inorder(){
struct Tnode *ptemp=NULL,*ptr=root;
if(!root){printf("empty tree\n");
}else{
push(ptr);
 printf("NON RECURSIVE IN ORDER IS:\n");
 while(top>=0){
  if((ptr->pleft)!=NULL){
 ptr=ptr->pleft;
 push(ptr);
}
 else{
ptemp=pop();
  printf("%d\t",ptemp->ch);
  if(ptemp->pright!=NULL){
  ptr=ptemp->pright;
  push(ptr);
 }}
}
}
}

No comments:

Post a Comment

FERMATS LITTLE THEOREM

import java.math.*; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) {    Sca...