hello i'm subhash n m persuing BCA IInd year n got an assignment of writing a program to create a binary tree n display the elements level wise ie i need to display in a form of tree.
for example if elements are 6,4,8,3,5,7,9 then i need to display it as


..........6.......
....4..........8...
.3...5.....7....9...
(here dots r used jst to seperate these values)
i've writen a program to crate the tree but not getting how to write for displaying it.
plz help me in writing this code.

Show us some code I suppose.
You should know how deep each element is, and you can use the debth to see how many spaces are needed
I think it'd make sense to print the data to a string, I'd say.

i've tried this :

#include<stdio.h>
#include<conio.h>
struct node
{
    struct node *lptr;
    int data;
    struct node *rptr;
};
typedef struct node tnode;
tnode *root=NULL,*temp;
tnode *q[20];
int rear=-1,front=0;

code to create the tree :

void create()
{
    tnode *n;
    n=(tnode *)malloc(sizeof(tnode));
    n->lptr=n->rptr=NULL;
    printf("\n\nEnter an element\n");
    scanf("%d",&(n->data));
    temp=root;
    if(root==NULL)
    {
        root=n;
    }
    while(1)
    {
        if((n->data)<(temp->data))
        {
            if(temp->lptr!=NULL)
                temp=temp->lptr;
            else
            {
                temp->lptr=n;
                break;
            }
        }
        else if((n->data)>(temp->data))
        {
            if(temp->rptr!=NULL)
            {
                temp=temp->rptr;
            }
            else
            {
                temp->rptr=n;
                break;
            }
        }
        else
        {
            printf("\n\nelement already exist\n");
            break;
        }
    }
}

code of insertion and deletion from queue :

void insert(tnode *t)
{
    temp=root;
    if(rear==19)
    {
        printf("\nqueue is full\n");
        return;
    }
    rear++;
    q[rear]=t;
}
tnode *del()
{
    tnode *x;
    if(front>19)
    {
        printf("\nqueue is empty\n");
        return NULL;
    }
    x=q[front];
    front++;
    return x;
}

code for binary first search :

void bfs()
{
    tnode *t;
    t=root;
    printf("\n\n");
    insert(t);
    while(t!=NULL)
    {
        t=del();
        if(t!=NULL)
        {
            printf("%d\t",t->data);
            if(t->lptr!=NULL)
                insert(t->lptr);
            if(t->rptr!=NULL)
                insert(t->rptr);
        }
    }
}

(using this code the elements r displayed as 6 4 8 3 5 7 9
but i need to display in the form of a tree)

void main()
{
    int ch;
    while(1)
    {
        clrscr();
        printf("\nEnter your choice\n\n");
        printf("1.create\n");
        printf("2.bfs\n");
        printf("3.exit\n\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: create();
                    break;
            case 2: bfs();
                    getch();
                    break;
            case 3: exit(0);
            default: printf("\n\nWRONG CHOICE\n\n");
        }
    }
}

I just code-tagged your code. Please try to do it in future.
Lemms see now...

i've tried this :

#include<stdio.h>
#include<conio.h>
struct node
{
	struct node *lptr;
	int data;
	struct node *rptr;
};
typedef struct node tnode;
tnode *root=NULL,*temp;
tnode *q[20];
int rear=-1,front=0;

code to create the tree :

void create()
{
	tnode *n;
	n=(tnode *)malloc(sizeof(tnode));
	n->lptr=n->rptr=NULL;
	printf("\n\nEnter an element\n");
	scanf("%d",&(n->data));
	temp=root;
	if(root==NULL)
	{
		root=n;
	}
	while(1)
	{
		if((n->data)<(temp->data))
		{
			if(temp->lptr!=NULL)
				temp=temp->lptr;
			else
			{
				temp->lptr=n;
				break;
			}
		}
		else if((n->data)>(temp->data))
		{
			if(temp->rptr!=NULL)
			{
				temp=temp->rptr;
			}
			else
			{
				temp->rptr=n;
				break;
			}
		}
		else
		{
			printf("\n\nelement already exist\n");
			break;
		}
	}
}

code of insertion and deletion from queue :

void insert(tnode *t)
{
	temp=root;
	if(rear==19)
	{
		printf("\nqueue is full\n");
		return;
	}
	rear++;
	q[rear]=t;
}
tnode *del()
{
	tnode *x;
	if(front>19)
	{
		printf("\nqueue is empty\n");
		return NULL;
	}
	x=q[front];
	front++;
	return x;
}

code for binary first search :

void bfs()
{
	tnode *t;
	t=root;
	printf("\n\n");
	insert(t);
	while(t!=NULL)
	{
		t=del();
		if(t!=NULL)
		{
			printf("%d\t",t->data);
			if(t->lptr!=NULL)
				insert(t->lptr);
			if(t->rptr!=NULL)
				insert(t->rptr);
		}
	}
}

(using this code the elements r displayed as 6    4    8    3    5    7    9
but i need to display in the form of a tree)

void main()
{
	int ch;
	while(1)
	{
		clrscr();
		printf("\nEnter your choice\n\n");
		printf("1.create\n");
		printf("2.bfs\n");
		printf("3.exit\n\n");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1: create();
					break;
			case 2: bfs();
					getch();
					break;
			case 3: exit(0);
			default: printf("\n\nWRONG CHOICE\n\n");
		}
	}
}
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.