ramytamer

Untitled

Jun 1st, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct Data {
  7.     int id;
  8.     char name[40];
  9. }Data;
  10.  
  11. typedef struct Node{
  12.     Data data;
  13.     struct Node *left, *right;
  14. }Node;
  15.  
  16. typedef Node Tree;
  17.  
  18.  
  19. // FUNCTION TO GET DATA OF NODE
  20. // #DATA
  21. int data(Node *node){
  22.     return node->data.id;
  23. }
  24. // FUNCTION TO GET DATA OF NODE
  25.  
  26. // START OF QUEUE STUFF
  27. typedef struct qNode{
  28.     Node* data;
  29.     struct qNode *next;
  30. }qNode;
  31.  
  32. typedef struct Queue{
  33.     qNode *head,*tail;
  34.     int numberOfElements;
  35. }Queue;
  36.  
  37. void reset(Queue *q){ q->head = q->tail = NULL; q->numberOfElements=0; }
  38. int isEmpty(Queue *q) { return !q->head; }
  39. void push(Queue *q,Node* val) {
  40.     qNode *node = (qNode*) malloc(sizeof(qNode));
  41.     node->data = val;
  42.     node->next = NULL;
  43.     if(!q->head)
  44.         q->tail = node;
  45.     else
  46.         q->head->next = node;
  47.     q->head = node;
  48.     q->numberOfElements++;
  49. }
  50.  
  51. Node* pop(Queue *q) {
  52.     if(!isEmpty(q)) {
  53.         if(!q->tail)
  54.             return NULL;
  55.         else{
  56.             Node* x = q->tail->data;
  57.             if(!q->tail->next)
  58.                 reset(q);
  59.             else
  60.                 q->tail = q->tail->next;
  61.             q->numberOfElements--;
  62.             return x;
  63.         }
  64.     }
  65.     return NULL;
  66. }
  67.  
  68. void empty(Queue *q){
  69.     while(!isEmpty(q))
  70.         pop(q);
  71. }
  72.  
  73. Node* getFirst(Queue q){
  74.     return pop(&q);
  75. }
  76.  
  77. void display(Queue q){
  78.     printf("[\n" );
  79.     while(!isEmpty(&q)) {
  80.         Node * node = pop(&q);
  81.         printf("Name: %-20s:%-4d:%p\n",node->data.name,data(node),node );
  82.     }
  83.     printf("\b\b  \n");
  84.     printf("]\n");
  85. }
  86.  
  87. int count(Queue *q) { return q->numberOfElements;}
  88. // END   OF QUEUE STUFF
  89.  
  90. Node * newNode(int id,char *name){
  91.     Node* node = (Node*) malloc(sizeof(Node));
  92.     node->data.id = id;
  93.     strcpy(node->data.name, name);
  94.     node->left = node->right = NULL;
  95.     return node;
  96. }
  97.  
  98. Node * addRight(Node *root,int id,char *name){
  99.     if(root) {
  100.         Node* new = newNode(id,name);
  101.         root->right = new;
  102.         return new;
  103.     }
  104.     return NULL;
  105. }
  106.  
  107. Node * addLeft(Node *root,int id,char *name){
  108.     if(root) {
  109.         Node* new = newNode(id,name);
  110.         root->left = new;
  111.         return new;
  112.     }
  113.     return NULL;
  114. }
  115.  
  116.  
  117. void strtoupper(char *str){
  118.     int i=0; for(i=0;i<strlen(str);i++) str[i] = toupper(str[i]);
  119. }
  120.  
  121. // START OF TRAVERSING FUNCTIONS.
  122. // #PRETRAVERS
  123. void preTravers(Tree *root){
  124.     if(root) {
  125.         printf("[%d]\n", root->data.id);
  126.         preTravers(root->left);
  127.         preTravers(root->right);
  128.     }
  129. }
  130.  
  131. // #POSTTRAVERS
  132. void postTravers(Tree *root){
  133.     if(root) {
  134.         postTravers(root->left);
  135.         postTravers(root->right);
  136.         printf("[%d]\n", root->data.id);
  137.     }
  138. }
  139.  
  140. // #INTRAVERS
  141. void inTravers(Tree *root){
  142.     if(root) {
  143.         inTravers(root->left);
  144.         printf("Name: %-20s, ID: %d\t%p \n",root->data.name, root->data.id,root);
  145.         inTravers(root->right);
  146.     }    
  147. }
  148. // END OF TRAVERSING FUNCTIONS.
  149.  
  150.  
  151. // START FUNCTION TO CHECK IF THERE ARE ANY CHILD FOR A NODE
  152. // #HASCHILD
  153. int hasChild(Node *node){
  154.     return node->right || node->left;
  155. }
  156. // END   FUNCTION TO CHECK IF THERE ARE ANY CHILD FOR A NODE
  157.  
  158. // #SAVETOFILE
  159. void saveToFile(Tree *root,FILE *fp){
  160.     if(root) {
  161.         saveToFile(root->left,fp);
  162.         saveToFile(root->right,fp);
  163.         fprintf(fp,"%s,%d",root->data.name,root->data.id); fprintf(fp, "\n");
  164.     }
  165. }
  166.  
  167. // START SHOWING FUNCTIONS.
  168. // #INSHOW
  169. void inShow(Tree *root,int currentDepth){
  170.     if(root && data(root)!=~(1<<31)) {
  171.         inShow(root->right,currentDepth+1);
  172.         int i; for(i=0;i<currentDepth;i++) printf("\t");
  173.         printf("Name: %s,ID: %3d::%p ",root->data.name,root->data.id,root);
  174.         inShow(root->left,currentDepth+1);
  175.     }else
  176.         printf("\n");
  177. }
  178. // END SHOWING FUNCTIONS.
  179.  
  180. // START OF INSERTING IN NST FUNCTION.
  181. // #INSERTINID
  182. void insertInId(Tree *root,int id,char *name){
  183.     Node * node = newNode(id,name);
  184.     if(root) {
  185.         if(id < root->data.id ){
  186.             // GO LEFT
  187.             // if there is no left node
  188.             if(!root->left)
  189.                 root->left = node;
  190.             else // there is a node insert to it
  191.                 insertInId(root->left,id,name);
  192.         }else{
  193.             // GO RIGHT
  194.             // if there is no right node
  195.             if(!root->right)
  196.                 root->right = node;
  197.             else
  198.                 insertInId(root->right,id,name);
  199.         }
  200.     }
  201.     // return node;
  202. }
  203.  
  204. // #INSERTINNAME
  205. void insertInName(Tree *root,int id,char *name){
  206.     Node *node = newNode(id,name);
  207.     if(root){
  208.         char fname[strlen(name)], froot[strlen(root->data.name)];
  209.         strcpy(fname,name); strcpy(froot,root->data.name);
  210.         strtoupper(fname); strtoupper(froot);
  211.         if(strcmp(froot,fname) > 0){ // root->data.name is larger than name
  212.             if(!root->left)
  213.                 root->left = node;
  214.             else
  215.                 insertInName(root->left,id,name);
  216.         }else{
  217.             if(!root->right)
  218.                 root->right = node;
  219.             else
  220.                 insertInName(root->right,id,name);
  221.         }
  222.     }
  223. }
  224. // END   OF INSERTING IN BST FUNCTION.
  225.  
  226. // START OF FUNCTION TO GET SMALLEST ELEMENT.
  227. // #GETSMALLEST
  228. Node * getSmallest(Tree *root){
  229.     if(root->left || root->right) {
  230.         if(root->left) return getSmallest(root->left);
  231.         if(root->right && !root->left) return root;
  232.     }
  233.     return root;
  234.  
  235. }
  236. // END   OF FUNCTION TO GET SMALLEST ELEMENT.
  237.  
  238. Queue q; int dummy=0;
  239.  
  240. // START OF SEARCHING IN BST FUNCTION.
  241. // #SEARCHBYID
  242. Node * searchById (Tree *root,int id){
  243.     if (!root)
  244.         return NULL;
  245.     if (root->data.id == id)
  246.         return root;
  247.     if (id < root->data.id)
  248.         return searchById(root->left,id);
  249.     else
  250.         return searchById(root->right,id);
  251. }
  252.  
  253. // #SEARCHBYNAME
  254. Node * searchByName (Tree *root,char *name,int id){
  255.     char fname[strlen(name)], froot[strlen(root->data.name)];
  256.     strcpy(fname,name); strcpy(froot,root->data.name);
  257.     strtoupper(fname); strtoupper(froot);
  258.     if (!root)
  259.         return NULL;
  260.     if(strstr(froot,fname)){
  261.         // printf("%-20s:%-4d:%p\n",root->data.name,data(root),root );
  262.         if(id!=-1 && ((root->data.id)==id)){
  263.             empty(&q);
  264.             dummy = 1;
  265.             push(&q,root);
  266.             return root;
  267.             exit(0);
  268.         }else if(!dummy)
  269.             push(&q,root);
  270.         if(root->left)
  271.             searchByName(root->left,name,id);
  272.         if(root->right)
  273.             searchByName(root->right,name,id);
  274.     }else{
  275.         /*if(strcmp(froot,fname) < 0){ // root->data.name is larger than name (go right)
  276.             // printf("comparing: %s,%s...go right\n",froot,fname);
  277.             if(root->left)
  278.                 return searchByName(root->left,name,id);
  279.         }else if(strcmp(froot,fname) > 0){
  280.             // printf("comparing: %s,%s...go left\n",froot,fname);
  281.             if(root->right)
  282.                 return searchByName(root->right,name,id);
  283.         }*/
  284.             if(root->right)
  285.                 searchByName(root->right,name,id);
  286.             if(root->left)
  287.                 searchByName(root->left,name,id);
  288.     }
  289.     return NULL;
  290. }
  291.  
  292. // END   OF SEARCHING IN BST FUNCTION.
  293.  
  294. // START OF FUNCTION TO GET PARENT OF NODE.
  295. // #GETPARENT
  296. Node * getParent(Tree *root,Node *node){
  297.     if(root) {
  298.         if(root->left == node || root->right == node) {
  299.             return root;
  300.         }else{
  301.             if(node->data.id < root->data.id)
  302.                 root = root->left;
  303.             else
  304.                 root = root->right;
  305.             return getParent(root,node);
  306.         }
  307.     }
  308.     return NULL;
  309. }
  310.  
  311. char * name(Node* node){
  312.     strtoupper(node->data.name);
  313.     return (node->data.name);
  314. }
  315.  
  316.  
  317. // #fast
  318. Node * getParentName(Tree *root,Node *node){
  319.     if(root) {
  320.         if( root->left == node || root->right == node)
  321.             return root;
  322.         else{
  323.             char fname[strlen(root->data.name)]; strcpy(fname,root->data.name) ;
  324.             char lname[strlen(node->data.name)]; strcpy(lname,node->data.name);
  325.             strtoupper(fname); strtoupper(lname);
  326.             if(strcmp(fname,lname) < 0 ){
  327.                 root = root->right;
  328.             }else{
  329.                 root = root->left;
  330.             }
  331.             return getParentName(root,node);
  332.         }
  333.     }
  334.     return NULL;
  335. }
  336. // END   OF FUNCTION TO GET PARENT OF NODE.
  337.  
  338. // START OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  339. // #GETMAX
  340. Node * getMax(Tree *root){
  341.     return !root->right ? root : getMax(root->right);
  342. }
  343. // #GETMIN
  344. Node * getMin(Tree *root){
  345.     return !root->left  ? root : getMin(root->left);
  346. }
  347.  
  348. Node * getMaxName(Tree *root){
  349.     return !root->right ? root : getMaxName(root->right);
  350. }
  351. Node * getMinName(Tree *root){
  352.     return !root->left ? root : getMinName(root->left);
  353. }
  354.  
  355. // END   OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  356.  
  357. // START OF DELETING FROM BST FUNCTION.
  358. // #DELETE
  359. void delete(Tree ** root, Node * parent, Node * node){
  360.     if(!parent && node) {
  361.         if(!node->left && !node->right){ // Not a single child  
  362.             (*root)->data.id = ~(1<<31);
  363.         }else if(!node->left && node->right){ // only RIGHT child
  364.             Node *minNode = getMin((*root)->right); Node *parentMinNode = getParent((*root),minNode);
  365.             printf("minnode: %s,minnode->right: %s\n",minNode->data.name,minNode->right->data.name );
  366.             (parentMinNode->left == minNode) ? (parentMinNode->left = NULL) : (parentMinNode->right = NULL);
  367.             minNode->left = (*root)->left;
  368.             (*root) = minNode;
  369.         } else if(node->left && !node->right){ // only LEFT child
  370.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  371.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  372.             maxNode->right = (*root)->right;
  373.             (*root) = maxNode;
  374.         } else if(node->left && node->right){ // have BOTH children
  375.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  376.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  377.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  378.             (*root) = maxNode;
  379.         }
  380.         return ;
  381.     }else if(parent && node){
  382.         if(!node->left && !node->right){ // Not a single child
  383.             (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL) ;
  384.         }else if(!node->left && node->right){ // only RIGHT child
  385.             (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right) ;
  386.         }else if(node->left && !node->right){ // only LEFT child
  387.             (parent->left == node) ? (parent->left = node->left) : (parent->right = node->right) ;
  388.         }else if(node->left && node->right){ // have BOTH children
  389.             Node *maxNode = getMax(node->left); Node *parentMaxNode = getParent((*root),maxNode);
  390.             (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  391.             (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  392.             maxNode->left = node->left; maxNode->right = node->right;
  393.         }
  394.         free(node);
  395.     }
  396. }
  397.  
  398. void deleteFromName(Tree **root,Node *parent,Node * node){
  399.     if(!parent && node) {
  400.         if(!node->left && !node->right) {
  401.             (*root)->data.id = ~(1<<31);
  402.         }else if(!node->left && node->right){
  403.             Node *minNode = getMinName((*root)->right);
  404.             Node *parentMinNode = getParentName((*root),minNode);
  405.             (parentMinNode->left == minNode) ? (parentMinNode->left = NULL) : (parentMinNode->right = NULL);
  406.             minNode->left = (*root)->left;
  407.             (*root) = minNode;
  408.         }else if(node->left && !node->right){
  409.             Node *maxNode = getMaxName((*root)->left);
  410.             Node * parentMaxNode = getParentName((*root),maxNode);
  411.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  412.             maxNode->right = (*root)->right;
  413.             (*root) = maxNode;
  414.         } else if(node->left && node->right){
  415.             Node *maxNode = getMaxName((*root)->left);
  416.             Node * parentMaxNode = getParentName((*root),maxNode);
  417.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  418.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  419.             (*root) = maxNode;
  420.         }
  421.         return ;
  422.     }else if(parent && node) {
  423.         if(!node->left && !node->right) {
  424.             (parent->left == node ) ? (parent->left=NULL) : (parent->right=NULL);
  425.         }else if(!node->left && node->right){
  426.             (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right);
  427.         } else if(node->left && !node->right){
  428.             (parent->left == node ) ? (parent->left = node->left) : (parent->right = node->left);
  429.         }else if(node->left && node->right){
  430.             Node * maxNode = getMaxName((*root)->left);
  431.             Node * parentMaxNode = getParentName((*root),maxNode);
  432.             (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);            
  433.             (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  434.             maxNode->left = node->left; maxNode->right = node->right;
  435.         }
  436.         free(node);
  437.     }
  438. }
  439. // END   OF DELETING FROM BST FUNCTION.
  440.  
  441. // START OF FUNCTION THAT LOADS NAMES FROM FILE
  442. // #LOADIDTREE
  443. void loadIdTree(Tree **root){
  444.     char line[50]; int id;
  445.     FILE *fp = fopen("students_info.txt", "r");
  446.     while (fgets(line,50,fp)){
  447.         char name[40];
  448.         strcpy(name,strtok(line,","));
  449.         id = atoi(strtok(NULL,"\n")); // convert string to number
  450.         (!*root) ? *root = (newNode(id,name)) : (insertInId((*root),id,name));
  451.     }
  452.     fclose(fp);
  453. }
  454.  
  455. // #LOADNAMETREE
  456. void loadNameTree(Tree **root){
  457.     char line[50]; int id;
  458.     FILE *fp = fopen("students_info.txt", "r");
  459.     while (fgets(line,50,fp)){
  460.         char name[40];
  461.         strcpy(name,strtok(line,","));
  462.         id = atoi(strtok(NULL,"\n")); // convert string to number
  463.         (!*root) ? *root = (newNode(id,name)) : (insertInName((*root),id,name));
  464.     }
  465.     fclose(fp);
  466. }
  467. // END   OF FUNCTION THAT LOADS NAMES FROM FILE
  468.  
  469. void displaySortedTree(Tree *root){ inTravers(root); }
  470.  
  471. void edit(Tree **idTree,Tree **nameTree,Node * node,char *name,int id){
  472.     deleteFromName(nameTree,getParentName(*nameTree,node),node);
  473.     delete(idTree,getParent(*idTree,node),node);
  474.     insertInId(*idTree,id,name);
  475.     insertInName(*nameTree,id,name);
  476. }
  477.  
  478. int main(){
  479.     reset(&q);
  480.     system("cls");
  481.  
  482.     Tree *idTree = NULL;
  483.     Tree *nameTree = NULL;
  484.     // loadIdTree(&nameTree);
  485.     loadNameTree(&nameTree);
  486.     loadIdTree(&idTree);
  487.  
  488.     printf("traverse id\n");
  489.     inTravers(idTree);
  490.     printf("traverse name\n");
  491.     inTravers(nameTree);
  492.  
  493.     /*FILE *fp = fopen("students_info","w");
  494.     saveToFile(nameTree,fp);
  495.     fclose(fp);*/
  496.     searchByName(nameTree,"",80);
  497.     Node * node = pop(&q);
  498.  
  499.     edit(&idTree,&nameTree,node,"new name",2162);
  500.  
  501.     printf("traverse id\n");
  502.     inTravers(idTree);
  503.     printf("traverse name\n");
  504.     inTravers(nameTree);
  505.     return 0;
  506. }
Advertisement
Add Comment
Please, Sign In to add comment