DS | Practical - 11 | C Program to Implement Tree Traversals Inorder, Preorder and Postorder

 

Write a C Program to Implement Tree Traversals Inorder, Preorder and Postorder

GTU DS Practical-11

Implement recursive and non-recursive tree traversing methods in-order, preorder and post-order traversal in C.

Code for Tree Traversals

      1. #include<stdio.h>
      2. #include<malloc.h>
      3. #include<stdlib.h>
      4. struct node
      5. {
      6. int data;
      7. struct node *lptr,*rptr;
      8. };
      9. void Insert(struct node *,int);
      10. void Preorder(struct node *);
      11. void Postorder(struct node *);
      12. void Inorder(struct node *);
      13. void Delete(struct node *,int);
      14. struct node *Header;
      15. int main()
      16. {
      17. int ch,x;
      18. Header=(struct node*)malloc(sizeof(struct node));
      19. Header->lptr=Header;
      20. Header->rptr=Header;
      21. do
      22. {
      23. printf("\n1.Insert a node in a Tree");
      24. printf("\n2.Preorder Traversal(Recursively)");
      25. printf("\n3.Postorder Traversal(Recursively)");
      26. printf("\n4.Inorder Traversal(Recursively)");
      27. printf("\n5.Delete a node from Binary Search Tree");
      28. printf("\n6.Exit");
      29. printf("\n\nEnter Choice: ");
      30. scanf("%d",&ch);
      31. switch(ch)
      32. {
      33. case 1:
      34. printf("\n\tEnter Element : ");
      35. scanf("%d",&x);
      36. Insert(Header,x);
      37. printf("\n\tInorder Traversal(Recursively)=\n\t");
      38. printf("\n\t-------------------------------------\n\t");
      39. Inorder(Header->lptr);
      40. printf("\n\t-------------------------------------\n\t");
      41. break;
      42. case 2:
      43. printf("\n\tPreorder Traversal(Recursively):\n\t");
      44. printf("\n\t-------------------------------------\n\t");
      45. Preorder(Header->lptr);
      46. printf("\n\t-------------------------------------\n\t");
      47. break;
      48. case 3:
      49. printf("\n\tPostorder Traversal(Recursively):\n\t");
      50. printf("\n\t-------------------------------------\n\t");
      51. Postorder(Header->lptr);
      52. printf("\n\t-------------------------------------\n\t");
      53. break;
      54. case 4:
      55. printf("\n\tInorder Traversal(Recursively):\n\t");
      56. printf("\n\t-------------------------------------\n\t");
      57. Inorder(Header->lptr);
      58. printf("\n\t-------------------------------------\n\t");
      59. break;
      60. case 5:
      61. printf("\n\tEnter Element which u want to Delete: ");
      62. scanf("%d",&x);
      63. printf("\n\t-------------------------------------\n\t");
      64. Delete(Header,x);
      65. printf("\n\t-------------------------------------\n\t");
      66. break;
      67. case 6: exit(0);
      68. break;
      69. default:
      70. printf("\n\t Plz Try Again.");
      71. }
      72. }while(6);
      73. }
      74. void Insert(struct node *h,int x)
      75. {
      76. struct node *New,*parent,*cur;
      77. New=(struct node *)malloc(sizeof(struct node));
      78. if(New==NULL)
      79. {
      80. printf("\n\tNo Tree Node Available.");
      81. return;
      82. }
      83. New->data=x;
      84. New->lptr=NULL;
      85. New->rptr=NULL;
      86. if(h->lptr==h)
      87. {
      88. h->lptr=New;
      89. return;
      90. }
      91. cur=h->lptr;
      92. parent=h;
      93. while(cur!=NULL)
      94. {
      95. if(x<cur->data)
      96. {
      97. parent=cur;
      98. cur=cur->lptr;
      99. }
      100. else if(x>cur->data)
      101. {
      102. parent=cur;
      103. cur=cur->rptr;
      104. }
      105. else
      106. {
      107. printf("\n\t Element Already Exist.\n");
      108. return;
      109. }
      110. }
      111. if(x<parent->data)
      112. {
      113. parent->lptr=New;
      114. return;
      115. }
      116. if(x>parent->data)
      117. {
      118. parent->rptr=New;
      119. return;
      120. }
      121. return;
      122. }
      123. void Preorder(struct node *t)
      124. {
      125. if(t!=NULL)
      126. printf("%d ",t->data);
      127. else
      128. {
      129. printf("\n\t Empty Tree.");
      130. return;
      131. }
      132. if(t->lptr!=NULL)
      133. Preorder(t->lptr);
      134. if(t->rptr!=NULL)
      135. Preorder(t->rptr);
      136. return;
      137. }
      138. void Postorder(struct node *t)
      139. {
      140. if(t==NULL)
      141. {
      142. printf("\n\t Empty Tree.");
      143. return;
      144. }
      145. Postorder(t->lptr);
      146. Postorder(t->rptr);
      147. printf("%d ",t->data);
      148. return;
      149. }
      150. void Inorder(struct node *t)
      151. {
      152. if(t==NULL)
      153. {
      154. printf("\n\t Empty Tree.");
      155. return;
      156. }
      157. if(t->lptr!=NULL)
      158. Inorder(t->lptr);
      159. printf("%d ",t->data);
      160. if(t->rptr!=NULL)
      161. Inorder(t->rptr);
      162. return;
      163. }
      164. void Delete(struct node *h,int x)
      165. {
      166. int found;
      167. char d;
      168. struct node *cur,*parent,*pred,*suc,*q;
      169. if(h->lptr==h)
      170. {
      171. printf("\n\t Empty Tree.");
      172. return;
      173. }
      174. parent=h;
      175. cur=h->lptr;
      176. d='L';
      177. found=0;
      178. while(!found && cur!=NULL)
      179. {
      180. if(x==cur->data)
      181. found=1;
      182. else if(x<cur->data)
      183. {
      184. parent=cur;
      185. cur=cur->lptr;
      186. d='L';
      187. }
      188. else
      189. {
      190. parent=cur;
      191. cur=cur->rptr;
      192. d='R';
      193. }
      194. }
      195. if(!found)
      196. {
      197. printf("\n\t Node is not found.");
      198. return;
      199. }
      200. if(cur->lptr==NULL)
      201. q=cur->rptr;
      202. else
      203. {
      204. if(cur->rptr==NULL)
      205. q=cur->lptr;
      206. else
      207. {
      208. suc=cur->rptr;
      209. if(suc->lptr==NULL)
      210. {
      211. suc->lptr=cur->lptr;
      212. q=suc;
      213. }
      214. else
      215. {
      216. pred=cur->rptr;
      217. suc=pred->lptr;
      218. while(suc->lptr!=NULL)
      219. {
      220. pred=suc;
      221. suc=pred->lptr;
      222. }
      223. pred->lptr=suc->rptr;
      224. suc->lptr=cur->lptr;
      225. suc->rptr=cur->rptr;
      226. q=suc;
      227. }
      228. }
      229. }
      230. if(d=='L')
      231. parent->lptr=q;
      232. else
      233. parent->rptr=q;
      234. printf("\n\t%d is Deleted.",x);
      235. }

       

      Goto Home Page :

    Comments

    Post a Comment