944,221 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 470
  • C RSS
Oct 28th, 2009
0

problem regarding trees in data structures using c

Expand Post »
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.
Last edited by subhashkataria2; Oct 28th, 2009 at 6:32 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
subhashkataria2 is offline Offline
3 posts
since Oct 2009
Oct 28th, 2009
0
Re: problem regarding trees in data structures using c
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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 29th, 2009
0
Re: problem regarding trees in data structures using c
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");
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
subhashkataria2 is offline Offline
3 posts
since Oct 2009
Oct 29th, 2009
0
Re: problem regarding trees in data structures using c
I just code-tagged your code. Please try to do it in future.
Lemms see now...
i've tried this :
  1. #include<stdio.h>
  2. #include<conio.h>
  3. struct node
  4. {
  5. struct node *lptr;
  6. int data;
  7. struct node *rptr;
  8. };
  9. typedef struct node tnode;
  10. tnode *root=NULL,*temp;
  11. tnode *q[20];
  12. int rear=-1,front=0;
  13.  
  14. code to create the tree :
  15.  
  16. void create()
  17. {
  18. tnode *n;
  19. n=(tnode *)malloc(sizeof(tnode));
  20. n->lptr=n->rptr=NULL;
  21. printf("\n\nEnter an element\n");
  22. scanf("%d",&(n->data));
  23. temp=root;
  24. if(root==NULL)
  25. {
  26. root=n;
  27. }
  28. while(1)
  29. {
  30. if((n->data)<(temp->data))
  31. {
  32. if(temp->lptr!=NULL)
  33. temp=temp->lptr;
  34. else
  35. {
  36. temp->lptr=n;
  37. break;
  38. }
  39. }
  40. else if((n->data)>(temp->data))
  41. {
  42. if(temp->rptr!=NULL)
  43. {
  44. temp=temp->rptr;
  45. }
  46. else
  47. {
  48. temp->rptr=n;
  49. break;
  50. }
  51. }
  52. else
  53. {
  54. printf("\n\nelement already exist\n");
  55. break;
  56. }
  57. }
  58. }
  59.  
  60. code of insertion and deletion from queue :
  61.  
  62. void insert(tnode *t)
  63. {
  64. temp=root;
  65. if(rear==19)
  66. {
  67. printf("\nqueue is full\n");
  68. return;
  69. }
  70. rear++;
  71. q[rear]=t;
  72. }
  73. tnode *del()
  74. {
  75. tnode *x;
  76. if(front>19)
  77. {
  78. printf("\nqueue is empty\n");
  79. return NULL;
  80. }
  81. x=q[front];
  82. front++;
  83. return x;
  84. }
  85.  
  86. code for binary first search :
  87.  
  88. void bfs()
  89. {
  90. tnode *t;
  91. t=root;
  92. printf("\n\n");
  93. insert(t);
  94. while(t!=NULL)
  95. {
  96. t=del();
  97. if(t!=NULL)
  98. {
  99. printf("%d\t",t->data);
  100. if(t->lptr!=NULL)
  101. insert(t->lptr);
  102. if(t->rptr!=NULL)
  103. insert(t->rptr);
  104. }
  105. }
  106. }
  107.  
  108. (using this code the elements r displayed as 6 4 8 3 5 7 9
  109. but i need to display in the form of a tree)
  110.  
  111. void main()
  112. {
  113. int ch;
  114. while(1)
  115. {
  116. clrscr();
  117. printf("\nEnter your choice\n\n");
  118. printf("1.create\n");
  119. printf("2.bfs\n");
  120. printf("3.exit\n\n");
  121. scanf("%d",&ch);
  122. switch(ch)
  123. {
  124. case 1: create();
  125. break;
  126. case 2: bfs();
  127. getch();
  128. break;
  129. case 3: exit(0);
  130. default: printf("\n\nWRONG CHOICE\n\n");
  131. }
  132. }
  133. }
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: A question about signal
Next Thread in C Forum Timeline: How to buffer output?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC