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);
}

See solution below

Untested, but should work.

void printukas1(treeptr mytree) // in order traversal
{
    if (NULL == mytree)
    {
        return;
    }

    printukas1(mytree->left);
    printf("%d\n",mytree->data);
    printukas1(mytree->rajt);
}

Well it works but the problem is that it prints like :
X
X
X
X
X
My idea was to make a bit visuality for a binary tree,and my printing code was printing like :

               X

       X 

               X

X(root)

               X

       X

               X

But it works only if tree is completed.

This will work but only display as you depicted if the tree is balanced. e.g if
input values were like {4, 2, 6, 1, 7, 3, 5}

void printukas1(treeptr mytree, int n)
{
    int i;
    if (NULL == mytree)
    {
        return;
    }

    printukas1(mytree->rajt, n + 1);

    for (i = 0; i < n; i++) 
        printf("\t");
    printf("%d\n",mytree->data);

    printukas1(mytree->left, n + 1);

    return;
}

This code is the same as mine,but my question was to make it working not only if the tree is balanced.It crashes because when tree is not balanced and pointer shows to NULL,so is there a possibility to assign something to NULL,so when it will show NULL it will print something.

This code is the same as mine

No it's not. My code will print the tree regardless of its balance and display the tree as it actually is.
(within the limitations of std console output)

A slight modification of my code to print null.

void printukas1(treeptr mytree, int n)
{
    int i;
    if (NULL == mytree)
    {
        for (i = 0; i < n; i++)
            printf("\t");
        printf("null");
        return;
    }

    printukas1(mytree->rajt, n + 1);

    for (i = 0; i < n; i++)
        printf("\t");
    printf("%d\n",mytree->data);

    printukas1(mytree->left, n + 1);
    return;
}

correction: line 8 should be printf("null\n");

Wows thanks! You helped me alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.