Advertisement
Guest User

tree

a guest
May 15th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1.  
  2. // main.c
  3. // trees
  4. //
  5. // Created by Mahmoud ElMarakshy on 4/28/13.
  6. // Copyright (c) 2013 Mahmoud ElMarakshy. All rights reserved.
  7. //
  8.  
  9. #include<stdio.h>
  10. #include<stdio.h>
  11. #include <ctype.h>
  12.  
  13. struct node{
  14. char * data;
  15. struct node *left;
  16. struct node *right;
  17. char * id;
  18.  
  19. }*root=NULL,*root2=NULL,*temp;
  20. struct node* insert(struct node *root,char x[],char id[]);
  21. struct node* insert2(struct node *root,char x[],char id[]);
  22. struct node * delete(struct node*ptr,char a[]);
  23. void traverse(struct node*ptr);
  24. struct node * Findmin(struct node*ptr);
  25. char* search(struct node *root,char x[]);
  26. char* search2(struct node *ptr,char x[]);
  27. struct node * delete2(struct node*T,char id[]);
  28. int validname(char x[]);
  29. int validid(char x[]);
  30. void load();
  31.  
  32.  
  33. int main(){
  34. int choice;
  35. char a[10],id[10];
  36. // load();
  37. printf("enter choice(1 to insert , 2 delete by name 6 delete by id , 3 traverse ,4 search by name search 5 by id ):");
  38. scanf("%d",&choice);
  39.  
  40. while(choice!=-1){
  41. switch (choice) {
  42. case 1:
  43. printf("enter name:");
  44. scanf("%s",a);
  45. while(!validname(a)){
  46. printf("INVALID ,enter name:");
  47. scanf("%s",a);
  48. }
  49. printf("enter id:");
  50. scanf("%s",id);
  51. while(!validid(id)){
  52. printf("INVALID ,enter id:");
  53. scanf("%s",id);
  54. }
  55. root=insert(root,a,id);
  56. root2=insert2(root2,a,id);
  57.  
  58. break;
  59. case 2:
  60. printf("enter name to delete:");
  61. scanf("%s",a);
  62. while(!validname(a)){
  63. printf("INVALID ,enter name:");
  64. scanf("%s",a);
  65. }
  66. delete2(root2,search(root, a));
  67. delete(root,a);
  68. break;
  69. case 3:
  70. traverse(root);
  71. printf("tree 2\n");
  72. traverse(root2);
  73.  
  74. break;
  75. case 4:
  76. printf("enter name to search:");
  77. scanf("%s",a);
  78. while(!validname(a)){
  79. printf("INVALID ,enter name:");
  80. scanf("%s",a);
  81. }
  82. search(root,a);
  83. break;
  84. case 5:
  85. printf("enter id to search:");
  86. scanf("%s",id);
  87. while(!validid(id)){
  88. printf("INVALID ,enter id:");
  89. scanf("%s",id);
  90. }
  91. search2(root2, id);
  92. break;
  93. case 6:
  94. printf("enter id to delete:");
  95. scanf("%s",id);
  96. while(!validid(id)){
  97. printf("INVALID ,enter id:");
  98. scanf("%s",id);
  99. }
  100. delete(root,search2(root2,id));
  101. delete2(root2, id);
  102. break;
  103. default:
  104. break;
  105. }
  106.  
  107. printf("enter choice:");
  108. scanf("%d",&choice);
  109. }
  110.  
  111. }
  112.  
  113. struct node * insert(struct node *root, char x[],char id[])
  114. {int a;
  115.  
  116. if(!root)
  117. {
  118. root=(struct node*)malloc(sizeof(struct node));
  119. free( root->data );
  120. free( root->id );// free previously allocated memory, if any
  121. root->data = strdup( x ); // malloc and copy
  122. root->id=strdup(id);
  123. root->left = NULL;
  124. root->right = NULL;
  125. // printf("1\n");
  126. return(root);
  127. }
  128. if((a=strcmp(root->data,x))>0){
  129. root->left = insert(root->left,x,id);
  130. }
  131. else
  132. {
  133. if(strcmp(root->data,x)<0)
  134. root->right = insert(root->right,x,id);
  135. }
  136. return(root);
  137. }
  138.  
  139. struct node * insert2(struct node *root, char x[],char id[])
  140. {
  141. if(!root)
  142. {
  143. root=(struct node*)malloc(sizeof(struct node));
  144. free( root->data );
  145. free( root->id );// free previously allocated memory, if any
  146. root->data = strdup( x ); // malloc and copy
  147. root->id=strdup(id);
  148. root->left = NULL;
  149. root->right = NULL;
  150. // printf("1\n");
  151. return(root);
  152. }
  153. printf("string comp %d of %s of %s\n",strcmp(root->id,id),root->id,id);
  154. if((strcmp(root->id,id))>0){
  155. root->left = insert(root->left,x,id);
  156. printf("go left\n");
  157. }
  158. else
  159. {
  160. if(strcmp(root->id,id)<0){
  161. printf("go right\n");
  162. root->right = insert(root->right,x,id);}
  163. }
  164. return(root);
  165. }
  166.  
  167.  
  168. void traverse(struct node*ptr){
  169. if(ptr!=NULL){
  170. traverse(ptr->left);
  171. printf("%s %s\n",ptr->data,ptr->id);
  172. traverse(ptr->right);
  173. }
  174. }
  175. struct node * delete(struct node*T,char a[])
  176. {
  177. if(T==NULL) ; // element is not found in the tree
  178. else if(strcmp(a,T->data)<0)
  179. T->left = delete(T->left,a);
  180. else if(strcmp(a,T->data)>0)
  181. T->right = delete(T->right,a);
  182. else if((T->left)!=NULL && (T->right)!=NULL) { // node with 2 children
  183. temp = Findmin(T->right);
  184. T->data = temp->data;
  185. T->id= temp->id;
  186. T->right = delete(T->right,T->data); }
  187. else {
  188. temp = T;
  189. if(T->left==NULL)
  190. T = T->right;
  191. else if(T->right==NULL)
  192. T = T->left;
  193. // free(temp);
  194. }
  195. return T; }
  196.  
  197. struct node * Findmin(struct node*ptr){
  198. if((ptr->left)==NULL){
  199. // printf("min is :%s",ptr->data);
  200. return ptr;
  201. }
  202. else
  203. return Findmin(ptr->left);
  204.  
  205. }
  206.  
  207.  
  208.  
  209. char * search(struct node *ptr,char x[])
  210. {
  211. while (ptr!=NULL) {
  212.  
  213. if(strcmp(x,ptr->data)==0){
  214. printf("found data is :%s %s\n",ptr->data,ptr->id);
  215. return ptr->id;
  216. }
  217. else{
  218. if(strcmp(x,ptr->data)<0)
  219. ptr=ptr->left;
  220. else
  221. ptr=ptr->right;
  222. }
  223. }
  224. printf("not found\n");
  225. return NULL;
  226. }
  227. char* search2(struct node *ptr,char x[])
  228. {
  229. while (ptr!=NULL) {
  230.  
  231. if(strcmp(x,ptr->id)==0){
  232. printf("found data is :%s %s\n",ptr->data,ptr->id);
  233.  
  234. return ptr->data;
  235. }
  236. else{
  237. if(strcmp(x,ptr->id)<0)
  238. ptr=ptr->left;
  239. else
  240. ptr=ptr->right;
  241. }
  242. }
  243. printf("not found\n");
  244. return NULL;
  245. }
  246. struct node * delete2(struct node*T,char id[])
  247. {
  248.  
  249.  
  250. if(T==NULL) ; // element is not found in the tree
  251. else if(strcmp(id,T->id)<0)
  252. T->left = delete(T->left,id);
  253. else if(strcmp(id,T->id)>0)
  254. T->right = delete(T->right,id);
  255. else if((T->left)!=NULL && (T->right)!=NULL) { // node with 2 children
  256. temp = Findmin(T->right);
  257. T->data = temp->data;
  258. T->id= temp->id;
  259. T->right = delete(T->right,T->data); }
  260. else {
  261. temp = T;
  262. if(T->left==NULL)
  263. T = T->right;
  264. else if(T->right==NULL)
  265. T = T->left;
  266. // free(temp);
  267. }
  268. return T;
  269. }
  270. void load()
  271. {
  272. FILE * ptr;
  273. char data[10];
  274. char path[10];
  275. char id[10];
  276. printf("enter path of file:");
  277. scanf("%s",path);
  278. if((ptr=fopen("/Users/mahmoudelmarakshy/Documents/proj/Mah.txt","r"))==NULL)
  279. {
  280. printf("invalid file\n");
  281. }
  282. else
  283. {
  284. while(!feof(ptr))
  285. {
  286. fscanf(ptr,"%[^,],%s\n",data,id);
  287. root=insert(root,data,id);
  288. root2=insert2(root2,data,id);
  289.  
  290. }
  291.  
  292. printf("FILE FOUND\n");
  293. printf("FILE LOADED\n");
  294.  
  295. }
  296. }
  297. int validname(char a[]){int c,flag=0;
  298. for(c=0; (a[c])!='\0'; c++)
  299. {
  300. if(isdigit(a[c]))
  301. flag=1;
  302. }
  303. if (flag==1)
  304. {
  305. return 0;
  306. }
  307. else
  308. return 1;
  309. }
  310. int validid(char a[]){int c,flag=0;
  311. for(c=0; (a[c])!='\0'; c++)
  312. {
  313. if(isalpha(a[c]))
  314. flag=1;
  315. }
  316. if (flag==1)
  317. {
  318. return 0;
  319. }
  320. else
  321. return 1;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement