C++实现搜索二叉树
> #pragma once
> using namespace std;
> template<class K>
> struct BSTreeNode
> {
> BSTreeNode<K>* _left;
> BSTreeNode<K>* _right;
> K _key;
> BSTreeNode(const K& key)
> :_left(nullptr)
> , _right(nullptr)
> , _key(key)
> {}
> };
>
> template<class K>
> struct BSTree
> {
> typedef BSTreeNode<K> Node;
> public:
> BSTree()
> :_root(nullptr)
> {}
>
> bool Insert(const K& key)
> {
> if (_root == nullptr)
> {
> _root = new Node(key);
> return true;
> }
> Node* parent = nullptr;
> Node* cur = _root;
> while (cur)
> {
> if (cur->_key > key)
> {
> parent = cur;
> cur = cur->_left;
> }
> else if (cur->_key < key)
> {
> parent = cur;
> cur = cur->_right;
> }
> else
> return false;
> }
> cur = new Node(key);
> if (parent->_key > key)
> {
> parent->_left = cur;
> }
> else if (parent->_key < key)
> {
> parent->_right = cur;
> }
> return true;
> }
> bool Find(const K& key)
> {
> Node* cur = _root;
> while (cur)
> {
> if (cur->_key > key)
> {
> cur = cur->_left;
> }
> else if (cur->_key < key)
> {
> cur = cur->_right;
> }
> else
> return true;
> }
> return false;
> }
>
> void InOrder()
> {
> _InOrder(_root);
> cout << endl;
>
> }
>
> void _InOrder(Node* root)
> {
> if (root == nullptr)
> {
> return;
> }
> _InOrder(root->_left);
> cout << root->_key << " ";
> _InOrder(root->_right);
> }
> bool Erase(const K& key)
> {
> Node* parent = nullptr;
> Node* cur = _root;
> while (cur)
> {
> if (cur->_key > key)
> {
> parent = cur;
> cur = cur->_left;
> }
> else if (cur->_key < key)
> {
> parent = cur;
> cur = cur->_right;
> }
> else
> {
>
> if (cur->_left == nullptr)
> {
> if (parent == nullptr)
> {
> _root = cur->_right;
> }
> else
> {
> if (parent->_left == cur)
> parent->_left = cur->_right;
> else
> parent->_right = cur->_right;
> }
> delete cur;
> }
> else if (cur->_right == nullptr)
> {
> if (parent == nullptr)
> {
> _root = cur->_left;
> }
> else
> {
> if (parent->_left == cur)
> parent->_left = cur->_left;
> else
> parent->_right = cur->_left;
> }
> delete cur;
> }
> else
> {
> Node* minparent = cur;
> Node* min = cur->_right;
> while (min->_left)
> {
> minparent = min;
> min = min->_left;
> }
> cur->_key = min->_key;
> if (minparent->_left == min)
> minparent->_left = min->_right;
> else
> minparent->_right = min->_right;
> delete min;
> }
> return true;
> }
> }
> return false;
> }
>
> bool _InsertR(Node*& root, const K& key)
> {
> if (root == nullptr)
> {
> root = new Node(key);
> return true;
> }
> if (root->_key > key)
> {
> return _InsertR(root->_left, key);
> }
> else if (root->_key < key)
> {
> return _InsertR(root->_right, key);
> }
> else
> return false;
> }
>
> bool InsertR(const K& key)
> {
> return _InsertR(_root, key);
> }
>
> bool _EraseR(Node*& root, const K& key)
> {
> if (root == nullptr)
> {
> return false;
> }
> if (root->_key > key)
> {
> return _EraseR(root->_left, key);
> }
> else if (root->_key < key)
> {
> return _EraseR(root->_right, key);
> }
> else
> {
> Node* del = root;
> if (root->_left == nullptr)
> root = root->_right;
> else if (root->_right == nullptr)
> root = root->left;
> else
> delete del;
> }
>
> }
>
> Node* EraseR(const K& key)
> {
> return _EraseR(_root, key);
> }
>
> private:
> Node* _root;
> };