Advertisement
Guest User

v1.0

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