C++泛型编程实现二叉搜索树BST
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++泛型编程实现二叉搜索树BST相关的知识,希望对你有一定的参考价值。
代码如下:
#include <iostream>
using namespace std;
template <typename K,typename V>
struct BNode
{
typedef BNode<K,V> Node;
K _key;
V _value;
Node * _left;
Node *_right;
BNode(const K & key,const V & value) :_key(key),_value(value), _left(nullptr), _right(nullptr) {}
};
template<typename K,typename V>
class BTree
{
public:
typedef BNode<K,V> Node;
BTree():_root(nullptr){}
Node *find(const K & key)
{
Node *cur = _root;
while (cur)
{
if (cur->_data == key) return cur;
else if (cur->_data > key) cur = cur->_left;
else cur = cur->_right;
}
}
bool Insert(const K & key ,const V & value)
{
if (_root == nullptr)
{
_root = new Node(key,value);
return true;
}
Node *cur = _root;
Node *parent = nullptr;
while (cur)
{
parent = cur;
if (cur->_key == key) return false;
else if (cur->_key> key) cur = cur->_left;
else cur = cur->_right;
}
cur = new Node(key,value);
if (parent->_key > key) parent->_left = cur;
else parent->_right = cur;
return true;
}
void inorder()
{
_inorder(_root);
}
void _inorder(Node *root)
{
if (root)
{
_inorder(_root->_left);
cout << _root->_key << "-->"<<root->_value<<" ";
_inorder(_root->_right);
}
}
Node *copy(Node * root)
{
if (root == nullptr) return nullptr;
Node *newNode = new Node(root->_key,root->_value);
newNode->_left = copy(_root->_left);
newNode->_right = copy(_root->_right);
return newNode;
}
BTree(const BTree<K,V> & btree):_root(copy(btree._root)){}
void destroy(Node * root)
{
if (root)
{
destroy(_root->_left);
destroy(_root->_right);
delete root;
}
}
~BTree()
{
if (_root)
{
destroy(_root);
_root = nullptr;
}
}
bool erase(const K & key)
{
Node *cur = _root;
Node *parent = nullptr;
while (cur)
{
if (cur->_key == key) break;
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
parent = cur;
cur = cur->_right;
}
}
//判断是否找到了需要删除的结点
if (cur == nullptr) return false;
//删除结点
//1.删除的结点为叶子结点
if (cur->_left == nullptr && cur->_right == nullptr)
{
//特殊情况是否根结点
if (cur == _root)
{
_root = nullptr;
}
else
{
if (parent->_left == cur) parent->_left = nullptr;
else parent->_right = nullptr;
}
delete cur;
}
//2.要删除的结点存在一个孩子
else if (cur->_left == nullptr)
{
if (cur == _root) _root = nullptr;
else
{
if (parent->_left == cur) parent->_left = cur->_right;
else parent->_right = cur->_right;
}
delete cur;
}
else if (cur->_right == nullptr)
{
if (cur == _root) _root = nullptr;
else
{
if (parent->_left == cur) parent->_left = cur->_left;
else parent->_right = cur->_left;
}
delete cur;
}
//3.要删除的结点左右孩子都存在
else
{
Node *leftRightMost = cur->_left;
parent = cur;
while (leftRightMost->_right)
{
parent = leftRightMost;
leftRightMost = leftRightMost->_right;
}
swap(cur->_key, leftRightMost->_key);
swap(cur->_value, leftRightMost->_value);
if (parent->_left == leftRightMost) parent->_left = leftRightMost->_left;
else parent->_right = leftRightMost->_left;
delete leftRightMost;
}
return true;
}
private:
Node * _root;
};
以上是关于C++泛型编程实现二叉搜索树BST的主要内容,如果未能解决你的问题,请参考以下文章