0

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

Edited by deceptikon: Fixed the code block

2
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by Newbienoob
0

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

Edited by nullptr: problems with code tags

0

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.

Edited by Newbienoob: mistake

0

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

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.

0

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)

0

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;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.