Advertisement
ahmad_zizo

DATA PROJECT

May 26th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. //
  2. // trees.c
  3. // final-trial
  4. //
  5. // Created by Ahmed Omar on 5/27/14.
  6. // Copyright (c) 2014 Ahmed Omar. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include "trees.h"
  11. #include <string.h>
  12.  
  13. //ana 3adlt hena
  14. //if 2 have the same id, the id will be inserted in the right node
  15. node_id* insert_id(node_id* tree, int id, char* name) {
  16. // this node will be inserted
  17. node_id* temp = NULL;
  18.  
  19. //if tree is empty
  20. if (!tree) {
  21.  
  22. temp = (node_id*)malloc(sizeof(node_id));
  23. temp->left = temp->right = NULL;
  24. temp->id = id;
  25. strcpy(temp->name, name);
  26. //insert
  27. //tree = temp;
  28. return temp;
  29. }
  30.  
  31. //insert node in left subtree
  32. if (id < tree->id) {
  33. tree->left = insert_id(tree->left, id, name);
  34. //insert node in right subtree
  35. } else {
  36. tree->right = insert_id(tree->right, id, name);
  37. }
  38.  
  39. return tree;
  40. }
  41.  
  42. //ana 3adlt hena
  43. //the same amendment as insert_id
  44. node_id* insert_name(node_id* tree, int id, char* name) {
  45. // this node will be inserted
  46. node_id* temp = NULL;
  47.  
  48. //if tree is empty
  49. if (!tree) {
  50.  
  51. temp = (node_id*)malloc(sizeof(node_id));
  52. temp->left = temp->right = NULL;
  53. temp->id = id;
  54. strcpy(temp->name, name);
  55. //insert
  56. //tree = temp;
  57. return temp;
  58. }
  59.  
  60. //insert node in left subtree
  61. if (strcmpi(node->data.name,names)>0) {
  62. tree->left = insert_name(tree->left, id, name);
  63. //insert node in right subtree
  64. } else {
  65. tree->right = insert_name(tree->right, id, name);
  66. }
  67.  
  68. return tree;
  69. }
  70.  
  71.  
  72. void insert(node_id** tree_id, node_id** tree_name, int id, char* name) {
  73. //insert to tree_id
  74. (*tree_id) = insert_id(*tree_id, id, name);
  75. //insert to tree_name
  76. (*tree_name) = insert_name(*tree_name, id, name);
  77. }
  78.  
  79. //ana 3adel hena x3
  80. //msh hatefre2 en kan name aw id
  81. void print_preorder(node_id* tree) {
  82. if (tree) {
  83. printf("%d\t%s\n", tree->id, tree->name);
  84. print_preorder(tree->left);
  85. print_preorder(tree->right);
  86. }
  87. }
  88.  
  89.  
  90. void print_inorder(node_id* tree) {
  91. if (tree) {
  92. print_inorder(tree->left);
  93. printf("%d\t%s\n", tree->id, tree->name);
  94. print_inorder(tree->right);
  95. }
  96. }
  97.  
  98. void print_postorder(node_id* tree) {
  99. if (tree) {
  100. print_postorder(tree->left);
  101. print_postorder(tree->right);
  102. printf("%d\t%s\n", tree->id, tree->name);
  103. }
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. //searh in tree_id
  112. //ana 3adelt hena
  113. //المفروض دلوقتي يقدر يطبع اتنين ليهم نفس البيانات
  114. void search_id(node_id** tree, int id) {
  115. if (!(*tree)) {
  116. return;
  117. }
  118.  
  119. if (id == (*tree)->id) {
  120. printf("%s\t%d",(*tree)->name,(*tree)->id);
  121. }
  122. if (id < (*tree)->id) {
  123. search_id(&((*tree)->left), id);
  124. }
  125. else {
  126. search_id(&((*tree)->right), id);
  127. }
  128.  
  129. return;
  130. }
  131.  
  132. //searh in tree_name
  133. //ana 3adelt hena
  134. //the same amendment as search_id
  135. void search_name(node_id** tree, char* name) {
  136. if (!(*tree)) {
  137. return;
  138. }
  139.  
  140. if (strcmpi(name, (*tree)->name) == 0) {
  141. printf("%s\t%d",(*tree)->name,(*tree)->id);
  142. }
  143. if (strcmpi(name, (*tree)->name) < 0) {
  144. search_name(&((*tree)->left), name);
  145. }
  146. else
  147. {
  148. search_name(&((*tree)->right), name);
  149. }
  150. return NULL;
  151. }
  152. //ana 3adlt hena
  153. //msh hatefr2 name aw id
  154. node_id * min_value_node_id(node_id* node) {
  155. node_id* current = node;
  156. while (current->left != NULL)
  157. current = current->left;
  158.  
  159. return current;
  160. }
  161.  
  162. //delete node by id
  163. //3adelt hena
  164. //خليته لو مسح هنا يمسح في التري التانية عشان التعديل يبقى عالاتنين
  165. node_id* delete_id(node_id* tree, int id) {
  166. if (tree == NULL) {
  167. return tree;
  168. }
  169. if (id < tree->id)
  170. //delete node from left subtree
  171. tree->left = delete_id(tree->left, id);
  172. else if (id > tree->id)
  173. //delete node from right subtree
  174. tree->right = delete_id(tree->right, id);
  175.  
  176. else {
  177.  
  178. if (tree->left == NULL) {
  179. node_id* temp = tree->right;
  180. delete_name(tree, temp->name);
  181. free(tree);
  182. return temp;
  183. }
  184. else if (tree->right == NULL) {
  185. node_id* temp = tree->left;
  186. delete_name(tree, temp->name);
  187. free(tree);
  188. return temp;
  189. }
  190.  
  191. node_id* temp = min_value_node_id(tree->right);
  192.  
  193. tree->id = temp->id;
  194. strcpy(tree->name, temp->name);
  195. tree->right = delete_id(tree->right, temp->id);
  196. }
  197. return tree;
  198. }
  199.  
  200. node_id* delete_name(node_id* tree, char* name) {
  201. if (tree == NULL) {
  202. return tree;
  203. }
  204.  
  205.  
  206. if (strcmp(name, tree->name) > 0)
  207. tree->left = delete_name(tree->left, name);
  208. else if (strcmp(name, tree->name) < 0) {
  209. tree->right = delete_name(tree->right, name);
  210. }
  211. else {
  212. if (tree->left == NULL) {
  213. node_id* temp = tree->right;
  214. delete_id(tree, temp->id);
  215. free(tree);
  216. return temp;
  217. }
  218. else if (tree->right == NULL) {
  219. node_id* temp = tree->left;
  220. delete_id(tree, temp->id);
  221. free(tree);
  222. return temp;
  223. }
  224. node_id* temp = min_value_node_id(tree->right);
  225. strcpy(tree->name, temp->name);
  226. tree->id = temp->id;
  227.  
  228. tree->right = delete_name(tree->right, temp->name);
  229. }
  230.  
  231. return tree;
  232. }
  233.  
  234.  
  235.  
  236. void reed_file_to_tree(node_id** tree_id, node_id** tree_name, char* file_name) {
  237. FILE* fp = fopen("project.txt", "r");
  238.  
  239. if (fp == NULL) {
  240. printf("Error open file\n");
  241. return;
  242. }
  243.  
  244. char buf[1024], *p;
  245.  
  246. //get line from file
  247. while (fgets(buf, sizeof(buf), fp) != NULL) {
  248. if ((p = strchr(buf, '\n')) == NULL) {
  249. fprintf(stderr, "input line too long.\n");
  250. return;
  251. }
  252. *p = '\0';
  253. // our line should be bigger than 1
  254. if (strlen(buf) > 1) {
  255. //split line
  256. char* pch = strtok(buf, ",");
  257. char name[40];
  258. //copy name
  259. strncpy(name, pch, 40);
  260. pch = strtok(NULL, ",");
  261. //copy id
  262. int id = atoi(pch);
  263. //insert
  264. (*tree_id) = insert_id(*tree_id, id, name);
  265. (*tree_name) = insert_name(*tree_name, id, name);
  266. }
  267.  
  268.  
  269.  
  270. }
  271.  
  272. fclose(fp);
  273.  
  274.  
  275. }
  276.  
  277. void write_tree_to_file(node_id* tree_id, FILE* fp) {
  278. if (fp == NULL) {
  279. printf("Error file\n");
  280. return;
  281. }
  282. char line[60];
  283. char numb[20];
  284. if (tree_id) {
  285. // convert int to char
  286. sprintf(numb, "%d", tree_id->id);
  287. //add strings
  288. strcpy(line, tree_id->name);
  289. strcpy(line+strlen(tree_id->name), ",");
  290. strcpy(line+strlen(line), numb);
  291. //add sym
  292. strcat(line,"\n");
  293. //write to file
  294. fputs(line, fp);
  295. write_tree_to_file(tree_id->left, fp);
  296. write_tree_to_file(tree_id->right, fp);
  297. }
  298. //fclose(fp);
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement