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!
NGUỒN SỈ ÁO KHOÁC DENIM NỮ TPHCM CHẤT LƯỢNG CAO Bạn muốn nhập sỉ áo khoác denim nữ để bắt đầu việc buôn bán Online phong cách nhưng lại chưa có kinh nghiệm, muốn sắm xưởng sỉ xống áo vừa có thể giải...
Xưởng sỉ áo khoác jeans denim giá...