Giúp mình chút

    Share

    life_single2610
    New Member
    New Member

    Giới tính: Nam Bài gửi: 8
    Tổng Điểm: 21
    Điểm Thưởng: 0
    Sinh Nhật: 28/02/1989 Bị Dụ Dỗ: 04/12/2009
    Tuổi: 22

    default Giúp mình chút

    Bài gửi by life_single2610 on 14/12/2009, 21:43

    Code:
    #include<stdio.h>
    #include<conio.h>

    typedef  struct tagTNode
    {
       int Key;
       struct tagTNode* pLeft;
       struct tagTNode* pRight;
    }TNODE;
    typedef  TNODE* TREE;
    void initTree(TREE &T)
    {
       T=NULL;
    }
    TNODE* GetNode(int x)
    {
       TNODE* p=new TNODE;
       if(p==NULL)   
          return p;
       p->Key=x;
       p->pLeft=p->pRight=NULL;
       return p;
    }
    int ThemNut(TREE &T, int x)
    {
       if(T!=NULL)
       {   if(x==T->Key)    
             return 0;
          else
          {
             if(x<T->Key)   
                ThemNut(T->pLeft, x);
             else       
                ThemNut(T->pRight, x);
          }
       }
       else
       {
          T=new TNODE;
          if(T==NULL)    
             return -1;
          T->Key=x;
          T->pLeft=T->pRight=NULL;
          return 1;
       }
    }



    int SearchNode(TREE &T,int X)
    {
       if(T!=NULL)
       {
          if(T->Key==X)   
             return 1;
          if(T->Key>X)      
             return SearchNode(T->pLeft,X);
          else         
             return SearchNode(T->pRight,X);
       }
       return 0;
    }



    /*
    TNODE* SearchNode(TREE T,int x)
    {
       if(T!=NULL)
       {
          if(T->Key==x)  return T;
          if(x<T->Key)
             return SearchNode(T->pLeft,x);
          else
             return SearchNode(T->pRight,x);
       }
       //return T;
    }
    */
    void SearchStandFor(TREE &p, TREE &q)
    {
       if(q->pLeft)
          SearchStandFor(p,q->pLeft);
       else
       {
          p->Key=q->Key;
          p=q;
          q=q->pRight;
       }
    }
    int Max(int &a,int &b)
    {
       if(a>b)
          return a;
       else
          return b;
    }
    int DoCaoCay(TREE T)
    {
       //int Max;
       if(T)
       {
          int T1=DoCaoCay(T->pLeft);
          int T2=DoCaoCay(T->pRight);
          return Max(T1,T2)+1;
       }
       else
          return 0;
    }

    int  DelNode(TREE &T, int x)
    {
       if(T==NULL)      
          return 0;
       if(T->Key>x)      
          return DelNode(T->pLeft,x);
       if(T->Key<x)   
          return DelNode(T->pRight,x);
       else
       {
          TNODE*p=T;
          if(T->pLeft==NULL)
             T=T->pRight;
          else if(T->pRight==NULL)
             T=T->pLeft;
          else
          {
          
             TNODE* q=T->pRight;
             SearchStandFor(p,q);
          }
       delete p;
       }
    return 1;
    }
    void  RemoveTree(TREE &T)
    {
       if(T)
       {
          RemoveTree(T->pLeft);
          RemoveTree(T->pRight);
          delete T;
       }
    }
    int DemNutLa(TREE T)
    {
       if(T!=NULL)
       {
       
       if(T->pLeft==NULL && T->pRight==NULL)
              return 1;
          else
              return DemNutLa(T->pLeft)+DemNutLa(T->pRight);
       }

       else
          return 0;
    }
    int DEMNODE(TREE &T)
    {
        if(T==NULL)
           return 0;
        return (1+DEMNODE(T->pLeft)+DEMNODE(T->pRight));
    }
    void sapxep(TREE &T)
    {
       
    }
    void XuatCay(TREE &T)
    {
       if(T!=NULL)
       {
        printf("%4d",T->Key);
        XuatCay(T->pLeft);
        XuatCay(T->pRight);
       }
    }

    int DEMNODECHAN(TREE &T)
    {
       int idem=0;
        if(T==NULL)
           return 0;
       if(T->Key%2==0)
          return (1+DEMNODECHAN(T->pLeft)+DEMNODECHAN(T->pRight));
       
       
       
    }

    int TongChan(TREE &T)
    {
       int S=0;
       
       if(T==NULL)
          return S;
       if(T!=NULL)
       {
           if(T->Key>0&&T->Key%2==0)
         S=S+T->Key;
        
        S+=TongChan(T->pLeft);
        S+=TongChan(T->pRight);
       }
       return S;
       
    }
    int TongLe(TREE &T)
    {
       int S=0;
       if(T==NULL)
          return S;
       if(T!=NULL)
       {
           if(T->Key>0&&T->Key%2!=0)
         S=S+T->Key;
        S+=TongLe(T->pLeft);
        S+=TongLe(T->pRight);
       }
       return S;
    }
    int TONGNODE(TREE &T)
    {
        if(T==NULL)
        return 0;
           return(T->Key+TONGNODE(T->pLeft)+TONGNODE(T->pRight));
    }


    void CreateTree(TREE &T)
    {
       int x;
       do
       {
       nhap:
             printf("\nNhap vao key:");
             printf("x=");
             scanf("%d",&x);
             if(x<0)
             {
                printf("\n Ban Phai Nhap So >0!! Nhap lai:");
                goto nhap;
             }
             if(x==0)   
                 break;
       int kq=ThemNut(T,x);
       if(kq==-1) break;
       else if(kq==0)   
          printf("\nDa co trong danh sach");
       }while(1);
    }
    void NLR(TREE &T)
    {
       if(T!=NULL)
       {
          printf("%4d",T->Key);
          NLR(T->pLeft);
          NLR(T->pRight);
       }
    }
    void LNR(TREE &T)
    {
       if(T!=NULL)
       {
          LNR(T->pLeft);
          printf("%4d",T->Key);
          LNR(T->pRight);
       }
    }
    void LRN(TREE &T)
    {
       if(T!=NULL)
       {
          LRN(T->pLeft);
          LRN(T->pRight);
          printf("%4d",T->Key);
       }
    }
    void xuat(TREE &T)
    {
       if(T==NULL)
       return;
       xuat(T->pLeft);     
       printf("%4d",T->Key);       
       xuat(T->pRight);
    }
    void XuatChan(TREE &T)
    {   
       if(T==NULL)               
       return;       
       XuatChan(T->pLeft);       
       if(T->Key%2==0)   
       {         
           printf("%4d",T->Key);       
           
       }
        XuatChan(T->pRight);
       
    }
    void Xuatle(TREE &T)
    {   
       if(T==NULL)               
       return;       
       Xuatle(T->pLeft);       
       if(T->Key%2!=0)   
       {         
           printf("%4d",T->Key);       
           
       }
        Xuatle(T->pRight);
       
    }
    void main()
    {
       TREE  T;
       int x;
       initTree(T);
       int chon;
       do
       {
          printf("\n______________-Cay Nhi Phan-______________");
          printf("\n    1.Nhap cay nhi phan                  ");
          printf("\n    2.Duyet theo cac phan tu co trong cay");
          printf("\n    3.Kiem tra so x                      ");
          printf("\n    4.Dem so node co trong cay          ");
          printf("\n    5.Dem so nut la co trong cay        ");
          printf("\n    6.Tong Node                          ");
          printf("\n    7.Do cao cay                        ");
          printf("\n    8.Them Node vao Key                  ");
          printf("\n    9.Xuat phan tu chan                ");
          printf("\n    10.XuAT Phan tu le                  ");
          printf("\n    11.Tong Chan                        ");
          printf("\n    12.Tong Le                          ");
          printf("\n    13.Sap xep tang dan                  ");
          printf("\n__________________________________________");
          printf("\nMoi ban chon:");
          scanf("%d",&chon);
          switch(chon)
          {
             case 1:
                printf("Khoi tao cay:");
                printf("\nNhap phim 0 de dung nhap:");
                CreateTree(T);
                printf("\ncay da da dc tao:");
                break;
             case 2:
                printf("\n__________Duyet Danh sach_______________\n");         
                char chon1;               
                   printf("\n_____Cac PT duyet_____");
                   printf("\n__a.Node-left-right___");
                   printf("\n__b.Left-Node-right___");
                   printf("\n__c.Left-Right-Node___");
                   printf("\n______________________");
                   printf("\nMoi ban chon:");
                   scanf("%s",&chon1);
                   switch(chon1)
                   {
                      case 'a':
                         printf("\nDa duyet theo NLR:\n");
                         NLR(T);
                         break;
                      case 'b':
                         printf("\nDa duyet theo LNR:\n");
                         LNR(T);
                         break;
                      case 'c':
                         printf("\nDa duyet theo LRN:\n");
                         LRN(T);
                      break;
                   }
                
                break;
             case 3:
                printf("\nTim node");
                printf("\nNhap vao so muon tim:");
                scanf("%d",&x);
                if(SearchNode(T,x)==0)
                   printf("\n Khong co trong cay");
                else
                   printf("\n so %d co trong cay",x);
                break;
             case 4:
                printf("\nDem so Node:");
                printf("\nCay co:%5d node ",DEMNODE(T));
                break;
             case 5:
                printf("\nDem so nut la co trong cay:___");
                printf("\nSo nut la : %d",DemNutLa(T));
                break;
             case 6:
                printf("\nTong tat ca cac node trong cay la: %d",TONGNODE(T));
                break;
             case 7:
                printf("\nDO caO CUA CAY:");
                printf("\nDo cao cua cay la:%d",DoCaoCay(T));
                break;
             case 8:
                printf("\nNhap vao so them vao cay:");
                scanf("%d",&x);
                ThemNut(T,x);   
                NLR(T);
                break;         
             case 9:
                XuatChan(T);
                break;
             case 10:
                Xuatle(T);
                break;
             case 11:
                printf("\nTong Chan:%d",TongChan(T));
                break;
             case 12:
                printf("\nTong Le:%d",TongLe(T));
                break;
             case 13:
                   xuat(T);
                   break;
             case 0:
                break;
                
          }
       }while(chon!='0');
       getch();
       return 0;
    }





    life_single2610
    New Member
    New Member

    Giới tính: Nam Bài gửi: 8
    Tổng Điểm: 21
    Điểm Thưởng: 0
    Sinh Nhật: 28/02/1989 Bị Dụ Dỗ: 04/12/2009
    Tuổi: 22

    default Re: Giúp mình chút

    Bài gửi by life_single2610 on 14/12/2009, 21:48

    Đâ là đề thầy cho , các bạn giúp mình làm 2 case cuối cùng nhé , mình làm hoài ma ko ra:
    1.xuất cây NPTK các số nguyên, tính tổng các giá trị nguyên tố.
    2.xuất cây NPTK các số nguyên, tính tổng các giá trị chính phương.
    Làm như bên c cơ bản thì dễ , nhưng với Nhị Phân thì mình bó tay , ai giúp minh nhanh nhé.cảm ơn nhìu

      Hôm nay: 23/2/2012, 17:33