Savage
02-01-2003, 20:38
Minh lam mot bai tap nho Delete mot Node trong mot Binary tree , nhung khong hieu sao sau khi xoa thi dan toi hien tuong
la Tree bi ngat dut ! mac du algorithm la OK roi. Vay cac Bac xem giup minh co dieu gi sai
trong doan chuong trinh sau nha ( chu yeu la phan Delete node )
// file cNode.h
#ifndef __cNode
class cNode
{
public:
cNode( int a ): Val(a), pLeft(NULL), pRight(NULL) {}
public:
int Val;
cNode *pLeft;
cNode *pRight;
};
#define __cNode
#endif
/************************************************** *******/
// file cTree.h
#include "cNode.h"
#ifndef __cTree
class cTree
{
public:
cTree( cNode *root ):pTree(root){ }
~cTree( ) {};
cTree& operator+( cNode* ); // add a new node
cTree& operator-( cNode* ); // delete a node
void PrintTree( cNode* );
void Del( cNode*, cNode* );
void Delete( cNode*,int );
public:
cNode *pTree; // points to the top of tree
};
/************************************************** *******/
cTree& cTree::operator+( cNode *NewNode )
{
cNode *temp;
cNode *pass;
temp = pTree;
pass = temp;
while(temp)
{
pass = temp;
if( NewNode->Val <= temp->Val )
temp = temp->pLeft;
else
temp = temp->pRight;
}
if( NewNode->Val <= pass->Val)
pass->pLeft = NewNode;
else
pass->pRight= NewNode;
return *this;
}
/************************************************** *******/
// function này dùng để xoá một Node theo kiểu thay thế node đó bằng node bên
phải nhất của left SubTree
void cTree::Del( cNode *root, cNode *TempNode )
{
cNode *temp;
if(!root->pRight)
{
TempNode->Val = root->Val;
temp = root;
root = root->pLeft;
delete temp;
}
else Del( root->pRight, TempNode );
}
/************************************************** *******/
// delete node in the general case
void cTree::Delete( cNode *root, int a )
{
if( a < root->Val )
Delete( root->pLeft, a );
else
if( a > root->Val )
Delete( root->pRight, a );
else
{
if(!root->pLeft) // there is no left subtree
{
cNode *temp = root;
root = root->pRight;
delete temp;
}
else
if(!root->pRight) // there if no right subtree
{
cNode *temp = root;
root = root->pLeft;
delete temp;
}
else // there are two subtrees
Del( root->pLeft, root );
}
return ;
}
/************************************************** *******/
cTree& cTree::operator -( cNode *DelNode )
{
Delete( pTree, DelNode->Val );
return *this;
}
/************************************************** *******/
void cTree::PrintTree( cNode *temp )
{
if( temp )
{
PrintTree(temp->pLeft);
printf("%d ", temp->Val);
PrintTree(temp->pRight);
}
}
/************************************************** *******/
#define __cTree
#endif
la Tree bi ngat dut ! mac du algorithm la OK roi. Vay cac Bac xem giup minh co dieu gi sai
trong doan chuong trinh sau nha ( chu yeu la phan Delete node )
// file cNode.h
#ifndef __cNode
class cNode
{
public:
cNode( int a ): Val(a), pLeft(NULL), pRight(NULL) {}
public:
int Val;
cNode *pLeft;
cNode *pRight;
};
#define __cNode
#endif
/************************************************** *******/
// file cTree.h
#include "cNode.h"
#ifndef __cTree
class cTree
{
public:
cTree( cNode *root ):pTree(root){ }
~cTree( ) {};
cTree& operator+( cNode* ); // add a new node
cTree& operator-( cNode* ); // delete a node
void PrintTree( cNode* );
void Del( cNode*, cNode* );
void Delete( cNode*,int );
public:
cNode *pTree; // points to the top of tree
};
/************************************************** *******/
cTree& cTree::operator+( cNode *NewNode )
{
cNode *temp;
cNode *pass;
temp = pTree;
pass = temp;
while(temp)
{
pass = temp;
if( NewNode->Val <= temp->Val )
temp = temp->pLeft;
else
temp = temp->pRight;
}
if( NewNode->Val <= pass->Val)
pass->pLeft = NewNode;
else
pass->pRight= NewNode;
return *this;
}
/************************************************** *******/
// function này dùng để xoá một Node theo kiểu thay thế node đó bằng node bên
phải nhất của left SubTree
void cTree::Del( cNode *root, cNode *TempNode )
{
cNode *temp;
if(!root->pRight)
{
TempNode->Val = root->Val;
temp = root;
root = root->pLeft;
delete temp;
}
else Del( root->pRight, TempNode );
}
/************************************************** *******/
// delete node in the general case
void cTree::Delete( cNode *root, int a )
{
if( a < root->Val )
Delete( root->pLeft, a );
else
if( a > root->Val )
Delete( root->pRight, a );
else
{
if(!root->pLeft) // there is no left subtree
{
cNode *temp = root;
root = root->pRight;
delete temp;
}
else
if(!root->pRight) // there if no right subtree
{
cNode *temp = root;
root = root->pLeft;
delete temp;
}
else // there are two subtrees
Del( root->pLeft, root );
}
return ;
}
/************************************************** *******/
cTree& cTree::operator -( cNode *DelNode )
{
Delete( pTree, DelNode->Val );
return *this;
}
/************************************************** *******/
void cTree::PrintTree( cNode *temp )
{
if( temp )
{
PrintTree(temp->pLeft);
printf("%d ", temp->Val);
PrintTree(temp->pRight);
}
}
/************************************************** *******/
#define __cTree
#endif