Code
// 构造一棵二叉树
template < typename T >
class BinarySearchTree
{
private:
struct MyNode
{
MyNode* left;
MyNode* right;
MyNode* parent;
T value;
};
MyNode* root;
public:
void Push( const T& node )
{
MyNode* pNode = this->root;
MyNode* pNodeNow = NULL ;
if ( this->root == NULL )
{
this->root = new MyNode();
this->root->value = node;
this->root->left = this->root->right = this->root->parent = NULL;
return ;
}
else
{
while ( pNode )
{
if ( node == pNode->value )
{
return;
}
// 叶子结点, 插入
if ( !pNode->left && !pNode->right )
{
pNodeNow = new MyNode();
pNodeNow->value = node;
node < pNode->value ? pNode->left = pNodeNow : pNode->right = pNodeNow;
return;
}
else if ( node < pNode->value )
{
if ( pNode->left )
{
pNode = pNode->left;
}
else
{
pNodeNow = new MyNode();
pNodeNow->value = node;
pNode->left = pNodeNow;
return;
}
}
else if ( node > pNode->value )
{
if ( pNode->right )
{
pNode = pNode->right;
}
else
{
pNodeNow = new MyNode();
pNodeNow->value = node;
pNode->right = pNodeNow;
return;
}
}
}
}
}
BinarySearchTree()
{
this->root = NULL;
}
~BinarySearchTree()
{
Destory( this->root );
}
void Destory( MyNode* pNode )
{
// 递归遍历并删除每个结点
if ( !pNode )
{
return;
}
Destory( pNode->left );
pNode->left = NULL;
Destory( pNode->right);
pNode->right = NULL;
cout << "delete node" << pNode->value << endl;
delete pNode ;
}
};
void main()
{
int j;
BinarySearchTree<int>* myTree = new BinarySearchTree<int>();
myTree->Push( 3 );
myTree->Push( 5 );
myTree->Push( 4 );
myTree->Push( 7 );
myTree->Push( 2 );
delete myTree;
system( "pause" );
}
// 构造一棵二叉树
template < typename T >
class BinarySearchTree
{
private:
struct MyNode
{
MyNode* left;
MyNode* right;
MyNode* parent;
T value;
};
MyNode* root;
public:
void Push( const T& node )
{
MyNode* pNode = this->root;
MyNode* pNodeNow = NULL ;
if ( this->root == NULL )
{
this->root = new MyNode();
this->root->value = node;
this->root->left = this->root->right = this->root->parent = NULL;
return ;
}
else
{
while ( pNode )
{
if ( node == pNode->value )
{
return;
}
// 叶子结点, 插入
if ( !pNode->left && !pNode->right )
{
pNodeNow = new MyNode();
pNodeNow->value = node;
node < pNode->value ? pNode->left = pNodeNow : pNode->right = pNodeNow;
return;
}
else if ( node < pNode->value )
{
if ( pNode->left )
{
pNode = pNode->left;
}
else
{
pNodeNow = new MyNode();
pNodeNow->value = node;
pNode->left = pNodeNow;
return;
}
}
else if ( node > pNode->value )
{
if ( pNode->right )
{
pNode = pNode->right;
}
else
{
pNodeNow = new MyNode();
pNodeNow->value = node;
pNode->right = pNodeNow;
return;
}
}
}
}
}
BinarySearchTree()
{
this->root = NULL;
}
~BinarySearchTree()
{
Destory( this->root );
}
void Destory( MyNode* pNode )
{
// 递归遍历并删除每个结点
if ( !pNode )
{
return;
}
Destory( pNode->left );
pNode->left = NULL;
Destory( pNode->right);
pNode->right = NULL;
cout << "delete node" << pNode->value << endl;
delete pNode ;
}
};
void main()
{
int j;
BinarySearchTree<int>* myTree = new BinarySearchTree<int>();
myTree->Push( 3 );
myTree->Push( 5 );
myTree->Push( 4 );
myTree->Push( 7 );
myTree->Push( 2 );
delete myTree;
system( "pause" );
}