Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Tuesday, December 24, 2013

print tree in c

Binary search tree
here the code for how to print tree in c in console window.
Was this help full or if you didn't understand this code then feel free to ask me comment your question or you can mail me @ raj.nishant360@gmail.com
You can mail me or comment your views about improving my blog.
#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *LCHILD;
 struct node *RCHILD;
}*tree;
struct node *create(int val)
{
 struct node *temp;
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=val;
 temp->LCHILD=NULL;
 temp->RCHILD=NULL;
 return temp;
}
void add(struct node **tr,int val)
{
 struct node *copy,*copy1;
 copy=(struct node*)malloc(sizeof(struct node));
 copy1=(struct node*)malloc(sizeof(struct node));
 copy1=*tr;
 if(copy1==NULL)
 {
  *tr=create(val);
  return ;
 }
 if(val>=copy1->data)
  add(&(copy1->RCHILD),val);
 else
  add(&(copy1->LCHILD),val);
}
void printTree(struct node *tree,int level)
{
 int i;
 if(tree!=NULL)
 {
  printTree(tree->RCHILD,level+1);
  printf("\n\n");
  for(i=0;i<level;i++)
   printf("       ");
  printf("%d",tree->data);
  printTree(tree->LCHILD,level+1);
 }
}
int main()
{
 tree=NULL;
  REPEAT:
 printf("\n\n\nENTER YOUR CHOICE\n");
 printf("1->add node to tree\n");
        printf("2->EXIT\n");
        int choice,val;
 scanf("%d",&choice);
 system("clear");
 switch(choice)
 {
 case 1:
  scanf("%d",&val);
  add(&tree,val);
  printTreeTriangle(tree,1);
  break;
        case 2:
                exit(2);
        default:
  printf("your choice is incorrect\n");
 }
 goto REPEAT;
 return 0;
}