Advertisement
nguyentruong98

Untitled

Nov 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. typedef struct Node
  5. {
  6.       char Name[80], Phone[80], Email[80];
  7.       struct Node* leftChild;
  8.       struct Node* rightChild;
  9.       struct Node* parent;
  10. }Node;
  11. Node* root;
  12. Node* makeNode(char *s1, char* s2, char* s3)
  13. {
  14.       Node*p = (Node*)malloc(sizeof(Node));
  15.       strcpy(p->Name,s1);
  16.       strcpy(p->Phone,s2);
  17.       strcpy(p->Email,s3);
  18.       p->parent = NULL;
  19.       p->leftChild = NULL;
  20.       p->rightChild = NULL;
  21.       return p;
  22. }
  23. void addNode(Node*p)
  24. {
  25.       if(root == NULL)
  26.       {
  27.             root = p;
  28.             return;
  29.       }
  30.       Node*q = root, *prev_q = NULL;
  31.       int cmp;
  32.       while(1)
  33.       {
  34.             if(q == NULL)
  35.             {
  36.                   q = p;
  37.                   if(prev_q != NULL)
  38.                   {
  39.                   if(cmp<0)
  40.                   prev_q->rightChild = q;
  41.                   if(cmp>0)
  42.                   prev_q->leftChild = q;
  43.                   q->parent = prev_q;
  44.                   return;
  45.             }
  46.             cmp = strcmp(q->Name,p->Name);
  47.                   if(cmp == 0)
  48.                   return;
  49.                   if(cmp<0)
  50.                   {
  51.                   prev_q = q;
  52.                   q = q->rightChild;
  53.                   return;
  54.                   }
  55.                   if(cmp>0)
  56.                   {
  57.                   prev_q = q;
  58.                   q = q->leftChild;
  59.                   return;
  60.                   }
  61.             }
  62.       }
  63. }
  64. Node* findNode(char* Name, Node*p)
  65. {
  66.       if(p == NULL)
  67.       return NULL;
  68.       int cmp;
  69.       cmp = strcmp(p->Name,Name);
  70.       Node* q;
  71.             if(cmp == 0)
  72.             return p;
  73.             if(cmp > 0)
  74.             {
  75.             q = findNode(Name, p->leftChild);
  76.             return q;
  77.             }
  78.             if(cmp < 0)
  79.             {
  80.             q = findNode(Name, p->rightChild);
  81.             return q;
  82.             }
  83.       }
  84. void printbst(Node* p)
  85. {
  86.       if(p == NULL)
  87.       return;
  88.       printf("%s %s %s", p->Name, p->Phone, p->Email);
  89.       printbst(p->leftChild);
  90.       printbst(p->rightChild);
  91.       return;
  92. }
  93. int main()
  94. {
  95.       FILE *f;
  96.       f = fopen("phonebook.txt", "r");
  97.       if (f == NULL)
  98.       {
  99.             printf("ERROR\n");
  100.       }
  101.       else
  102.       {
  103.             char s1[80], s2[80], s3[80];
  104.             do{  
  105.             fscanf(f, "%s %s %s", s1, s2, s3);
  106.             Node* newNode = makeNode(s1,s2,s3);
  107.             addNode(newNode);
  108.             }while(!feof(f));
  109.       }
  110.       printbst(root);
  111.       return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement