Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "trees.h"
- #include <string.h>
- //
- // trees.c
- // final-trial
- //
- // Created by Ahmed Omar on 5/27/14.
- // Copyright (c) 2014 Ahmed Omar. All rights reserved.
- //
- #include <stdio.h>
- #include "trees.h"
- #include <string.h>
- //ana 3adlt hena
- //if 2 have the same id, the id will be inserted in the right node
- node_id* insert_id(node_id* tree, int id, char* name)
- {
- // this node will be inserted
- node_id* temp = NULL;
- //if tree is empty
- if (!tree)
- {
- temp = (node_id*)malloc(sizeof(node_id));
- temp->left = temp->right = NULL;
- temp->id = id;
- strcpy(temp->name, name);
- //insert
- //tree = temp;
- return temp;
- }
- //insert node in left subtree
- if (id < tree->id)
- {
- tree->left = insert_id(tree->left, id, name);
- //insert node in right subtree
- }
- else
- {
- tree->right = insert_id(tree->right, id, name);
- }
- return tree;
- }
- //ana 3adlt hena
- //the same amendment as insert_id
- node_id* insert_name(node_id* tree, int id, char* name)
- {
- // this node will be inserted
- node_id* temp = NULL;
- //if tree is empty
- if (!tree)
- {
- temp = (node_id*)malloc(sizeof(node_id));
- temp->left = temp->right = NULL;
- temp->id = id;
- strcpy(temp->name, name);
- //insert
- //tree = temp;
- return temp;
- }
- //insert node in left subtree
- if (strcmp(tree->name,name)>0)
- {
- tree->left = insert_name(tree->left, id, name);
- //insert node in right subtree
- }
- else
- {
- tree->right = insert_name(tree->right, id, name);
- }
- return tree;
- }
- void insert(node_id** tree_id, node_id** tree_name, int id, char* name)
- {
- //insert to tree_id
- (*tree_id) = insert_id(*tree_id, id, name);
- //insert to tree_name
- (*tree_name) = insert_name(*tree_name, id, name);
- }
- //ana 3adel hena x3
- //msh hatefre2 en kan name aw id
- void print_preorder(node_id* tree,int c)
- {
- if (tree && tree->id != ~(1<<31))
- {
- int i;
- for(i=0; i<c; i++) printf("\t");
- printf("%d %s\n", tree->id, tree->name);
- print_preorder(tree->left,c+1);
- print_preorder(tree->right,c+1);
- }
- else
- printf("\n");
- }
- void print_inorder(node_id* tree,int c)
- {
- if (tree && tree->id != ~(1<<31))
- {
- print_inorder(tree->left,c+1);
- int i;
- for(i=0; i<c; i++) printf("\t");
- printf("%d %s\n", tree->id, tree->name);
- print_inorder(tree->right,c+1);
- }
- else
- printf("\n");
- }
- void print_postorder(node_id* tree, int c)
- {
- if (tree && tree->id != ~(1<<31))
- {
- print_postorder(tree->left,c+1);
- print_postorder(tree->right,c+1);
- int i;
- for(i=0; i<c; i++) printf("\t");
- printf("%d %s\n", tree->id, tree->name);
- }
- else
- printf("\n");
- }
- void search_id_2(node_id** tree, int id)
- {
- if (!(*tree))
- {
- return;
- }
- if (id == (*tree)->id)
- {
- printf("%s %d\n",(*tree)->name,(*tree)->id);
- }
- if (id < (*tree)->id)
- {
- search_id_2(&((*tree)->left), id);
- }
- else
- {
- search_id_2(&((*tree)->right), id);
- }
- return;
- }
- void search_name_2(node_id** tree, char* name)
- {
- if (!(*tree))
- {
- return;
- }
- if (strstr((*tree)->name,name) != NULL)
- {
- printf("%s %d\n",(*tree)->name,(*tree)->id);
- }
- if (strcmpi(name, (*tree)->name) < 0)
- {
- search_name_2(&((*tree)->left), name);
- }
- else
- {
- search_name_2(&((*tree)->right), name);
- }
- return NULL;
- }
- //searh in tree_id
- //ana 3adelt hena
- node_id* search_id(node_id** tree, int id)
- {
- if (!(*tree))
- {
- return NULL;
- }
- if (id == (*tree)->id)
- {
- return *tree;
- }
- else if (id < (*tree)->id)
- {
- search_id(&((*tree)->left), id);
- }
- else if (id > (*tree)->id)
- {
- search_id(&((*tree)->right), id);
- }
- return NULL;
- }
- //searh in tree_name
- /*node_id* search_name(node_id** tree, char* name)
- {
- if (!(*tree))
- {
- return NULL;
- }
- if (strstr((*tree)->name,name) != NULL)
- {
- return *tree;
- }
- else if (strcmpi(name, (*tree)->name) < 0)
- {
- search_name(&((*tree)->left), name);
- }
- else if (strcmpi(name, (*tree)->name) > 0)
- {
- search_name(&((*tree)->right), name);
- }
- return NULL;
- }*/
- void search_name(node_id* root , char *name , node_id* ptrarray[] , int*n)
- {
- if (root == NULL)
- return ;
- else if(strstr(tree_name->name,name) != NULL)
- {
- ptrarray[*n] = root;
- (*n)++;
- search_name(root->left , name,a,n);
- search_name(root->right , name,a,n);
- }
- else if(stricmp(name,root->name) > 0)
- searchByName(root->right , name,a,n);
- else if(stricmp(name,root->name) < 0)
- searchByName(root->left , name,a,n);
- }
- //ana 3adlt hena
- //msh hatefr2 name aw id
- node_id * min_value_node_id(node_id* node)
- {
- node_id* current = node;
- while (current->left != NULL)
- current = current->left;
- return current;
- }
- //delete node by id
- //3adelt hena
- //ÎáíÊå áæ ãÓÍ åäÇ íãÓÍ Ýí ÇáÊÑí ÇáÊÇäíÉ ÚÔÇä ÇáÊÚÏíá íÈÞì ÚÇáÇÊäíä
- node_id* delete_id(node_id* tree, int id)
- {
- if (tree == NULL)
- {
- return tree;
- }
- if (id < tree->id)
- //delete node from left subtree
- tree->left = delete_id(tree->left, id);
- else if (id > tree->id)
- //delete node from right subtree
- tree->right = delete_id(tree->right, id);
- else
- {
- if (tree->left == NULL)
- {
- node_id* temp = tree->right;
- free(tree);
- return temp;
- }
- else if (tree->right == NULL)
- {
- node_id* temp = tree->left;
- free(tree);
- return temp;
- }
- node_id* temp = min_value_node_id(tree->right);
- tree->id = temp->id;
- strcpy(tree->name, temp->name);
- tree->right = delete_id(tree->right, temp->id);
- }
- return tree;
- }
- node_id* delete_name(node_id* tree, char* name, int id)
- {
- if (tree == NULL)
- {
- return tree;
- }
- if (strcmp(name, tree->name) > 0)
- tree->left = delete_name(tree->left, name,id);
- else if (strcmp(name, tree->name) < 0)
- {
- tree->right = delete_name(tree->right, name),id;
- }
- else
- {
- if (tree->left == NULL && tree->id == id)
- {
- node_id* temp = tree->right;
- free(tree);
- return temp;
- }
- else if (tree->right == NULL && tree->id == id)
- {
- node_id* temp = tree->left;
- free(tree);
- return temp;
- }
- node_id* temp = min_value_node_id(tree->right);
- strcpy(tree->name, temp->name);
- tree->id = temp->id;
- tree->right = delete_name(tree->right, temp->name);
- }
- return tree;
- }
- void reed_file_to_tree(node_id** tree_id, node_id** tree_name, char* file_name)
- {
- FILE* fp = fopen("/Users/BoshraNour/Desktop/final-trial/final-trial/project.txt", "r");
- if (fp == NULL)
- {
- printf("Error open file\n");
- return;
- }
- char buf[1024], *p;
- //get line from file
- while (fgets(buf, sizeof(buf), fp) != NULL)
- {
- if ((p = strchr(buf, '\n')) == NULL)
- {
- fprintf(stderr, "input line too long.\n");
- return;
- }
- *p = '\0';
- // our line should be bigger than 1
- if (strlen(buf) > 1)
- {
- //split line
- char* pch = strtok(buf, ",");
- char name[40];
- //copy name
- strncpy(name, pch, 40);
- pch = strtok(NULL, ",");
- //copy id
- int id = atoi(pch);
- //insert
- (*tree_id) = insert_id(*tree_id, id, name);
- (*tree_name) = insert_name(*tree_name, id, name);
- }
- }
- fclose(fp);
- }
- void write_tree_to_file(node_id* tree_id, FILE* fp)
- {
- if (fp == NULL)
- {
- printf("Error file\n");
- return;
- }
- char line[60];
- char numb[20];
- if (tree_id)
- {
- // convert int to char
- sprintf(numb, "%d", tree_id->id);
- //add strings
- strcpy(line, tree_id->name);
- strcpy(line+strlen(tree_id->name), ",");
- strcpy(line+strlen(line), numb);
- //add sym
- strcat(line,"\n");
- //write to file
- fputs(line, fp);
- write_tree_to_file(tree_id->left, fp);
- write_tree_to_file(tree_id->right, fp);
- }
- //fclose(fp);
- }
- int main()
- {
- node_id* root_id;
- node_id* root_name;
- root_name = NULL;
- root_id = NULL;
- printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
- printf("choose:\n");
- printf("1- insert data\n");
- printf("2- write file to tree\n3- print\n");
- printf("4- delete\n");
- printf("5- search\n");
- printf("6- read file to tree\n7- quit\n\n");
- int command;
- char co[20];
- char name[40];
- char id_2[10];
- int id;
- char c;
- printf("Enter command: ");
- scanf(" %s", co);
- command = atoi(co);
- do
- {
- if(command<1 || command>7)
- {
- system("cls");
- printf("enter valid number\n\n");
- }
- else
- switch (command)
- {
- case 7:
- {
- system("cls");
- printf("THANK YOU!\n\n");
- printf("Ahmad Omar 2715 & Ahmad Abdulaziz 2210\n\n");
- exit(0);
- break;
- }
- case 1:
- {
- system("cls");
- printf("Enter name: ");
- while((c= getchar()) != '\n' && c != EOF);
- //gets(name);
- int j,f;
- scanf(" %[^\n]",name);
- printf("Enter id: ");
- scanf(" %[^\n]",id_2);
- int l=strlen(name);
- for(j=0; j<l; j++)
- if ( !(name[j]=='\n' || name[j] == 32 || (name[j] >= 65 && name[j] <= 90) || (name[j] >= 97 && name[j] <= 122) ))
- {
- printf("ERROR in name\n");
- f=1;
- break;
- }
- if(f==1)
- break;
- node_id *test = search_id(&root_id,atoi(id_2));
- if(test != NULL && atoi(id_2) == test->id)
- printf("ERROR, id already exist!\n");
- else if(atoi(name)==0 && atoi(id_2)> 0 && atoi(id_2) == atof(id_2))
- {
- id= atoi(id_2);
- insert(&root_id, &root_name, id, name);
- system("cls");
- printf("%d %s inserted succesfuly\n\n",id,name);
- }
- else
- {
- printf("\nERROR in name or id!\n");
- }
- break;
- }
- case 2:
- {
- system("cls");
- char file_name[50];
- printf("Enter file name: ");
- scanf("%s", file_name);
- FILE* file = fopen(file_name, "w");
- write_tree_to_file(root_id, file);
- fclose(file);
- }
- break;
- case 3:
- {
- char z[15];
- system("cls");
- printf("choose type of print:\n");
- printf("1- print preorder by id\n");
- printf("2- print preorder by name\n3- print post order by id\n");
- printf("4- print post order by name\n5- inorder by id\n");
- printf("6- print inorder by name\n");
- printf("your choice: ");
- scanf(" %[^\n]s",z);
- int x=atoi(z);
- if(x==1)
- print_preorder(root_id,0);
- else if(x==2)
- print_preorder(root_name,0);
- else if(x==3)
- print_postorder(root_id,0);
- else if(x==4)
- print_postorder(root_name,0);
- else if(x==5)
- print_inorder(root_id,0);
- else if(x==6)
- print_inorder(root_name,0);
- else
- {
- system("cls");
- printf("enter a valid choice!\n");
- break;
- }
- break;
- }
- case 4:
- {
- /*char z2[15];
- system("cls");
- printf("1- by name\n2- by id\n");
- printf("your choice: ");
- scanf(" %[^\n]s",z2);
- int x2=atoi(z2);
- if(x==1)
- {
- node_id*a[100];
- int n = 0;
- int id;
- searchByName(nroot,name,a,&n);
- if (n == 0)
- {
- printf("Not Found \n");
- }
- else
- {
- int i,ch;
- for ( i = 0 ; i < n ; i++)
- {
- printf("%d %s\n",ptrarray[i]->id , ptrarray[i]->name);
- }
- printf ( "enter the id of student you want to delete: \n");
- scanf("%d",&ch);
- delete_name(tree_name, name,ch);
- }
- else if(x2==2)
- {
- system("cls");
- printf("Enter id: ");
- scanf("%d", &id);
- node_id* temp = search_id(&root_id, id);
- if (temp != NULL)
- {
- strcpy(name, temp->name);
- }
- if(temp==NULL)
- printf("NOT FOUND.\n\n");
- else
- {
- root_id = delete_id(root_id, id);
- root_name = delete_name(root_name,name);
- printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
- }
- }
- else
- {
- system("cls");
- printf("enter a valid choice.\n");
- break;
- }
- break;*/
- char z2[15];
- system("cls");
- printf("1- by name\n2- by id\n");
- printf("your choice: ");
- scanf(" %[^\n]s",z2);
- int x2=atoi(z2);
- if(x2==1)
- {
- system("cls");
- printf("Enter name: ");
- while((c= getchar()) != '\n' && c != EOF);
- gets(name);
- node_id* temp = search_name(&root_name, name);
- if (temp != NULL)
- {
- id = temp->id;
- }
- if(temp == NULL)
- printf("NOT FOUND.\n\n");
- else
- {
- root_id = delete_id(root_id, id);
- root_name = delete_name(root_name, name);
- printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
- }
- }
- else if(x2==2)
- {
- system("cls");
- printf("Enter id: ");
- scanf("%d", &id);
- node_id* temp = search_id(&root_id, id);
- if (temp != NULL)
- {
- strcpy(name, temp->name);
- }
- if(temp==NULL)
- printf("NOT FOUND.\n\n");
- else
- {
- root_id = delete_id(root_id, id);
- root_name = delete_name(root_name,name);
- printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
- }
- }
- else
- {
- system("cls");
- printf("enter a valid choice.\n");
- break;
- }
- break;
- }
- case 5:
- {
- system("cls");
- char z3[15];
- printf("1- by id\n2- by name\n");
- printf("your choice: ");
- scanf("% [^\n]s",z3);
- int x3=atoi(z3);
- if(x3==2)
- {
- system("cls");
- printf("Enter name: ");
- while((c= getchar()) != '\n' && c != EOF);
- gets(name);
- search_name_2(&root_name, name);
- }
- else if(x3==1)
- {
- system("cls");
- printf("Enter id: ");
- int id;
- scanf("%d", &id);
- search_id_2(&root_id, id);
- }
- else
- {
- system("cls");
- printf("enter a valid choice!\n");
- break;
- }
- break;
- }
- case 6:
- {
- system("cls");
- char file_name[50];
- printf("Enter file name: ");
- scanf("%s", file_name);
- reed_file_to_tree(&root_id, &root_name, file_name);
- break;
- }
- }
- printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
- printf("choose:\n");
- printf("1- insert data\n");
- printf("2- write file to tree\n3- print\n");
- printf("4- delete\n");
- printf("5- search\n");
- printf("6- read file to tree\n7- quit\n\n");
- printf("Enter command: ");
- scanf(" %s", co);
- command = atoi(co);
- }
- while (command != ~(1<<31));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement