/***********二叉查找树的实现*************/
//节点类
template
class TreeNode
{
public:
Type data;
TreeNode *left;
TreeNode *right;
TreeNode() :data(Type()), left(nullptr), right(nullptr){}
TreeNode(const Type &d, TreeNode *l = nullptr, TreeNode *r = nullptr) :data(d), left(l), right(r){}
};
//查找树类
template
class BST
{
private:
TreeNode *root;
void deleteByMerging(TreeNode *&node);
void deleteByCopying(TreeNode *&node);
void preorder(TreeNode *p);
void inorder(TreeNode *p);
void postorder(TreeNode *p);
void visit(TreeNode *p) const
{
cout << p->data << ” “;
}
public:
BST();
~BST();
bool insert(const Type &el);
void findAndDeleteByMerging(const Type &el);//合并删除
void findAndDeleteByCopying(const Type &el);//复制删除
Type* find(const Type &el) const;//查找元素
void preorder(); //前序遍历
void inorder(); //中序遍历
void postorder();//后序遍历
};
//构造函数
template
BST::BST()
{
cout << “构造函数,创建一颗空树” << endl;
this->root = nullptr;
}
//析构函数
template
BST::~BST()
{
cout << “析构函数” << endl;
}
//插入一个新节点
template
bool BST::insert(const Type &el)
{
TreeNode *add = new TreeNode(el, nullptr, nullptr);
if (add == nullptr)
return false;
//插入元素,应该先找到插入的位置
TreeNode *move = this->root, *prev = nullptr;
while (move != nullptr)
{
if (move->data == el)
break;
prev = move;
if (move->data < el)
move = move->right;
else
move = move->left;
}
if (this->root == nullptr)
{
this->root = add;
return true;
}
else if (prev->data>el)
{
prev->left = add;
return true;
}
else
{
prev->right = add;
return true;
}
}
//合并删除
template
void BST::deleteByMerging(TreeNode *&node)
{
TreeNode *tmp = node;
if (!node->right)
node = node->left;
else if (!node->left)
node = node->right;
else
{
tmp = node->left;
while (tmp->right != nullptr)
tmp = tmp->right;
tmp->right = node->right;
tmp = node;
node = node->left;
}
delete tmp;
}
template
void BST::findAndDeleteByMerging(const Type &el)
{
if (this->root == nullptr)
{
cout << “树为空,无法删除元素” << endl;
return;
}
TreeNode *move = this->root, *prev = nullptr;
while (move != nullptr)
{
if (move->data == el)
break;
prev = move;
if (move->data < el)
move = move->right;
else
move = move->left;
}
if (move != nullptr)
{
if (this->root->data == el)//这里必须要判断,防止prev=nullptr则下面两个条件都将不成立
deleteByMerging(this->root);
else if (prev->left == move)
deleteByMerging(prev->left);
else
deleteByMerging(prev->right);
}
else
cout << “在树中未查找到该元素” << endl;
}
//复制删除
template
void BST::deleteByCopying(TreeNode *&node)
{
TreeNode *tmp = node, *prev;
if (!node->right)
node = node->left;
else if (!node->left)
node = node->right;
else
{
tmp = node->left;
prev = node;
while (tmp->right != nullptr)
{
prev = tmp;
tmp = tmp->right;
}
node->data = tmp->data;
if (prev->left==tmp)
prev->left = tmp->left;
else
prev->right = tmp->left;
}
delete tmp;
}
template
void BST::findAndDeleteByCopying(const Type &el)
{
if (this->root == nullptr)
{
cout << “树为空,无法删除元素” << endl;
return;
}
TreeNode *move = this->root, *prev = nullptr;
while (move != nullptr)
{
if (move->data == el)
break;
prev = move;
if (move->data < el)
move = move->right;
else
move = move->left;
}
if (move != nullptr)
{
if (move == this->root)
deleteByCopying(this->root);
if (move == prev->left)
deleteByCopying(prev->left);
else
deleteByCopying(prev->right);
}
else
cout << “在树中未查找到该元素” << endl;
}
//查找元素
template
Type* BST::find(const Type &el) const
{
if (this->root == nullptr)
{
cout << “树为空树,无法查找” << endl;
return nullptr;
}
TreeNode *move = this->root;
while (move != nullptr)
{
if (move->data == el)
return &move->data;
if (move->data < el)
move = move->right;
else
move = move->left;
}
if (move == nullptr)
{
cout << “树中未查到该元素” << endl;
return nullptr;
}
}
//前序遍历
template
void BST::preorder(TreeNode *p)
{
if (p != nullptr)
{
visit(p);
preorder(p->left);
preorder(p->right);
}
}
template
void BST::preorder()
{
preorder(this->root);
}
//中序遍历
template
void BST::inorder(TreeNode *p)
{
if (p != nullptr)
{
inorder(p->left);
visit(p);
inorder(p->right);
}
}
template
void BST::inorder()
{
inorder(this->root);
}
//后序遍历
template
void BST::postorder(TreeNode *p)
{
if (p != nullptr)
{
postorder(p->left);
postorder(p->right);
visit(p);
}
}
template
void BST::postorder()
{
postorder(this->root);
}
//测试程序
int main()
{
BST tree;
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(5);
tree.insert(20);
tree.insert(40);
tree.insert(4);
tree.insert(7);
tree.insert(3.8);
tree.insert(4.2);
tree.insert(6);
tree.insert(8);
tree.insert(4.1);
tree.preorder();
cout << endl;
//tree.findAndDeleteByCopying(5);
//tree.findAndDeleteByMerging(5);
cout << *tree.find(5) << endl;
//tree.preorder();
cout << endl;
return 0;
}