Chào mọi người, mình có vấn đề nho nhỏ mong mọi người giúp đỡ.
Code:
Mã:
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
typedef struct tagTNode
{
int Key;
struct tagTNode *pLeft, *pRight;
} TNode;
typedef TNode* Tree;
void CreateTree (Tree &T)
{
T = NULL;
}
int InsertNode (Tree &T, int x)
{
if (T)
{
if (T->Key == x) return 0;
if (T->Key > x)
return InsertNode (T->pLeft,x);
else
return InsertNode (T->pRight,x);
}
T = new TNode;
if (T == NULL) return -1;
T->Key = x;
T->pLeft = T->pRight = NULL;
return 1;
}
void LNR (Tree T)
{
if (T)
{
LNR (T->pLeft);
cout << T->Key << " ";
LNR (T->pRight);
}
}
void SearchStandFor (Tree &p, Tree &q)
{
if (q->pLeft)
SearchStandFor (p,q->pLeft);
else
{
p->Key = q->Key;
p = q;
q = q->pRight;
}
}
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;
}
}
int main ()
{
Tree T;
CreateTree (T);
int x;
do
{
cout << "Nhap nut: ";
cin >> x;
if (x != 0) InsertNode (T,x);
} while (x != 0 && x > 0);
delNode (T,4);
LNR (T);
getch ();
return 0;
}
Mình tiến hành nhập vào: 2 4 3 5 0. Kết quả in ra thì đúng, nhưng vẫn báo lỗi. Debug thấy lỗi ở ngay phần in, nếu không xóa Node thì in cây bình thường vẫn được. Mình không biết lỗi đây là gì, mong mọi người giúp đỡ. Và nếu muốn in ra kết quả mà không báo lỗi thì mình phải in kiểu gì. Cảm ơn mọi người rất nhiều.
View more random threads:
[/b] Tại Lương Sơn TV bạn sở hữu thể xem truyền hình trực tiếp bóng đá hôm nay các giải đấu to trong và ngoài nước. Lương Sơn TV ko chỉ đem lại những trận chiến mãn nhãn sở hữu chất lượng hình ảnh...
Các giải đấu không thể bỏ lỡ tại...