Advertisement
nguyenvanquan7826

cây nhị phân sinh viên

Dec 4th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. struct item{
  6.     char id[20];
  7.     char name[20];
  8.     double scores;
  9. };
  10. struct Node
  11. {
  12.      item key; //truong key cua du lieu
  13.      Node *Left, *Right; //con trai va con phai
  14. };
  15. typedef Node *Tree;  //cay
  16.  
  17. int compare(item x, item y){ // so sanh 2 item theo key
  18.     return strcmp(x.id, y.id);
  19. }
  20.  
  21. item inputItem(){   // nhap 1 item
  22.     item x;
  23.     char id[20];
  24.     printf("Enter id of student (q to quit): ");
  25.     gets(x.id);
  26.    
  27.     if(strcmp(x.id, "q") == 0 || strcmp(x.id, "Q") == 0){
  28.         return x;
  29.     }
  30.    
  31.     printf("Enter name of student: ");
  32.     gets(x.name);
  33.    
  34.     printf("Enter scores of student:");
  35.     scanf("%lf", &x.scores);
  36.    
  37.     //fflush(stdin);
  38.     while (getchar() != '\n');
  39.    
  40.     return x;
  41. }
  42.  
  43. void outItem(item x){
  44.     printf("%-20s %-20s %-3.2f \n", x.id, x.name, x.scores);
  45. }
  46.  
  47. int insertNode(Tree &T, item x) // chen 1 Node vao cay
  48. {
  49.     if (T != NULL)
  50.     {
  51.         if (compare(T->key, x) == 0) return -1;  // Node nay da co
  52.         if (compare(T->key, x) > 0) return insertNode(T->Left, x); // chen vao Node trai
  53.         else if (compare(T->key, x) < 0) return insertNode(T->Right, x);   // chen vao Node phai
  54.     }
  55.     T = (Node *) malloc(sizeof(Node));
  56.     if (T == NULL) return 0;    // khong du bo nho
  57.     T->key = x;
  58.     T->Left = T->Right = NULL;
  59.     return 1;   // ok
  60. }
  61.  
  62. void CreateTree(Tree &T)        // nhap cay
  63. {
  64.     item x;
  65.     while (1)
  66.     {
  67.         printf("Enter a student: ");
  68.         x = inputItem();
  69.         if (strcmp(x.id, "q") == 0 || strcmp(x.id, "Q") == 0) break;  // x = 0 thi thoat
  70.         int check = insertNode(T, x);
  71.         if (check == -1) printf("Student is exits!");
  72.         else if (check == 0) printf("Memory full");
  73.  
  74.     }
  75. }
  76.  
  77. // Duyet theo LNR
  78. void LNR(Tree T)
  79. {
  80.      if(T!=NULL)
  81.      {
  82.           LNR(T->Left);
  83.           outItem(T->key);
  84.           LNR(T->Right);
  85.      }
  86. }
  87.  
  88. Node* searchKey(Tree T, int scores)     // tim nut co diem < 4
  89. {
  90.     if (T!=NULL)
  91.     {
  92.         if (T->key.scores < scores) { Node *P = T; return P;}
  93.         if (T->key.scores >= scores) {
  94.             Node *node = searchKey(T->Left, scores);
  95.             if(node != NULL) return node;
  96.             else {
  97.                 return searchKey(T->Right, scores);
  98.             }
  99.         }
  100.     }
  101.     return NULL;
  102. }
  103.  
  104. int delKey(Tree &T, item x)     // xoa nut co key x
  105. {
  106.     if (T==NULL) return 0;
  107.     else if (compare(T->key, x) > 0) return delKey(T->Left, x);
  108.     else if (compare(T->key, x) < 0) return delKey(T->Right, x);
  109.     else // T->key == x
  110.     {
  111.         Node *P = T;
  112.         if (T->Left == NULL) T = T->Right;    // Node chi co cay con phai
  113.         else if (T->Right == NULL) T = T->Left;   // Node chi co cay con trai
  114.         else // Node co ca 2 con
  115.         {
  116.             Node *S = T, *Q = S->Left;
  117.             // S la cha cua Q, Q la Node phai nhat cua cay con trai cua P
  118.             while (Q->Right != NULL)
  119.             {
  120.                 S = Q;
  121.                 Q = Q->Right;
  122.             }
  123.             P->key = Q->key;
  124.             S->Right = Q->Left;
  125.             delete Q;
  126.         }
  127.     }
  128.     return 1;
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.     Tree T;
  135.     T=NULL; //Tao cay rong
  136.  
  137.     CreateTree(T); //Nhap cay
  138.     //duyet cay
  139.     printf("LNR: \n");
  140.     LNR(T);
  141.     printf("\n");
  142.  
  143.     Node *P;
  144.     item x;
  145.     printf("Enter id of student to add: ");
  146.     x = inputItem();
  147.     if (insertNode(T, x) == -1){
  148.         printf("add failt!");
  149.     }else{
  150.         printf("add success:\n");
  151.         printf("LNR: \n");
  152.         LNR(T);
  153.         printf("\n");
  154.     }
  155.  
  156.    
  157.     int del = 1;
  158.     while(del){
  159.         Node* node = searchKey(T, 4);
  160.         if(node != NULL){
  161.             printf("del");
  162.             del = delKey(T, node->key);
  163.         }else{
  164.             printf("null");
  165.             del = 0;
  166.         }
  167.     }
  168.     printf("LNR: \n");
  169.     LNR(T);
  170.     printf("\n");
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement