| | |
problem regarding trees in data structures using c
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
Last edited by subhashkataria2; 29 Days Ago at 6:32 pm.
0
#2 29 Days Ago
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.
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.
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 28 Days Ago
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");
}
}
}
#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");
}
}
}
0
#4 28 Days Ago
I just code-tagged your code. Please try to do it in future.
Lemms see now...
Lemms see now...
•
•
•
•
i've tried this :
C Syntax (Toggle Plain Text)
#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"); } } }
![]() |
Similar Threads
- Data Structures in C Problem (C)
- Data Structures problem (C++)
- Data Structures (C++)
- Help with Data structures: Hash tables (C++)
- data structures and classic algorithms outdated? (Computer Science)
- Do I need to take Data Structures? (C++)
- Data Structures (C)
- Data Structures??? (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
Other Threads in the C Forum
- Previous Thread: A question about signal
- Next Thread: How to buffer output?
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






