Advertisement
ahmad_zizo

data

May 31st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "trees.h"
  3. #include <string.h>
  4. //
  5. // trees.c
  6. // final-trial
  7. //
  8. // Created by Ahmed Omar on 5/27/14.
  9. // Copyright (c) 2014 Ahmed Omar. All rights reserved.
  10. //
  11.  
  12. #include <stdio.h>
  13. #include "trees.h"
  14. #include <string.h>
  15.  
  16. //ana 3adlt hena
  17. //if 2 have the same id, the id will be inserted in the right node
  18. node_id* insert_id(node_id* tree, int id, char* name)
  19. {
  20. // this node will be inserted
  21. node_id* temp = NULL;
  22.  
  23. //if tree is empty
  24. if (!tree)
  25. {
  26.  
  27. temp = (node_id*)malloc(sizeof(node_id));
  28. temp->left = temp->right = NULL;
  29. temp->id = id;
  30. strcpy(temp->name, name);
  31. //insert
  32. //tree = temp;
  33. return temp;
  34. }
  35.  
  36. //insert node in left subtree
  37. if (id < tree->id)
  38. {
  39. tree->left = insert_id(tree->left, id, name);
  40. //insert node in right subtree
  41. }
  42. else
  43. {
  44. tree->right = insert_id(tree->right, id, name);
  45. }
  46.  
  47. return tree;
  48. }
  49.  
  50. //ana 3adlt hena
  51. //the same amendment as insert_id
  52. node_id* insert_name(node_id* tree, int id, char* name)
  53. {
  54. // this node will be inserted
  55. node_id* temp = NULL;
  56.  
  57. //if tree is empty
  58. if (!tree)
  59. {
  60.  
  61. temp = (node_id*)malloc(sizeof(node_id));
  62. temp->left = temp->right = NULL;
  63. temp->id = id;
  64. strcpy(temp->name, name);
  65. //insert
  66. //tree = temp;
  67. return temp;
  68. }
  69.  
  70. //insert node in left subtree
  71. if (strcmp(tree->name,name)>0)
  72. {
  73. tree->left = insert_name(tree->left, id, name);
  74. //insert node in right subtree
  75. }
  76. else
  77. {
  78. tree->right = insert_name(tree->right, id, name);
  79. }
  80.  
  81. return tree;
  82. }
  83.  
  84.  
  85. void insert(node_id** tree_id, node_id** tree_name, int id, char* name)
  86. {
  87. //insert to tree_id
  88. (*tree_id) = insert_id(*tree_id, id, name);
  89. //insert to tree_name
  90. (*tree_name) = insert_name(*tree_name, id, name);
  91. }
  92.  
  93. //ana 3adel hena x3
  94. //msh hatefre2 en kan name aw id
  95. void print_preorder(node_id* tree,int c)
  96. {
  97. if (tree && tree->id != ~(1<<31))
  98. {
  99. int i;
  100. for(i=0; i<c; i++) printf("\t");
  101. printf("%d %s\n", tree->id, tree->name);
  102. print_preorder(tree->left,c+1);
  103. print_preorder(tree->right,c+1);
  104. }
  105. else
  106. printf("\n");
  107. }
  108.  
  109.  
  110. void print_inorder(node_id* tree,int c)
  111. {
  112. if (tree && tree->id != ~(1<<31))
  113. {
  114. print_inorder(tree->left,c+1);
  115. int i;
  116. for(i=0; i<c; i++) printf("\t");
  117. printf("%d %s\n", tree->id, tree->name);
  118. print_inorder(tree->right,c+1);
  119. }
  120. else
  121. printf("\n");
  122.  
  123. }
  124.  
  125. void print_postorder(node_id* tree, int c)
  126. {
  127. if (tree && tree->id != ~(1<<31))
  128. {
  129. print_postorder(tree->left,c+1);
  130. print_postorder(tree->right,c+1);
  131. int i;
  132. for(i=0; i<c; i++) printf("\t");
  133. printf("%d %s\n", tree->id, tree->name);
  134. }
  135. else
  136. printf("\n");
  137. }
  138.  
  139. void search_id_2(node_id** tree, int id)
  140. {
  141. if (!(*tree))
  142. {
  143. return;
  144. }
  145.  
  146. if (id == (*tree)->id)
  147. {
  148. printf("%s %d\n",(*tree)->name,(*tree)->id);
  149. }
  150. if (id < (*tree)->id)
  151. {
  152. search_id_2(&((*tree)->left), id);
  153. }
  154. else
  155. {
  156. search_id_2(&((*tree)->right), id);
  157. }
  158.  
  159. return;
  160. }
  161.  
  162. void search_name_2(node_id** tree, char* name)
  163. {
  164. if (!(*tree))
  165. {
  166. return;
  167. }
  168.  
  169. if (strstr((*tree)->name,name) != NULL)
  170. {
  171. printf("%s %d\n",(*tree)->name,(*tree)->id);
  172. }
  173. if (strcmpi(name, (*tree)->name) < 0)
  174. {
  175. search_name_2(&((*tree)->left), name);
  176. }
  177. else
  178. {
  179. search_name_2(&((*tree)->right), name);
  180. }
  181. return NULL;
  182. }
  183.  
  184.  
  185. //searh in tree_id
  186. //ana 3adelt hena
  187. node_id* search_id(node_id** tree, int id)
  188. {
  189. if (!(*tree))
  190. {
  191. return NULL;
  192. }
  193.  
  194. if (id == (*tree)->id)
  195. {
  196. return *tree;
  197. }
  198. else if (id < (*tree)->id)
  199. {
  200. search_id(&((*tree)->left), id);
  201. }
  202. else if (id > (*tree)->id)
  203. {
  204. search_id(&((*tree)->right), id);
  205. }
  206.  
  207. return NULL;
  208. }
  209.  
  210.  
  211.  
  212. //searh in tree_name
  213. /*node_id* search_name(node_id** tree, char* name)
  214. {
  215. if (!(*tree))
  216. {
  217. return NULL;
  218. }
  219.  
  220. if (strstr((*tree)->name,name) != NULL)
  221. {
  222. return *tree;
  223. }
  224. else if (strcmpi(name, (*tree)->name) < 0)
  225. {
  226. search_name(&((*tree)->left), name);
  227. }
  228. else if (strcmpi(name, (*tree)->name) > 0)
  229. {
  230. search_name(&((*tree)->right), name);
  231. }
  232. return NULL;
  233. }*/
  234.  
  235. void search_name(node_id* root , char *name , node_id* ptrarray[] , int*n)
  236. {
  237. if (root == NULL)
  238. return ;
  239. else if(strstr(tree_name->name,name) != NULL)
  240. {
  241. ptrarray[*n] = root;
  242. (*n)++;
  243. search_name(root->left , name,a,n);
  244. search_name(root->right , name,a,n);
  245.  
  246.  
  247. }
  248. else if(stricmp(name,root->name) > 0)
  249. searchByName(root->right , name,a,n);
  250. else if(stricmp(name,root->name) < 0)
  251. searchByName(root->left , name,a,n);
  252. }
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261. //ana 3adlt hena
  262. //msh hatefr2 name aw id
  263. node_id * min_value_node_id(node_id* node)
  264. {
  265. node_id* current = node;
  266. while (current->left != NULL)
  267. current = current->left;
  268.  
  269. return current;
  270. }
  271.  
  272. //delete node by id
  273. //3adelt hena
  274. //ÎáíÊå áæ ãÓÍ åäÇ íãÓÍ Ýí ÇáÊÑí ÇáÊÇäíÉ ÚÔÇä ÇáÊÚÏíá íÈÞì ÚÇáÇÊäíä
  275. node_id* delete_id(node_id* tree, int id)
  276. {
  277. if (tree == NULL)
  278. {
  279. return tree;
  280. }
  281. if (id < tree->id)
  282. //delete node from left subtree
  283. tree->left = delete_id(tree->left, id);
  284. else if (id > tree->id)
  285. //delete node from right subtree
  286. tree->right = delete_id(tree->right, id);
  287.  
  288. else
  289. {
  290.  
  291. if (tree->left == NULL)
  292. {
  293. node_id* temp = tree->right;
  294. free(tree);
  295. return temp;
  296. }
  297. else if (tree->right == NULL)
  298. {
  299. node_id* temp = tree->left;
  300. free(tree);
  301. return temp;
  302. }
  303.  
  304. node_id* temp = min_value_node_id(tree->right);
  305.  
  306. tree->id = temp->id;
  307. strcpy(tree->name, temp->name);
  308. tree->right = delete_id(tree->right, temp->id);
  309. }
  310. return tree;
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317. node_id* delete_name(node_id* tree, char* name, int id)
  318. {
  319. if (tree == NULL)
  320. {
  321. return tree;
  322. }
  323. if (strcmp(name, tree->name) > 0)
  324. tree->left = delete_name(tree->left, name,id);
  325. else if (strcmp(name, tree->name) < 0)
  326. {
  327. tree->right = delete_name(tree->right, name),id;
  328. }
  329. else
  330. {
  331. if (tree->left == NULL && tree->id == id)
  332. {
  333. node_id* temp = tree->right;
  334. free(tree);
  335. return temp;
  336. }
  337. else if (tree->right == NULL && tree->id == id)
  338. {
  339. node_id* temp = tree->left;
  340. free(tree);
  341. return temp;
  342. }
  343. node_id* temp = min_value_node_id(tree->right);
  344. strcpy(tree->name, temp->name);
  345. tree->id = temp->id;
  346.  
  347. tree->right = delete_name(tree->right, temp->name);
  348. }
  349. return tree;
  350. }
  351.  
  352.  
  353. void reed_file_to_tree(node_id** tree_id, node_id** tree_name, char* file_name)
  354. {
  355. FILE* fp = fopen("/Users/BoshraNour/Desktop/final-trial/final-trial/project.txt", "r");
  356.  
  357. if (fp == NULL)
  358. {
  359. printf("Error open file\n");
  360. return;
  361. }
  362.  
  363. char buf[1024], *p;
  364.  
  365. //get line from file
  366. while (fgets(buf, sizeof(buf), fp) != NULL)
  367. {
  368. if ((p = strchr(buf, '\n')) == NULL)
  369. {
  370. fprintf(stderr, "input line too long.\n");
  371. return;
  372. }
  373. *p = '\0';
  374. // our line should be bigger than 1
  375. if (strlen(buf) > 1)
  376. {
  377. //split line
  378. char* pch = strtok(buf, ",");
  379. char name[40];
  380. //copy name
  381. strncpy(name, pch, 40);
  382. pch = strtok(NULL, ",");
  383. //copy id
  384. int id = atoi(pch);
  385. //insert
  386. (*tree_id) = insert_id(*tree_id, id, name);
  387. (*tree_name) = insert_name(*tree_name, id, name);
  388. }
  389. }
  390. fclose(fp);
  391. }
  392.  
  393. void write_tree_to_file(node_id* tree_id, FILE* fp)
  394. {
  395. if (fp == NULL)
  396. {
  397. printf("Error file\n");
  398. return;
  399. }
  400. char line[60];
  401. char numb[20];
  402. if (tree_id)
  403. {
  404. // convert int to char
  405. sprintf(numb, "%d", tree_id->id);
  406. //add strings
  407. strcpy(line, tree_id->name);
  408. strcpy(line+strlen(tree_id->name), ",");
  409. strcpy(line+strlen(line), numb);
  410. //add sym
  411. strcat(line,"\n");
  412. //write to file
  413. fputs(line, fp);
  414. write_tree_to_file(tree_id->left, fp);
  415. write_tree_to_file(tree_id->right, fp);
  416. }
  417. //fclose(fp);
  418. }
  419.  
  420.  
  421.  
  422. int main()
  423. {
  424. node_id* root_id;
  425. node_id* root_name;
  426.  
  427.  
  428. root_name = NULL;
  429. root_id = NULL;
  430. printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
  431. printf("choose:\n");
  432. printf("1- insert data\n");
  433. printf("2- write file to tree\n3- print\n");
  434. printf("4- delete\n");
  435. printf("5- search\n");
  436. printf("6- read file to tree\n7- quit\n\n");
  437.  
  438.  
  439. int command;
  440. char co[20];
  441.  
  442. char name[40];
  443. char id_2[10];
  444. int id;
  445. char c;
  446. printf("Enter command: ");
  447. scanf(" %s", co);
  448. command = atoi(co);
  449.  
  450. do
  451. {
  452. if(command<1 || command>7)
  453. {
  454. system("cls");
  455. printf("enter valid number\n\n");
  456. }
  457. else
  458. switch (command)
  459. {
  460. case 7:
  461. {
  462. system("cls");
  463. printf("THANK YOU!\n\n");
  464. printf("Ahmad Omar 2715 & Ahmad Abdulaziz 2210\n\n");
  465. exit(0);
  466. break;
  467. }
  468. case 1:
  469. {
  470. system("cls");
  471. printf("Enter name: ");
  472. while((c= getchar()) != '\n' && c != EOF);
  473. //gets(name);
  474. int j,f;
  475.  
  476. scanf(" %[^\n]",name);
  477. printf("Enter id: ");
  478. scanf(" %[^\n]",id_2);
  479. int l=strlen(name);
  480. for(j=0; j<l; j++)
  481. if ( !(name[j]=='\n' || name[j] == 32 || (name[j] >= 65 && name[j] <= 90) || (name[j] >= 97 && name[j] <= 122) ))
  482. {
  483. printf("ERROR in name\n");
  484. f=1;
  485. break;
  486. }
  487. if(f==1)
  488. break;
  489. node_id *test = search_id(&root_id,atoi(id_2));
  490. if(test != NULL && atoi(id_2) == test->id)
  491. printf("ERROR, id already exist!\n");
  492. else if(atoi(name)==0 && atoi(id_2)> 0 && atoi(id_2) == atof(id_2))
  493. {
  494. id= atoi(id_2);
  495. insert(&root_id, &root_name, id, name);
  496. system("cls");
  497. printf("%d %s inserted succesfuly\n\n",id,name);
  498. }
  499. else
  500. {
  501. printf("\nERROR in name or id!\n");
  502. }
  503. break;
  504. }
  505.  
  506.  
  507. case 2:
  508. {
  509. system("cls");
  510. char file_name[50];
  511. printf("Enter file name: ");
  512. scanf("%s", file_name);
  513. FILE* file = fopen(file_name, "w");
  514. write_tree_to_file(root_id, file);
  515. fclose(file);
  516. }
  517. break;
  518.  
  519. case 3:
  520. {
  521. char z[15];
  522. system("cls");
  523. printf("choose type of print:\n");
  524. printf("1- print preorder by id\n");
  525. printf("2- print preorder by name\n3- print post order by id\n");
  526. printf("4- print post order by name\n5- inorder by id\n");
  527. printf("6- print inorder by name\n");
  528. printf("your choice: ");
  529. scanf(" %[^\n]s",z);
  530. int x=atoi(z);
  531. if(x==1)
  532. print_preorder(root_id,0);
  533. else if(x==2)
  534. print_preorder(root_name,0);
  535. else if(x==3)
  536. print_postorder(root_id,0);
  537. else if(x==4)
  538. print_postorder(root_name,0);
  539. else if(x==5)
  540. print_inorder(root_id,0);
  541. else if(x==6)
  542. print_inorder(root_name,0);
  543. else
  544. {
  545. system("cls");
  546. printf("enter a valid choice!\n");
  547. break;
  548. }
  549. break;
  550. }
  551. case 4:
  552. {
  553. /*char z2[15];
  554. system("cls");
  555. printf("1- by name\n2- by id\n");
  556. printf("your choice: ");
  557. scanf(" %[^\n]s",z2);
  558. int x2=atoi(z2);
  559. if(x==1)
  560. {
  561. node_id*a[100];
  562. int n = 0;
  563. int id;
  564. searchByName(nroot,name,a,&n);
  565. if (n == 0)
  566. {
  567. printf("Not Found \n");
  568. }
  569. else
  570. {
  571. int i,ch;
  572. for ( i = 0 ; i < n ; i++)
  573. {
  574. printf("%d %s\n",ptrarray[i]->id , ptrarray[i]->name);
  575. }
  576. printf ( "enter the id of student you want to delete: \n");
  577. scanf("%d",&ch);
  578. delete_name(tree_name, name,ch);
  579. }
  580.  
  581. else if(x2==2)
  582. {
  583. system("cls");
  584. printf("Enter id: ");
  585. scanf("%d", &id);
  586. node_id* temp = search_id(&root_id, id);
  587. if (temp != NULL)
  588. {
  589. strcpy(name, temp->name);
  590. }
  591. if(temp==NULL)
  592. printf("NOT FOUND.\n\n");
  593. else
  594. {
  595. root_id = delete_id(root_id, id);
  596. root_name = delete_name(root_name,name);
  597. printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
  598. }
  599. }
  600. else
  601. {
  602. system("cls");
  603. printf("enter a valid choice.\n");
  604. break;
  605. }
  606. break;*/
  607. char z2[15];
  608. system("cls");
  609. printf("1- by name\n2- by id\n");
  610. printf("your choice: ");
  611. scanf(" %[^\n]s",z2);
  612. int x2=atoi(z2);
  613. if(x2==1)
  614. {
  615. system("cls");
  616. printf("Enter name: ");
  617. while((c= getchar()) != '\n' && c != EOF);
  618. gets(name);
  619. node_id* temp = search_name(&root_name, name);
  620. if (temp != NULL)
  621. {
  622. id = temp->id;
  623. }
  624. if(temp == NULL)
  625. printf("NOT FOUND.\n\n");
  626. else
  627. {
  628. root_id = delete_id(root_id, id);
  629. root_name = delete_name(root_name, name);
  630. printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
  631. }
  632. }
  633.  
  634. else if(x2==2)
  635. {
  636. system("cls");
  637. printf("Enter id: ");
  638. scanf("%d", &id);
  639. node_id* temp = search_id(&root_id, id);
  640. if (temp != NULL)
  641. {
  642. strcpy(name, temp->name);
  643. }
  644. if(temp==NULL)
  645. printf("NOT FOUND.\n\n");
  646. else
  647. {
  648. root_id = delete_id(root_id, id);
  649. root_name = delete_name(root_name,name);
  650. printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
  651. }
  652. }
  653. else
  654. {
  655. system("cls");
  656. printf("enter a valid choice.\n");
  657. break;
  658. }
  659. break;
  660. }
  661. case 5:
  662. {
  663. system("cls");
  664. char z3[15];
  665. printf("1- by id\n2- by name\n");
  666. printf("your choice: ");
  667. scanf("% [^\n]s",z3);
  668. int x3=atoi(z3);
  669. if(x3==2)
  670. {
  671. system("cls");
  672. printf("Enter name: ");
  673. while((c= getchar()) != '\n' && c != EOF);
  674. gets(name);
  675. search_name_2(&root_name, name);
  676. }
  677. else if(x3==1)
  678. {
  679. system("cls");
  680. printf("Enter id: ");
  681. int id;
  682. scanf("%d", &id);
  683. search_id_2(&root_id, id);
  684. }
  685. else
  686. {
  687. system("cls");
  688. printf("enter a valid choice!\n");
  689. break;
  690. }
  691. break;
  692. }
  693. case 6:
  694. {
  695. system("cls");
  696. char file_name[50];
  697. printf("Enter file name: ");
  698. scanf("%s", file_name);
  699. reed_file_to_tree(&root_id, &root_name, file_name);
  700.  
  701. break;
  702. }
  703. }
  704. printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
  705. printf("choose:\n");
  706. printf("1- insert data\n");
  707. printf("2- write file to tree\n3- print\n");
  708. printf("4- delete\n");
  709. printf("5- search\n");
  710. printf("6- read file to tree\n7- quit\n\n");
  711. printf("Enter command: ");
  712. scanf(" %s", co);
  713. command = atoi(co);
  714. }
  715. while (command != ~(1<<31));
  716.  
  717. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement