Announcement

Collapse
No announcement yet.

nhờ phát hiện lỗi ngữ nghĩa của hàm xoá số nguyên tố trong cây nhị phân tìm kiếm

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • nhờ phát hiện lỗi ngữ nghĩa của hàm xoá số nguyên tố trong cây nhị phân tìm kiếm

    đây là code của em trong khi thực hành cây nhị phân tìm kiếm, em gặp phải vấn đề code của hàm xoá số nguyên tố trong cây(removenguyento), mong anh chị sửa lỗi ngữ nghĩa dùm!
    PHP Code:
    // cây nhị phân tìm kiếm.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include "stdio.h"
    #include "conio.h"
    #include "stdlib.h"
    typedef struct tagnode
    {
        
    int data;
        
    struct tagnode *R;
        
    struct tagnode *L;
    }
    node;
    typedef nodetree;
    void creattree(tree &t)
    {
        
    NULL;
    }
    nodecreatnode(int x)
    {
        
    node *p;
        
    p= new node;
        
    p->data x;
        
    p->p->NULL;
        return 
    p;
    }
    void addnode(tree &t,node *p)
    {
        if(
    == NULL)
            
    p;
        else
        {
            if(
    t->data p->data)
                
    addnode(t->L,p);
            else
                if(
    t->data p->data)
                    
    addnode(t->R,p);
                else
                    exit(
    1);
        }
    }
    void changeminR(tree &p,tree &t)
    {
        if(
    t->!= NULL)
            
    changeminR(p,t->L);
        else
        {
            
    p->data t->data;
            
    p=t;
            
    t=t->R;
        }
    }
    void removenode(tree &t,int x)
    {
        if(
    != NULL)
        {
            if(
    t->data x)
                
    removenode(t->R,x);
            else
            {
                if(
    t->data x)
                    
    removenode(t->L,x);
                else
    //tim thay x trong cay
                
    {
                    
    node *p;
                    
    p=t;//t,p cung tro toi 1 phan tu
                    
    if(t->== NULL)//co 1 nut con
                        
    t=t->R;
                    else
                    {
                        if(
    t->== NULL)//co 1 nut con
                            
    t=t->L;
                        else
    //co 2 nut con
                            
    changeminR(p,t->R);
                    }
                    
    delete(p);
                }
            }
        }
        else
            
    printf("khong tim thay phan tu de xoa!");
    }
    nodesearchnode(tree t,int x)
    {
        if(
    != NULL)
            if(
    t->data == x)
                return 
    t;
            else
                if(
    t->data x)
                    return 
    searchnode(t->R,x);
                else
                    return 
    searchnode(t->L,x);
        return 
    NULL;
    }
    nodenodemax(tree t)
    {
        if(
    != NULL)
            if(
    t->== NULL)
                return 
    t;
            else
                return 
    nodemax(t->R);
        return 
    NULL;
    }
    nodenodemin(tree t)
    {
        if(
    != NULL)
            if(
    t->== NULL)
                return 
    t;
            else
                return 
    nodemin(t->L);
        return 
    NULL;
    }    
    void inputtree(tree &t)
    {
        
    int x;
        
    node *p;
        
    creattree(t);
        
    printf("moi ban nhap cac phan tu cho cay ket thuc bang so 0:\n");
        do
        {
            
    scanf("%3d",&x);
            if(
    == 0)
                break;
            
    creatnode(x);
            
    addnode(t,p);
        }while(
    != 0);
    }
    int demnode(tree t)
    {
        
    int dem =0;
        if(
    t!=NULL)
            
    dem =  demnode(t->L) + demnode(t->R);
        return 
    dem;
    }
    int sumnode(tree t)
    {
        
    int sum 0;
        if(
    t!=NULL)
            
    sum =  t->data sumnode(t->L) + sumnode(t->R);
        return 
    sum;
    }
    int isnodechan(int x)
    {
        if(
    x%== 0)
            return 
    1;
        else
            return 
    0;
    }
    int demnodechan(tree t)
    {
        
    int dem=0;
        if(
    t!=NULL)
            if(
    isnodechan(t->data) == 1)
                
    dem demnodechan(t->L) + demnodechan(t->R);
            else
                
    dem demnodechan(t->L) + demnodechan(t->R);
        return 
    dem;
    }
    int sumnodechan(tree t)
    {
        
    int sum=0;
        if(
    t!=NULL)
            if(
    isnodechan(t->data) == 1)
                
    sum t->data sumnodechan(t->L) + sumnodechan(t->R);
            else
                
    sum sumnodechan(t->L) + sumnodechan(t->R);
        return 
    sum;
    }
    int isnutla(tree t)//kiem tr co phai nut la hay khong
    {
        if((
    != NULL) && (t->== NULL) && (t->== NULL))
            return 
    1;
        else
            return 
    0;
    }
    int demnodela(tree t)//dem nut la
    {
        
    int dem 0;
        if(
    != NULL)
        {
            if(
    isnutla(t) == 1)
                
    dem++;
            else
                
    dem =  demnodela(t->L) + demnodela(t->R);
        }
        return 
    dem;
    }
    int sumnodela(tree t)//tong cac nut la
    {
        
    int sum=0;
        if(
    != NULL)
            if(
    isnutla(t) == 1)
                
    sum sum t->data
            else
                
    sum sumnodela(t->L) + sumnodela(t->R);
        return 
    sum;
    }
    int isnode1con(tree t)
    {
        if(
    != NULL)
            if(((
    t->== NULL) && (t->!= NULL)) || ((t->!= NULL) && (t->== NULL)))
                return 
    1;
        return 
    0;
    }
    int demnode1con(tree t)
    {
        
    int dem=0;
        if(
    != NULL)
            if(
    isnode1con(t) == 1)
                
    dem +demnode1con(t->L) + demnode1con(t->R); 
            else
                
    dem demnode1con(t->L) + demnode1con(t->R);
        return 
    dem;
    }
    int sumnode1con(tree t)
    {
        
    int s=0;
        if(
    != NULL)
            if(
    isnode1con(t) == 1)
                
    =  t->data sumnode1con(t->L) + sumnode1con(t->R);
            else
                
    =  sumnode1con(t->L) + sumnode1con(t->R);
        return 
    s;
    }
    int isnode2con(tree t)
    {
        if((
    t!=NULL) && (t->!= NULL) && (t->!= NULL))
            return 
    1;
        else
            return 
    0;
    }
    int demnode2con(tree t)
    {
        
    int dem=0;
        if(
    t!=NULL)
            if(
    isnode2con(t)==1)
                
    dem demnode2con(t->L) + demnode2con(t->R);
            else
                
    dem demnode2con(t->L) + demnode2con(t->R);
        return 
    dem;
    }
    int sumnode2con(tree t)
    {
        
    int sum 0;
        if(
    t!=NULL)
            if(
    isnode2con(t)==1)
                
    sum =   t->data sumnode2con(t->L) + sumnode2con(t->R);
            else
                
    sum =  sumnode2con(t->L) + sumnode2con(t->R);
        return 
    sum;
    }
    int max(int a,int b)
    {
        if(
    a>b)
            return 
    a;
        else 
            return 
    b;
    }
    int hight(tree t)
    {
        
    int h=0;
        if(
    t!=NULL)
        {
            
    h++;
            
    max(hight(t->L),hight(t->R));
        }
        return 
    h;
    }
    int demlever(tree t,int x)
    {
        
    int muc=0;
        if(
    t->data x)
            {
                
    muc=demlever(t->R,x);
                
            }
        else
            if(
    t->data x)
            {
                
    muc demlever(t->L,x);
                
            }
        return 
    muc;
    }
    int isnguyento(int x)
    {
        
    int i;
        if(
    x>=2)
        {
            for(
    i=2;i<x;i++)
                if(
    x%== 0)
                    break;
                if(
    x==i)
                    return 
    1;
        }
        return 
    0;
    }
    void removenguyento(tree &t)
    {
        if(
    t!=NULL)
        {
            if(
    isnguyento(t->data)==1)
                
    removenode(t,t->data);
            
    removenguyento(t->L);
            
    removenguyento(t->R);
        }
    }
    void outRNL(tree t)
    {
        if(
    != NULL)
        {
            
    outRNL(t->R);
            
    printf("%3d",t->data);
            
    outRNL(t->L);
        }
    }
    void outLNR(tree t)
    {
        if(
    != NULL)
        {
            
    outLNR(t->L);
            
    printf("%3d",t->data);
            
    outLNR(t->R);
        }
    }
    void outNRL(tree t)
    {
        if(
    != NULL)
        {
            
    printf("%3d",t->data);
            
    outNRL(t->R);
            
    outNRL(t->L);
        }
    }
    void outNLR(tree t)
    {
        if(
    != NULL)
        {
            
    printf("%3d",t->data);
            
    outNLR(t->L);
            
    outNLR(t->R);
        }
    }
    void main()
    {
        
    tree t;
        
    node *p;
        
    int x,k;
        
    inputtree(t);
        
    printf("\ncay duoc in ra kieu LNR:\n");
        
    outLNR(t);
        
    printf("\nso nut trong cay la: %d ",demnode(t));
        
    printf("\ntong cac nut  trong cay la: %d ",sumnode(t));
        
    printf("\nso nut chan trong cay la: %d ",demnodechan(t));
        
    printf("\ntong cac nut chan trong cay la: %d ",sumnodechan(t));
        
    printf("\nso nut la trong cay la: %d ",demnodela(t));
        
    printf("\ntong cac nut la trong cay la: %d ",sumnodela(t));
        
    printf("\nso nut  1 con trong cay la: %d ",demnode1con(t));
        
    printf("\ntong so nut  1 con trong cay la: %d ",sumnode1con(t));
        
    printf("\nso nut  2 con trong cay la: %d ",demnode2con(t));
        
    printf("\ntong so nut 2 con trong cay la: %d ",sumnode2con(t));
        
    printf("\nchieu cao cua cay la:  %d",hight(t));
        
    printf("\nkhoa lon nhat cua cay la:");
        if(
    nodemax(t) == NULL)
            
    printf("cay rong!");
        else
            
    printf("%d",nodemax(t)->data);
        
    printf("\nkhoa nho nhat cua cay la:");
        if(
    nodemin(t) == NULL)
            
    printf("cay rong!");
        else
            
    printf("%d",nodemin(t)->data);
        
    removenguyento(t);
        
    printf("\ncay duoc in ra kieu LNR sau khi xoa nguyen to:\n");
        
    outLNR(t);
        
    printf("\nmoi ban nhap phan tu muon xoa:\n");
        
    scanf("%d",&x);
        
    removenode(t,x);
        
    printf("cay duoc in ra kieu LNR:\n");
        
    outLNR(t);
        
    printf("\nmoi ban nhap phan tu muon xac dinh muc hay do sau:\n");
        
    scanf("%d",&k);
        
    searchnode(t,k);
        if(
    == NULL)
            
    printf("khong tim thay!");
        else
        {
            
    printf("tim thay\n");
            
    printf("muc hay do sau cua phan tu vua nhap la:  %d",demlever(t,k));
        }
        
    getch();

    còn đây là code tách ra:

    PHP Code:
    int isnguyento(int x)
    {
        
    int i;
        if(
    x>=2)
        {
            for(
    i=2;i<x;i++)
                if(
    x%== 0)
                    break;
                if(
    x==i)
                    return 
    1;
        }
        return 
    0;
    }
    void removenguyento(tree &t)
    {
        if(
    t!=NULL)
        {
            if(
    isnguyento(t->data)==1)
                
    removenode(t,t->data);
            
    removenguyento(t->L);
            
    removenguyento(t->R);
        }

    Last edited by 11520537; 10-05-2012, 16:03.
    Tôi không hối tiếc những gì mình đã làm. Tôi chỉ hối tiếc những gì đã không làm khi có cơ hội!

  • #2
    Code này bị gì em? Nó có báo lỗi gì không, lỗi ở hàng nào?

    Comment


    • #3
      Lỗi ngữ nghĩa --> là lỗi lúc chạy hả bạn. Bạn đưa cụ thể lên được không.

      Comment


      • #4
        "Lỗi ngữ nghĩa" , nghe cao xa quá @_@, semantic error à :-" , trước giờ không dùng chữ này

        Comment


        • #5
          các anh thật là.. lỗi ngữ nghĩa là lỗi do người lập trình không hiểu bản chất bài toán dẫn đến viết code sai(còn lỗi ngữ pháp thì chương trình dịch nó giải quyết)
          trong code này thì các anh xem hàm removenguyento(tree t) của em sai ở điểm nào thôi(khi chạy chương trình thì không có lỗi, nhưng thực hiện hàm này thì sai kết quả)
          Tôi không hối tiếc những gì mình đã làm. Tôi chỉ hối tiếc những gì đã không làm khi có cơ hội!

          Comment


          • #6
            Originally posted by 11520537 View Post
            các anh thật là.. lỗi ngữ nghĩa là lỗi do người lập trình không hiểu bản chất bài toán dẫn đến viết code sai(còn lỗi ngữ pháp thì chương trình dịch nó giải quyết)
            trong code này thì các anh xem hàm removenguyento(tree t) của em sai ở điểm nào thôi(khi chạy chương trình thì không có lỗi, nhưng thực hiện hàm này thì sai kết quả)
            Vậy em đã chạy chương trình với dữ liệu đầu vào như thế nào? Kết quả thu được từ chương trình là gì và kết quả mong muốn đáng ra phải thu được là gì? Phải có cái này mới giúp mọi người thu hẹp phạm vi cần phải xác định lỗi được.

            Mà hàm xóa một node trong cây em đã kiểm tra thử xem chắc chắn nó chạy đúng chưa để mọi người khỏi phải kiểm tra lại? Nhiều khả năng cái sai nằm trong hàm xóa node.

            Comment


            • #7
              theo mình thì bạn sai ở chỗ là nếu bạn tìm phần tử thế mạng thì nó sẽ bỏ qua phần tử thế mạng đó và nếu phần tử thế mạng đó là số nguyên tố thì nó cũng không xóa bạn ak. bạn thử dùng step over, step in là bạn sẽ nhìn thấy
              :love:
              Tương lai khóc hay cười phụ thuộc vào độ lười của quá khứ.
              :cry:

              Comment


              • #8
                Lỗi của bạn nè:

                void removenguyento(tree &t)
                {
                if(t!=NULL)
                {
                if(isnguyento(t->data)==1)
                removenode(t,t->data);
                removenguyento(t->L);
                removenguyento(t->R);
                }
                }


                chỉ đơn giản sửa ngược lại tí thôi ah:
                void removenguyento(tree &t)
                {
                if(t!=NULL)
                {
                removenguyento(t->L);
                removenguyento(t->R);
                if(isnguyento(t->data)==1)
                removenode(t,t->data);

                }
                }



                Mình test thử thấy OK rồi

                Comment


                • #9
                  Originally posted by 11520045 View Post
                  Lỗi của bạn nè:

                  void removenguyento(tree &t)
                  {
                  if(t!=NULL)
                  {
                  if(isnguyento(t->data)==1)
                  removenode(t,t->data);
                  removenguyento(t->L);
                  removenguyento(t->R);
                  }
                  }


                  chỉ đơn giản sửa ngược lại tí thôi ah:
                  void removenguyento(tree &t)
                  {
                  if(t!=NULL)
                  {
                  removenguyento(t->L);
                  removenguyento(t->R);
                  if(isnguyento(t->data)==1)
                  removenode(t,t->data);

                  }
                  }
                  p
                  uhm đổi lại như vậy thì đúng rùi theo cái này là tìm từ dưới lên trên rồi đó
                  :love:
                  Tương lai khóc hay cười phụ thuộc vào độ lười của quá khứ.
                  :cry:

                  Comment


                  • #10
                    Originally posted by 11520139 View Post
                    ... theo cái này là tìm từ dưới lên trên rồi đó
                    Bạn chắc chứ ???

                    Comment


                    • #11
                      Originally posted by 11520045 View Post
                      Bạn chắc chứ ???
                      ờ ý mình là nói nó xét từ lá lên rùi chứ không phải xét gốc trước như code lúc đầu nữa
                      :love:
                      Tương lai khóc hay cười phụ thuộc vào độ lười của quá khứ.
                      :cry:

                      Comment


                      • #12
                        đây là một đoạn code của mình.... khi mình delete p thì nó lại báo lỗi là chưa khai báo p..
                        trong khi đấy thì mình đã khai báo rồi..
                        đây là lỗi:
                        error C2541: 'delete' : cannot delete objects that are not pointers
                        error C2065: 'p' : undeclared identifier
                        mong mọi người giúp!!!!!!!
                        Code:
                        void delNode(TREE &T, int x)
                        {
                        	if(T==NULL)
                        		return;
                        	if(x>T->info)
                        		delNode(T->Right,x);
                        	else if(x<T->info)
                        		delNode(T->Left,x);
                        	else
                        	{
                        		Node *p=T;
                        		if(T->Left==NULL) 
                        			T=T->Right;
                        		else if(T->Right==NULL) 
                        			T=T->Left;
                        		else
                        			SearchStandfor(p,T->Right);
                        	}
                        	delete p;
                        }

                        Comment


                        • #13
                          đúng rồi bạn ak con trỏ P của bạn chỉ tồn tại trong chương khoảng thời gian thi hành lệnh else cái đoạn có khai báo node *p đó. còn thực sự nếu ở ngoài vòng đó thì sẽ không có con trỏ p bạn nhé. bạn nên chuyển vị trí khai báo lên đầu hàm như thế này chắc là sẽ được
                          PHP Code:
                          void delNode(TREE &Tint x)
                          {
                                  
                          node *p;
                              if(
                          T==NULL)
                                  return;
                              if(
                          x>T->info)
                                  
                          delNode(T->Right,x);
                              else if(
                          x<T->info)
                                  
                          delNode(T->Left,x);
                              else
                              {
                                  
                          p=T;
                                  if(
                          T->Left==NULL
                                      
                          T=T->Right;
                                  else if(
                          T->Right==NULL
                                      
                          T=T->Left;
                                  else
                                      
                          SearchStandfor(p,T->Right);
                              }
                              
                          delete p;

                          Last edited by 11520139; 13-06-2012, 12:34.
                          :love:
                          Tương lai khóc hay cười phụ thuộc vào độ lười của quá khứ.
                          :cry:

                          Comment


                          • #14
                            Bạn Nguyễn Văn Hướng nói đúng rồi đó, hồi trước code c mình toàn bị lỗi này
                            Huỳnh Văn Hoa Xuân
                            Mail: luckyboyx1993@gmail.com
                            Lao động hăng say - Tình yêu sẽ đến:love:

                            Comment


                            • #15
                              thank nhiều
                              chạy được oy`
                              Originally posted by 11520139 View Post
                              đúng rồi bạn ak con trỏ P của bạn chỉ tồn tại trong chương khoảng thời gian thi hành lệnh else cái đoạn có khai báo node *p đó. còn thực sự nếu ở ngoài vòng đó thì sẽ không có con trỏ p bạn nhé. bạn nên chuyển vị trí khai báo lên đầu hàm như thế này chắc là sẽ được
                              PHP Code:
                              void delNode(TREE &Tint x)
                              {
                                      
                              node *p;
                                  if(
                              T==NULL)
                                      return;
                                  if(
                              x>T->info)
                                      
                              delNode(T->Right,x);
                                  else if(
                              x<T->info)
                                      
                              delNode(T->Left,x);
                                  else
                                  {
                                      
                              p=T;
                                      if(
                              T->Left==NULL
                                          
                              T=T->Right;
                                      else if(
                              T->Right==NULL
                                          
                              T=T->Left;
                                      else
                                          
                              SearchStandfor(p,T->Right);
                                  }
                                  
                              delete p;

                              Comment

                              LHQC

                              Collapse
                              Working...
                              X