Hello.Got a problem with a binary tree again,i have made a visual printing but the problem is that it prints only completed tree,so if it is not full it crashes,is there a posibillity to add some extra print to value NULL,so after one right or left leave won't have value(NULL) it will print for example "NULL" or something like that.Also wanted to ask if i won't have problems with other operations in future,such as "search" or "delete root" etc(with this kind of printing)?
#include <stdio.h>
#include <stdlib.h>
struct tree
{
int data;
struct tree *rajt;
struct tree *left;
};
typedef struct tree Tree;
typedef Tree *treeptr;
int main()
{
init();
}
int insert(treeptr *mytree,int elem)
{
if(*mytree==NULL)
{
*mytree=(Tree*)malloc(sizeof(Tree));
(*mytree)->data=elem;
(*mytree)->left=NULL;
(*mytree)->rajt=NULL;
}
else if(elem<(*mytree)->data)
insert(&((*mytree)->left),elem);
else if(elem>(*mytree)->data)
insert(&((*mytree)->rajt),elem);
else printf("Can't insert the element,there is such element inside \n");
}
int init()
{
int i,valju,j;
treeptr root=NULL;
printf("Enter 7 elements to the tree : \n");
for(i=0;i<7;i++)
{
scanf("%d",&valju);
printf("Next value \n");
insert(&root,valju);
}
printf("Tree looks like: \n");
printukas1(root);
printf("\n");
system("PAUSE");
}
int printukas1(treeptr mytree,int n)
{
int i;
if(mytree->left)
printukas1(mytree->rajt,n+1);
for(i=0;i<n;i++)printf("\t");
printf("%d\n",mytree->data);
if(mytree->rajt)
printukas1(mytree->left,n+1);
}