Announcement

Collapse
No announcement yet.

[thắc mắc c/c++] hàm chèn 1 tree node vào cây nhị phân tìm kiếm

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

  • [C++] [thắc mắc c/c++] hàm chèn 1 tree node vào cây nhị phân tìm kiếm

    dưới đây là hàm chèn 1 node vào cây nhị phân tìm kiếm
    Code:
    int insertnode(tree &t,int x)
    {
    
    	if(t)
    	{
    		{
    		if(t->data==x) return 0;
    		if(t->data>x) return insertnode(t->pRight,x);
    		else return insertnode(t->pLeft,x);
    		}
    		if(t==NULL) return -1;
    		t->data=x;
    		t->pLeft=t->pRight=NULL;
    		return 1;
    	}
    }
    với
    Code:
    typedef struct tagTNode
    {
    	int data;
    	struct tagTnode* pLeft;
    	struct tagTnode* pRight;
    }Tnode;
    typedef Tnode* tree;
    khi thực hiện chạy thử thì báo lỗi là a reference of type "tree &" (not constant-qualified) cannot be initialized with a value of type "tagTnode"
    Em không hiểu rõ chỗ này, mong mọi người giúp e

  • #2
    Originally posted by 11520176 View Post
    dưới đây là hàm chèn 1 node vào cây nhị phân tìm kiếm
    Code:
    int insertnode(tree &t,int x)
    {
    
    	if(t)
    	{
    		{
    		if(t->data==x) return 0;
    		if(t->data>x) return insertnode(t->pRight,x);
    		else return insertnode(t->pLeft,x);
    		}
    		if(t==NULL) return -1;
    		t->data=x;
    		t->pLeft=t->pRight=NULL;
    		return 1;
    	}
    }
    với
    Code:
    typedef struct tagTNode
    {
    	int data;
    	struct tagTnode* pLeft;
    	struct tagTnode* pRight;
    }Tnode;
    typedef Tnode* tree;
    khi thực hiện chạy thử thì báo lỗi là a reference of type "tree &" (not constant-qualified) cannot be initialized with a value of type "tagTnode"
    Em không hiểu rõ chỗ này, mong mọi người giúp e
    Lỗi ở dòng nào hử em?

    Comment


    • #3
      Originally posted by 11520176 View Post
      dưới đây là hàm chèn 1 node vào cây nhị phân tìm kiếm
      Code:
      int insertnode(tree &t,int x)
      {
      
      	if(t)
      	{
      		{
      		if(t->data==x) return 0;
      		if(t->data>x) return insertnode(t->pRight,x);
      		else return insertnode(t->pLeft,x);
      		}
      		[COLOR="#FF0000"]if(t==NULL)[/COLOR] return -1;
      		t->data=x;
      		t->pLeft=t->pRight=NULL;
      		return 1;
      	}
      }
      với
      Code:
      typedef struct tagTNode
      {
      	int data;
      	struct tagTnode* pLeft;
      	struct tagTnode* pRight;
      }Tnode;
      typedef Tnode* tree;
      khi thực hiện chạy thử thì báo lỗi là a reference of type "tree &" (not constant-qualified) cannot be initialized with a value of type "tagTnode"
      Em không hiểu rõ chỗ này, mong mọi người giúp e
      hình như cái if(t==NULL) đang lồng vào trong if(t) (tức là t!=NULL), phải tách ra chứ?
      Khi còn nhỏ ta thấy nhiều chuyện là lớn
      Khi lớn ta thấy nhiều chuyện là nhỏ
      Đến khi thành người ta mới phân biệt được chuyện nào là lớn, chuyện nào là nhỏ

      Comment


      • #4
        theo cái thông báo lỗi thì bạn check lại xem có chỗ nào trong chương trình đáng lẽ phải truyền vào tree (tức con trỏ đến Tnode) mà lại truyền nhầm vào 1 Tnode không? :-/
        mà hình như cái phần từ if(t == NULL) return -1; trở xuống sẽ không đc thực hiện, vì đống if-else ở trên trường hợp nào cũng return, thoát khỏi hàm cả rồi :-S

        Comment


        • #5
          Originally posted by 11520176 View Post
          dưới đây là hàm chèn 1 node vào cây nhị phân tìm kiếm
          Code:
          int insertnode(tree &t,int x)
          {
          
          	if(t)
          	{
          		{
          		if(t->data==x) return 0;
          		if(t->data[COLOR="#FF0000"]>[/COLOR]x) return insertnode(t->pRight,x);
          		else return insertnode(t->pLeft,x);
          		}
          		[COLOR="#FF0000"]if(t==NULL) return -1;
          		t->data=x;
          		t->pLeft=t->pRight=NULL;
          		return 1;[/COLOR]
          	}
          }
          nguyên cái phần tô đỏ có vấn đề đó. em cần hiểu rõ được chương trình nó sẽ chạy trình tự thế nào sẽ nhìn thấy được lỗi thôi.
          có thể sửa lại như thế này:
          Code:
          int insertnode(tree &t,int x)
          {
          
          	if(t)
          	{
          		if(t->data==x) return 0;
          		if(t->data[COLOR="#00FFFF"]<[/COLOR]x) return insertnode(t->pRight,x);
          		else return insertnode(t->pLeft,x);
          	[COLOR="#00FFFF"]}
                          t->data=x;
          		t->pLeft=t->pRight=NULL;
                          if(t==NULL) return -1;
          		return 1;[/COLOR]
          }
          như vậy chắc ổn hơn.
          Last edited by 10520355; 07-06-2012, 09:52.
          University of Information Technology
          Cao Văn Nhàn _ CE10520355
          SĐT: 0188 403 4943

          Email: caovannhan2002@gmail.com

          Comment


          • #6
            Originally posted by truonganpn View Post
            Lỗi ở dòng nào hử em?
            nó báo lỗi ngay dòng return insertnode(t->pRight,x) và return insertnode(t->pLeft,x).
            Nó báo là a reference of type "tree &" (not constant-qualified) cannot be initialized with a value of type "tagTnode", e ko hiểu rõ ở đó.

            Comment

            LHQC

            Collapse
            Working...
            X