immuntasir

Untitled

Mar 22nd, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct btnode
  5. {
  6. int value;
  7. struct btnode *l;
  8. struct btnode *r;
  9. }*root = NULL, *temp = NULL, *t2, *t1;
  10.  
  11. void del1();
  12. void insert();
  13. void del();
  14. void inorder(struct btnode *t);
  15. void create();
  16. void SearchAPlaceForInsertion(struct btnode *t);
  17. void preorder(struct btnode *t);
  18. void postorder(struct btnode *t);
  19. void search1(struct btnode *t,int data);
  20. int smallest(struct btnode *t);
  21. int largest(struct btnode *t);
  22.  
  23. int flag = 1;
  24.  
  25. int main()
  26. {
  27. int ch;
  28.  
  29. printf("\nOPERATIONS ---");
  30. printf("\n1 - Insert an element into tree\n");
  31. printf("2 - Delete an element from the tree\n");
  32. printf("3 - Inorder Traversal\n");
  33. printf("4 - Preorder Traversal\n");
  34. printf("5 - Postorder Traversal\n");
  35. printf("6 - Exit\n");
  36. while(1)
  37. {
  38. printf("\nEnter your choice : ");
  39. scanf("%d", &ch);
  40. switch (ch)
  41. {
  42. case 1:
  43. insert();
  44. break;
  45. case 2:
  46. del();
  47. break;
  48. case 3:
  49. inorder(root);
  50. break;
  51. case 4:
  52. preorder(root);
  53. break;
  54. case 5:
  55. postorder(root);
  56. break;
  57. case 6:
  58. exit(0);
  59. default :
  60. printf("Wrong choice, Please enter correct choice ");
  61. break;
  62. }
  63. }
  64. }
  65.  
  66. /* To insert a node in the tree */
  67. void create() {
  68. int data;
  69. printf("Enter data of node to be inserted : ");
  70. scanf("%d", &data);
  71. temp = (struct btnode *)malloc(1*sizeof(struct btnode));
  72. temp->value = data;
  73. temp->l = temp->r = NULL;
  74. }
  75. void SearchAPlaceForInsertion(struct btnode *t) {
  76. if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
  77. SearchAPlaceForInsertion(t->r);
  78. else if ((temp->value > t->value) && (t->r == NULL))
  79. t->r = temp;
  80. else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
  81. SearchAPlaceForInsertion(t->l);
  82. else if ((temp->value < t->value) && (t->l == NULL))
  83. t->l = temp;
  84. }
  85. void insert() {
  86. create();
  87. if (root == NULL)
  88. root = temp;
  89. else
  90. SearchAPlaceForInsertion(root);
  91. }
  92.  
  93. /* To create a node */
  94.  
  95.  
  96. /* Function to the appropriate position to insert the new node */
  97.  
  98. /* recursive function to perform inorder traversal of tree */
  99.  
  100.  
  101. /* To check for the deleted node */
  102. void del1(struct btnode *t) {
  103. int k;
  104.  
  105. /* To delete leaf node */
  106. if ((t->l == NULL) && (t->r == NULL)) {
  107. if (t1->l == t) {
  108. t1->l = NULL;
  109. }
  110. else {
  111. t1->r = NULL;
  112. }
  113. t = NULL;
  114. free(t);
  115. return;
  116. }
  117.  
  118. /* To delete node having one left hand child */
  119. else if ((t->r == NULL)) {
  120. if (t1 == t) {
  121. root = t->l;
  122. t1 = root;
  123. }
  124. else if (t1->l == t) {
  125. t1->l = t->l;
  126. }
  127. else {
  128. t1->r = t->l;
  129. }
  130. t = NULL;
  131. free(t);
  132. return;
  133. }
  134.  
  135. /* To delete node having right hand child */
  136. else if (t->l == NULL) {
  137. if (t1 == t) {
  138. root = t->r;
  139. t1 = root;
  140. }
  141. else if (t1->r == t)
  142. t1->r = t->r;
  143. else
  144. t1->l = t->r;
  145. t == NULL;
  146. free(t);
  147. return;
  148. }
  149.  
  150. /* To delete node having two child */
  151. else if ((t->l != NULL) && (t->r != NULL)) {
  152. t2 = root;
  153. if (t->r != NULL) {
  154. k = smallest(t->r);
  155. flag = 1;
  156. }
  157. else {
  158. k =largest(t->l);
  159. flag = 2;
  160. }
  161. search1(root, k);
  162. t->value = k;
  163. }
  164.  
  165. }
  166. void search1(struct btnode *t, int data) {
  167. if ((data>t->value)) {
  168. t1 = t;
  169. search1(t->r, data);
  170. }
  171. else if ((data < t->value)) {
  172. t1 = t;
  173. search1(t->l, data);
  174. }
  175. else if ((data==t->value)) {
  176. del1(t);
  177. }
  178. }
  179. void del() {
  180. int data;
  181.  
  182. if (root == NULL) {
  183. printf("No elements in a tree to delete");
  184. return;
  185. }
  186. printf("Enter the data to be deleted : ");
  187. scanf("%d", &data);
  188. t1 = root;
  189. t2 = root;
  190. search1(root, data);
  191. }
  192. void inorder(struct btnode *t) {
  193. if (root == NULL) {
  194. printf("No elements in a tree to display");
  195. return;
  196. }
  197. if (t->l != NULL)
  198. inorder(t->l);
  199. printf("%d ", t->value);
  200. if (t->r != NULL)
  201. inorder(t->r);
  202. }
  203. /* To find the preorder traversal */
  204. void preorder(struct btnode *t) {
  205. if (root == NULL) {
  206. printf("No elements in a tree to display");
  207. return;
  208. }
  209. printf("%d ", t->value);
  210. if (t->l != NULL)
  211. preorder(t->l);
  212. if (t->r != NULL)
  213. preorder(t->r);
  214. }
  215.  
  216. /* To find the postorder traversal */
  217. void postorder(struct btnode *t)
  218. {
  219. if (root == NULL) {
  220. printf("No elements in a tree to display ");
  221. return;
  222. }
  223. if (t->l != NULL)
  224. postorder(t->l);
  225. if (t->r != NULL)
  226. postorder(t->r);
  227. printf("%d -> ", t->value);
  228. }
  229.  
  230. /* Search for the appropriate position to insert the new node */
  231.  
  232.  
  233. /* To delete a node */
  234.  
  235.  
  236. /* To find the smallest element in the right sub tree */
  237. int smallest(struct btnode *t) {
  238. t2 = t;
  239. if (t->l != NULL) {
  240. t2 = t;
  241. return(smallest(t->l));
  242. }
  243. else
  244. return (t->value);
  245. }
  246.  
  247. /* To find the largest element in the left sub tree */
  248. int largest(struct btnode *t)
  249. {
  250. if (t->r != NULL) {
  251. t2 = t;
  252. return(largest(t->r));
  253. }
  254. else
  255. return(t->value);
  256. }
Advertisement
Add Comment
Please, Sign In to add comment