Advertisement
ahmad_zizo

tree.c

May 31st, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 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 (strcmp(tree->name,name)>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.  
  112. //searh in tree_id
  113. //ana 3adelt hena
  114. //ÇáãÝÑæÖ ÏáæÞÊí íÞÏÑ íØÈÚ ÇÊäíä áíåã äÝÓ ÇáÈíÇäÇÊ
  115. node_id* search_id(node_id** tree, int id) {
  116. if (!(*tree)) {
  117. return NULL;
  118. }
  119.  
  120. if (id == (*tree)->id) {
  121. return *tree;
  122. } else if (id < (*tree)->id) {
  123. search_id(&((*tree)->left), id);
  124. } else if (id > (*tree)->id) {
  125. search_id(&((*tree)->right), id);
  126. }
  127.  
  128. return NULL;
  129. }
  130.  
  131. //searh in tree_name
  132. node_id* search_name(node_id** tree, char* name) {
  133. if (!(*tree)) {
  134. return NULL;
  135. }
  136.  
  137. if (strcmp(name, (*tree)->name) == 0) {
  138. return *tree;
  139. } else if (strcmp(name, (*tree)->name) < 0) {
  140. search_name(&((*tree)->left), name);
  141. } else if (strcmp(name, (*tree)->name) > 0) {
  142. search_name(&((*tree)->right), name);
  143. }
  144. return NULL;
  145. }
  146.  
  147. //ana 3adlt hena
  148. //msh hatefr2 name aw id
  149. node_id * min_value_node_id(node_id* node) {
  150. node_id* current = node;
  151. while (current->left != NULL)
  152. current = current->left;
  153.  
  154. return current;
  155. }
  156.  
  157. //delete node by id
  158. //3adelt hena
  159. //ÎáíÊå áæ ãÓÍ åäÇ íãÓÍ Ýí ÇáÊÑí ÇáÊÇäíÉ ÚÔÇä ÇáÊÚÏíá íÈÞì ÚÇáÇÊäíä
  160. node_id* delete_id(node_id* tree, int id) {
  161. if (tree == NULL) {
  162. return tree;
  163. }
  164. if (id < tree->id)
  165. //delete node from left subtree
  166. tree->left = delete_id(tree->left, id);
  167. else if (id > tree->id)
  168. //delete node from right subtree
  169. tree->right = delete_id(tree->right, id);
  170.  
  171. else {
  172.  
  173. if (tree->left == NULL) {
  174. node_id* temp = tree->right;
  175. free(tree);
  176. return temp;
  177. }
  178. else if (tree->right == NULL) {
  179. node_id* temp = tree->left;
  180. free(tree);
  181. return temp;
  182. }
  183.  
  184. node_id* temp = min_value_node_id(tree->right);
  185.  
  186. tree->id = temp->id;
  187. strcpy(tree->name, temp->name);
  188. tree->right = delete_id(tree->right, temp->id);
  189. }
  190. return tree;
  191. }
  192.  
  193. node_id* delete_name(node_id* tree, char* name) {
  194. if (tree == NULL) {
  195. return tree;
  196. }
  197.  
  198.  
  199. if (strcmp(name, tree->name) > 0)
  200. tree->left = delete_name(tree->left, name);
  201. else if (strcmp(name, tree->name) < 0) {
  202. tree->right = delete_name(tree->right, name);
  203. }
  204. else {
  205. if (tree->left == NULL) {
  206. node_id* temp = tree->right;
  207. free(tree);
  208. return temp;
  209. }
  210. else if (tree->right == NULL) {
  211. node_id* temp = tree->left;
  212. free(tree);
  213. return temp;
  214. }
  215. node_id* temp = min_value_node_id(tree->right);
  216. strcpy(tree->name, temp->name);
  217. tree->id = temp->id;
  218.  
  219. tree->right = delete_name(tree->right, temp->name);
  220. }
  221.  
  222. return tree;
  223. }
  224.  
  225.  
  226.  
  227. void reed_file_to_tree(node_id** tree_id, node_id** tree_name, char* file_name) {
  228. FILE* fp = fopen("/Users/BoshraNour/Desktop/final-trial/final-trial/project.txt", "r");
  229.  
  230. if (fp == NULL) {
  231. printf("Error open file\n");
  232. return;
  233. }
  234.  
  235. char buf[1024], *p;
  236.  
  237. //get line from file
  238. while (fgets(buf, sizeof(buf), fp) != NULL) {
  239. if ((p = strchr(buf, '\n')) == NULL) {
  240. fprintf(stderr, "input line too long.\n");
  241. return;
  242. }
  243. *p = '\0';
  244. // our line should be bigger than 1
  245. if (strlen(buf) > 1) {
  246. //split line
  247. char* pch = strtok(buf, ",");
  248. char name[40];
  249. //copy name
  250. strncpy(name, pch, 40);
  251. pch = strtok(NULL, ",");
  252. //copy id
  253. int id = atoi(pch);
  254. //insert
  255. (*tree_id) = insert_id(*tree_id, id, name);
  256. (*tree_name) = insert_name(*tree_name, id, name);
  257. }
  258.  
  259.  
  260.  
  261. }
  262.  
  263. fclose(fp);
  264.  
  265.  
  266. }
  267.  
  268. void write_tree_to_file(node_id* tree_id, FILE* fp) {
  269. if (fp == NULL) {
  270. printf("Error file\n");
  271. return;
  272. }
  273. char line[60];
  274. char numb[20];
  275. if (tree_id) {
  276. // convert int to char
  277. sprintf(numb, "%d", tree_id->id);
  278. //add strings
  279. strcpy(line, tree_id->name);
  280. strcpy(line+strlen(tree_id->name), ",");
  281. strcpy(line+strlen(line), numb);
  282. //add sym
  283. strcat(line,"\n");
  284. //write to file
  285. fputs(line, fp);
  286. write_tree_to_file(tree_id->left, fp);
  287. write_tree_to_file(tree_id->right, fp);
  288. }
  289. //fclose(fp);
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement