Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    của bác đây.

    Mã nguồn PHP:
    int insertNode (Tree &T, int x){ if (T) { if (T->Key == x) return 0; // Da ton tai khoa trong cay if (T->Key > x) return insertNode (T->pLeft,x); else return insertNode (T->pRight,x); } else { T = new TNode; if (T == NULL) // Het bo nho { cout << "
    Het bo nho"; return -1; } T -> Key = x; T -> pLeft = NULL; T -> pRight = NULL; return 1; }}int insertTNode (Tree &T, TNode *a) // Mình debug th?y hàm này có v?n d?, mà không nh?n ra v?n d? ? dâu m?c dù tuong t? hàm trên{ if (T) { if (T->Key == a->Key) return 0; if (T->Key > a->Key) insertTNode (T->pLeft,a); else insertTNode (T->pRight,a); } else { T = new TNode; if (T == NULL) { cout << "
    Thieu bo nho"; return -1; } T->Key = a->Key; T->pLeft = NULL; T->pRight = NULL; return 1; }}  

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Thank cậu nhiều nhé.

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Thắc mắc về thuật toán chèn vào cây nhị phân

    Chào mọi người, mình có bài tập thắc mắc mong mọi người giải thích giùm mình

    Mã:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    typedef struct tagTNode
    {
    	int Key;
    	struct tagTNode* pLeft;
    	struct tagTNode* pRight;
    }TNode;
    typedef TNode* Tree;
    
    
    TNode *CreateTNode (int x)
    {
    	TNode *p;
    	p = new TNode;
    	if (p == NULL)
    	{
    		cout << "
    Het bo nho";
    		getch ();
    		exit (1);
    	}
    	p -> pLeft = NULL;
    	p -> pRight = NULL;
    	p -> Key = x;
    	return p;
    }
    
    int insertNode (Tree &T, int x)
    {
    	if (T)
    	{
    		if (T->Key == x) return 0;       // Da ton tai khoa trong cay
    		if (T->Key > x) return insertNode (T->pLeft,x);
    		else
    			return insertNode (T->pRight,x);
    	}
    	T = new TNode;
    	if (T == NULL)                  // Het bo nho
    	{
    		cout << "
    Het bo nho";
    		return -1;
    	}       
    	T -> Key = x;
    	T -> pLeft = NULL;
    	T -> pRight = NULL;
    	return 1;
    }
    
    int insertTNode (Tree &T, TNode *a)           // Mình debug thấy hàm này có vấn đề, mà không nhận ra vấn đề ở đâu mặc dù tuơng tự hàm trên
    {
    	if (T)
    	{
    		if (T->Key == a->Key) return 0;
    		if (T->Key > a->Key) insertTNode (T->pLeft,a);
    		else 
    			insertTNode (T->pRight,a);
    	}
    	T = new TNode;
    	if (T == NULL)
    	{
    		cout << "
    Thieu bo nho";
    		return -1;
    	}
    	T->Key = a->Key;
    	T->pLeft = NULL;
    	T->pRight = NULL;
    	return 1;
    }
    int main ()
    {
    	Tree T = NULL;
    	T = CreateTNode (0);
    	TNode *a = CreateTNode (3);
    	insertNode (T,1);
    	insertNode (T,2);
    	insertTNode (T,a);
    
    	TNode *p = T;
    	while (p != NULL)
    	{
    		cout << p->Key << "  ";
    		p = p->pRight;
    	}
    
    	getch ();
    	return 0;
    }
    Kết quả mình muốn in ra là 0 1 2 3, nhưng chạy thì chỉ ra mỗi 3. Mong mọi người giúp đỡ. Thank all!

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •