Advertisement
ramytamer

Untitled

May 31st, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.65 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. }Queue;
  35.  
  36. void reset(Queue *q){ q->head = q->tail = NULL; }
  37. int isEmpty(Queue *q) { return !q->head; }
  38. void push(Queue *q,Node* val) {
  39.     qNode *node = (qNode*) malloc(sizeof(qNode));
  40.     node->data = val;
  41.     node->next = NULL;
  42.     if(!q->head)
  43.         q->tail = node;
  44.     else
  45.         q->head->next = node;
  46.     q->head = node;
  47. }
  48.  
  49. Node* pop(Queue *q) {
  50.     if(!isEmpty(q)) {
  51.         if(!q->tail)
  52.             return NULL;
  53.         else{
  54.             Node* x = q->tail->data;
  55.             if(!q->tail->next)
  56.                 reset(q);
  57.             else
  58.                 q->tail = q->tail->next;
  59.             return x;
  60.         }
  61.     }
  62.     return NULL;
  63. }
  64.  
  65. void empty(Queue *q){
  66.     while(!isEmpty(q))
  67.         pop(q);
  68. }
  69.  
  70. Node* getFirst(Queue q){
  71.     return pop(&q);
  72. }
  73.  
  74. void display(Queue q){
  75.     printf("[\n" );
  76.     while(!isEmpty(&q)) {
  77.         Node * node = pop(&q);
  78.         printf("Name: %-20s:%-4d:%p\n",node->data.name,data(node),node );
  79.     }
  80.     printf("\b\b  \n");
  81.     printf("]\n");
  82. }
  83. // END   OF QUEUE STUFF
  84.  
  85. Node * newNode(int id,char *name){
  86.     Node* node = (Node*) malloc(sizeof(Node));
  87.     node->data.id = id;
  88.     strcpy(node->data.name, name);
  89.     node->left = node->right = NULL;
  90.     return node;
  91. }
  92.  
  93. Node * addRight(Node *root,int id,char *name){
  94.     if(root) {
  95.         Node* new = newNode(id,name);
  96.         root->right = new;
  97.         return new;
  98.     }
  99.     return NULL;
  100. }
  101.  
  102. Node * addLeft(Node *root,int id,char *name){
  103.     if(root) {
  104.         Node* new = newNode(id,name);
  105.         root->left = new;
  106.         return new;
  107.     }
  108.     return NULL;
  109. }
  110.  
  111.  
  112. void strtoupper(char *str){
  113.     int i=0; for(i=0;i<strlen(str);i++) str[i] = toupper(str[i]);
  114. }
  115.  
  116. // START OF TRAVERSING FUNCTIONS.
  117. // #PRETRAVERS
  118. void preTravers(Tree *root){
  119.     if(root) {
  120.         printf("[%d]\n", root->data.id);
  121.         preTravers(root->left);
  122.         preTravers(root->right);
  123.     }
  124. }
  125.  
  126. // #POSTTRAVERS
  127. void postTravers(Tree *root){
  128.     if(root) {
  129.         postTravers(root->left);
  130.         postTravers(root->right);
  131.         printf("[%d]\n", root->data.id);
  132.     }
  133. }
  134.  
  135. // #INTRAVERS
  136. void inTravers(Tree *root){
  137.     if(root) {
  138.         inTravers(root->left);
  139.         printf("Name: %-20s, ID: %d\t%p \n",root->data.name, root->data.id,root);
  140.         inTravers(root->right);
  141.     }    
  142. }
  143. // END OF TRAVERSING FUNCTIONS.
  144.  
  145.  
  146. // START FUNCTION TO CHECK IF THERE ARE ANY CHILD FOR A NODE
  147. // #HASCHILD
  148. int hasChild(Node *node){
  149.     return node->right || node->left;
  150. }
  151. // END   FUNCTION TO CHECK IF THERE ARE ANY CHILD FOR A NODE
  152.  
  153. // #SAVETOFILE
  154. void saveToFile(Tree *root,FILE *fp){
  155.     if(root) {
  156.         saveToFile(root->left,fp);
  157.         saveToFile(root->right,fp);
  158.         fprintf(fp,"%s,%d",root->data.name,root->data.id); fprintf(fp, "\n");
  159.     }
  160. }
  161.  
  162. // START SHOWING FUNCTIONS.
  163. // #INSHOW
  164. void inShow(Tree *root,int currentDepth){
  165.     if(root) {
  166.         inShow(root->right,currentDepth+1);
  167.         int i; for(i=0;i<currentDepth;i++) printf("\t");
  168.         printf("Name: %s,ID: %3d::%p ",root->data.name,root->data.id,root);
  169.         inShow(root->left,currentDepth+1);
  170.     }else
  171.         printf("\n");
  172. }
  173. // END SHOWING FUNCTIONS.
  174.  
  175. // START OF INSERTING IN NST FUNCTION.
  176. // #INSERTINID
  177. void insertInId(Tree *root,int id,char *name){
  178.     Node * node = newNode(id,name);
  179.     if(root) {
  180.         if(id < root->data.id ){
  181.             // GO LEFT
  182.             // if there is no left node
  183.             if(!root->left)
  184.                 root->left = node;
  185.             else // there is a node insert to it
  186.                 insertInId(root->left,id,name);
  187.         }else{
  188.             // GO RIGHT
  189.             // if there is no right node
  190.             if(!root->right)
  191.                 root->right = node;
  192.             else
  193.                 insertInId(root->right,id,name);
  194.         }
  195.     }
  196.     // return node;
  197. }
  198.  
  199. // #INSERTINNAME
  200. void insertInName(Tree *root,int id,char *name){
  201.     Node *node = newNode(id,name);
  202.     if(root){
  203.         char fname[strlen(name)], froot[strlen(root->data.name)];
  204.         strcpy(fname,name); strcpy(froot,root->data.name);
  205.         strtoupper(fname); strtoupper(froot);
  206.         if(strcmp(froot,fname) > 0){ // root->data.name is larger than name
  207.             if(!root->left)
  208.                 root->left = node;
  209.             else
  210.                 insertInName(root->left,id,name);
  211.         }else{
  212.             if(!root->right)
  213.                 root->right = node;
  214.             else
  215.                 insertInName(root->right,id,name);
  216.         }
  217.     }
  218. }
  219. // END   OF INSERTING IN BST FUNCTION.
  220.  
  221. // START OF FUNCTION TO GET SMALLEST ELEMENT.
  222. // #GETSMALLEST
  223. Node * getSmallest(Tree *root){
  224.     if(root->left || root->right) {
  225.         if(root->left) return getSmallest(root->left);
  226.         if(root->right && !root->left) return root;
  227.     }
  228.     return root;
  229.  
  230. }
  231. // END   OF FUNCTION TO GET SMALLEST ELEMENT.
  232.  
  233. Queue q; int dummy=0;
  234.  
  235. // START OF SEARCHING IN BST FUNCTION.
  236. // #SEARCHBYID
  237. Node * searchById (Tree *root,int id){
  238.     if (!root)
  239.         return NULL;
  240.     if (root->data.id == id)
  241.         return root;
  242.     if (id < root->data.id)
  243.         return searchById(root->left,id);
  244.     else
  245.         return searchById(root->right,id);
  246. }
  247.  
  248. // #SEARCHBYNAME
  249. Node * searchByName (Tree *root,char *name,int id){
  250.     char fname[strlen(name)], froot[strlen(root->data.name)];
  251.     strcpy(fname,name); strcpy(froot,root->data.name);
  252.     strtoupper(fname); strtoupper(froot);
  253.     if (!root)
  254.         return NULL;
  255.     if(strstr(froot,fname)){
  256.         // printf("%-20s:%-4d:%p\n",root->data.name,data(root),root );
  257.         if(id!=-1 && ((root->data.id)==id)){
  258.             empty(&q);
  259.             dummy = 1;
  260.             push(&q,root);
  261.             return root;
  262.             exit(0);
  263.         }else if(!dummy)
  264.             push(&q,root);
  265.         if(root->left)
  266.             searchByName(root->left,name,id);
  267.         if(root->right)
  268.             searchByName(root->right,name,id);
  269.     }else{
  270.         /*if(strcmp(froot,fname) < 0){ // root->data.name is larger than name (go right)
  271.             // printf("comparing: %s,%s...go right\n",froot,fname);
  272.             if(root->left)
  273.                 return searchByName(root->left,name,id);
  274.         }else if(strcmp(froot,fname) > 0){
  275.             // printf("comparing: %s,%s...go left\n",froot,fname);
  276.             if(root->right)
  277.                 return searchByName(root->right,name,id);
  278.         }*/
  279.             if(root->right)
  280.                 searchByName(root->right,name,id);
  281.             if(root->left)
  282.                 searchByName(root->left,name,id);
  283.     }
  284.     return NULL;
  285. }
  286.  
  287. // END   OF SEARCHING IN BST FUNCTION.
  288.  
  289. // START OF FUNCTION TO GET PARENT OF NODE.
  290. // #GETPARENT
  291. Node * getParent(Tree *root,Node *node){
  292.     if(root) {
  293.         if(root->left == node || root->right == node) {
  294.             return root;
  295.         }else{
  296.             if(node->data.id < root->data.id)
  297.                 root = root->left;
  298.             else
  299.                 root = root->right;
  300.             return getParent(root,node);
  301.         }
  302.     }
  303.     return NULL;
  304. }
  305. // END   OF FUNCTION TO GET PARENT OF NODE.
  306.  
  307. // START OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  308. // #GETMAX
  309. Node * getMax(Tree *root){
  310.     return !root->right ? root : getMax(root->right);
  311. }
  312. // #GETMIN
  313. Node * getMin(Tree *root){
  314.     return !root->left  ? root : getMin(root->left);
  315. }
  316. // END   OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  317.  
  318. // START OF DELETING FROM BST FUNCTION.
  319. // #DELETE
  320. void delete(Tree ** root, Node * parent, Node * node){
  321.     if(!parent && node) {
  322.         if(!node->left && !node->right){ // Not a single child  
  323.             (*root)->data.id = ~(1<<31);
  324.         }else if(!node->left && node->right){ // only RIGHT child
  325.             Node *minNode = getMin((*root)->right); Node *parentMinNode = getParent((*root),minNode);
  326.             printf("minnode: %s,minnode->right: %s\n",minNode->data.name,minNode->right->data.name );
  327.             (parentMinNode->left == minNode) ? (parentMinNode->left = NULL) : (parentMinNode->right = NULL);
  328.             minNode->left = (*root)->left;
  329.             (*root) = minNode;
  330.         } else if(node->left && !node->right){ // only LEFT child
  331.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  332.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  333.             maxNode->right = (*root)->right;
  334.             (*root) = maxNode;
  335.         } else if(node->left && node->right){ // have BOTH children
  336.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  337.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  338.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  339.             (*root) = maxNode;
  340.         }
  341.         return ;
  342.     }else if(parent && node){
  343.         if(!node->left && !node->right){ // Not a single child
  344.             (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL) ;
  345.         }else if(!node->left && node->right){ // only RIGHT child
  346.             (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right) ;
  347.         }else if(node->left && !node->right){ // only LEFT child
  348.             (parent->left == node) ? (parent->left = node->left) : (parent->right = node->right) ;
  349.         }else if(node->left && node->right){ // have BOTH children
  350.             Node *maxNode = getMax(node->left); Node *parentMaxNode = getParent((*root),maxNode);
  351.             (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  352.             (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  353.             maxNode->left = node->left; maxNode->right = node->right;
  354.         }
  355.         free(node);
  356.     }
  357. }
  358. // END   OF DELETING FROM BST FUNCTION.
  359.  
  360. // START OF FUNCTION THAT LOADS NAMES FROM FILE
  361. // #LOADIDTREE
  362. void loadIdTree(Tree **root){
  363.     char line[50]; int id;
  364.     FILE *fp = fopen("students_info.txt", "r");
  365.     while (fgets(line,50,fp)){
  366.         char name[40];
  367.         strcpy(name,strtok(line,","));
  368.         id = atoi(strtok(NULL,"\n")); // convert string to number
  369.         (!*root) ? *root = (newNode(id,name)) : (insertInId((*root),id,name));
  370.     }
  371.     fclose(fp);
  372. }
  373.  
  374. // #LOADNAMETREE
  375. void loadNameTree(Tree **root){
  376.     char line[50]; int id;
  377.     FILE *fp = fopen("students_info.txt", "r");
  378.     while (fgets(line,50,fp)){
  379.         char name[40];
  380.         strcpy(name,strtok(line,","));
  381.         id = atoi(strtok(NULL,"\n")); // convert string to number
  382.         (!*root) ? *root = (newNode(id,name)) : (insertInName((*root),id,name));
  383.     }
  384.     fclose(fp);
  385. }
  386. // END   OF FUNCTION THAT LOADS NAMES FROM FILE
  387.  
  388. void displaySortedTree(Tree *root){ inTravers(root); }
  389.  
  390.  
  391. int main(){
  392.     reset(&q);
  393.     system("cls");
  394.  
  395.     Tree *idTree = NULL;
  396.     Tree *nameTree = NULL;
  397.     // loadIdTree(&nameTree);
  398.     loadNameTree(&nameTree);
  399.     loadIdTree(&idTree);
  400.  
  401.     /*FILE *fp = fopen("students_info","w");
  402.     saveToFile(nameTree,fp);
  403.     fclose(fp);*/
  404.     inShow(nameTree,0);
  405.  
  406.     searchByName(nameTree,"mu",-1);
  407.     display(q);
  408.  
  409.     return 0;
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement