Following is the code for inserting elements in a tree and then retrieving them back using inorder traversal technique.......
elements are getting inserted just fine,but the code doesn't displays the elements of the tree while performing inorder traversal...
Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *right;
struct node *left;
}*z,*t,*root=NULL,*x=NULL,*y=NULL;
void inorder_visit(struct node *temp);//function to traverse tree with inorder technique
void insert_node(struct node *temp,struct node *z);//function to insert elements in a tree
int main(){
int n,i;
for(i=0;i<5;++i){
printf("enter the data:");
scanf("%d",&n);
z=(struct node*)malloc(sizeof(int));
if(z==NULL)
printf("sorry!");
else{
z->data=n;
z->left=NULL;
z->right=NULL;
t=root;
insert_node(t,z);
}
}
t=root;
inorder_visit(t);
return 0;
}
void inorder_visit(struct node *temp){
if(temp!=NULL){
inorder_visit(temp->left);
printf("%d\t",temp->data);
inorder_visit(temp->right);
}
}
void insert_node(struct node *temp,struct node *z){
if(temp==NULL)
temp=z;
else {
if(z->data<=temp->data)
insert_node(temp->left,z);
else
insert_node(temp->right,z);}
}