赞
踩
二插搜索树,也称为二叉查找树(BST),是一种特殊的二叉树,它的每个节点都包含一个可进行比较的键及其对应的值,且每个节点的左子树中所有键都小于该节点的键,右子树中所有键都大于该节点的键。
二叉搜索树是一种递归数据结构,它或者是一棵空树,或者是具有下列性质的二叉树:若它的左子树不为空,则左子树上所有节点的值都小于它的根节点;若它的右子树不为空,则右子树上所有节点的值都大于它的根节点;它的左右子树也分别为二叉搜索树。
template<class K>
struct BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
BSTreeNode(const K& key)
:_left(nullptr)
, _right(nullptr)
, _key(key)
{}
};
class BSTree { typedef BSTreeNode<K> Node;//typedef一下,方便书写 public: //构造函数 BSTree() :_root(nullptr) {} //析构函数 ~BSTree() { Destroy(_root); } protected://不希望在类外面掉调到的函数用protected保护起来 void Destroy(Node*& root) { if (root == nullptr) return; Destroy(root->_left); Destroy(root->_right); delete root; root = nullptr; } private: Node* _root; }
bool Insert(const K& key) { if (_root == NULL) { _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 { parent->_right = cur; } return true; }
递归:
递归一般要在类里面套一层,因为递归调位需要传参(_root),而在类外不能访问_root;bool _InsertR(Node*& root, const K& key),要引用root,这样就容易连接起来;
bool InsertR(const K& key) { return _InsertR(_root, key); } protected: bool _InsertR(Node*& root, const K& key) { if (root == NULL) { 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 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; }
递归:
bool FindR(const K& key) { return _FindR(_root, key); } protected: bool _FindR(Node* root, const K& key) { if (root == nullptr) return false; if (root->_key == key) return true; if (root->_key > key) { return _FindR(root->_left, key); } else { return _FindR(root->_right, key); } }
bool Erase(const K& key) { Node* parent = _root; 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 (cur == _root) { _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 (cur == _root) { _root == cur->_left; } else { if (parent->_left == cur) { parent->_left = cur->_left; } else { parent->_right = cur->_left; } } delete cur; } //都不为空, else { // 找右树最小节点替代, //Node* pminRight = cur; //Node* minRight = cur->_right; //while (minRight->_left) //{ // pminRight = minRight; // minRight = minRight->_left; //} //cur->_key = minRight->_key; //if (pminRight->_left == minRight) //{ // pminRight->_left = minRight->_right; //} //else //{ // pminRight->_right = minRight->_right; //} //delete minRight; //可以是左树最大节点替代 Node* pmaxLeft = cur; Node* maxLeft = cur->_left; while (maxLeft->_right) { pmaxLeft = maxLeft; maxLeft = maxLeft->_right; } cur->_key = maxLeft->_key; if (pmaxLeft->_left == maxLeft) { pmaxLeft->_left = maxLeft->_left; } else { pmaxLeft->_right = maxLeft->_left; } delete maxLeft; } return true; } } return false; }
递归:
bool EraseR(const K& key) { return _EraseR(_root, key); } protected: 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* cur = root; if (root->_left == nullptr) { root = root->_right; } else if (root->_right == nullptr) { root = root->_left; } else { Node* maxLeft = root->_left; while (maxLeft->_right) { maxLeft = maxLeft->_right; } swap(cur->_key, maxLeft->_key); return _EraseR(root->_left, key); } delete cur; return true; } }
完整代码如下:
//结点 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> class BSTree { typedef BSTreeNode<K> Node; public: BSTree() :_root(nullptr) {} //BSTree() = default;//制定强制生成默认构造参数 void InOrder() { _InOrder(_root); cout << endl; } ~BSTree() { Destroy(_root); } bool Insert(const K& key) { if (_root == NULL) { _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 { 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; } bool Erase(const K& key) { Node* parent = _root; 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 (cur == _root) { _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 (cur == _root) { _root == cur->_left; } else { if (parent->_left == cur) { parent->_left = cur->_left; } else { parent->_right = cur->_left; } } delete cur; } //都不为空, else { // 找右树最小节点替代, //Node* pminRight = cur; //Node* minRight = cur->_right; //while (minRight->_left) //{ // pminRight = minRight; // minRight = minRight->_left; //} //cur->_key = minRight->_key; //if (pminRight->_left == minRight) //{ // pminRight->_left = minRight->_right; //} //else //{ // pminRight->_right = minRight->_right; //} //delete minRight; //可以是左树最大节点替代 Node* pmaxLeft = cur; Node* maxLeft = cur->_left; while (maxLeft->_right) { pmaxLeft = maxLeft; maxLeft = maxLeft->_right; } cur->_key = maxLeft->_key; if (pmaxLeft->_left == maxLeft) { pmaxLeft->_left = maxLeft->_left; } else { pmaxLeft->_right = maxLeft->_left; } delete maxLeft; } return true; } } return false; } bool FindR(const K& key) { return _FindR(_root, key); } bool InsertR(const K& key) { return _InsertR(_root, key); } bool EraseR(const K& key) { return _EraseR(_root, key); } protected: bool _FindR(Node* root, const K& key) { if (root == nullptr) return false; if (root->_key == key) return true; if (root->_key > key) { return _FindR(root->_left, key); } else { return _FindR(root->_right, key); } } bool _InsertR(Node*& root, const K& key) { if (root == NULL) { 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 _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* cur = root; if (root->_left == nullptr) { root = root->_right; } else if (root->_right == nullptr) { root = root->_left; } else { Node* maxLeft = root->_left; while (maxLeft->_right) { maxLeft = maxLeft->_right; } swap(cur->_key, maxLeft->_key); return _EraseR(root->_left, key); } delete cur; return true; } } void Destroy(Node*& root) { if (root == nullptr) return; Destroy(root->_left); Destroy(root->_right); delete root; root = nullptr; } void _InOrder(Node* root) { if (root == nullptr) return; _InOrder(root->_left); cout << root->_key << " "; _InOrder(root->_right); } private: Node* _root = nullptr; };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。