problem regarding trees in data structures using c

Reply

Join Date: Oct 2009
Posts: 3
Reputation: subhashkataria2 is an unknown quantity at this point 
Solved Threads: 0
subhashkataria2 subhashkataria2 is offline Offline
Newbie Poster

problem regarding trees in data structures using c

 
0
  #1
29 Days Ago
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; 29 Days Ago at 6:32 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: subhashkataria2 is an unknown quantity at this point 
Solved Threads: 0
subhashkataria2 subhashkataria2 is offline Offline
Newbie Poster
 
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");
}
}
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
0
  #4
28 Days Ago
I just code-tagged your code. Please try to do it in future.
Lemms see now...
Originally Posted by subhashkataria2 View Post
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. }
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC