目录
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
它的左右子树也分别为二叉搜索树
1、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
2、最多查找高度次,走到到空,还没找到,这个值不存在。
插入的具体过程如下:
1、树为空,则直接新增节点,赋值给root指针
2、树不空,按二叉搜索树性质查找插入位置,插入新节点
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点,删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除
c. 要删除的结点只有右孩子结点,删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除
d. 要删除的结点有左、右孩子结点,在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题--替换法删除
- template<class K, class V >
- struct BSTreeNode
- {
- BSTreeNode
* _left; - BSTreeNode
* _right; - K _key;
- V _vaule;
-
- BSTreeNode(const K& key, const V& vaule = key)
- :_left(nullptr)
- ,_right(nullptr)
- ,_key(key)
- ,_vaule(vaule)
- {}
- };
-
- template<class K, class V >
- class BSTree
- {
- typedef BSTreeNode
Node; - public:
- BSTree() = default;
-
- BSTree(const BSTree
& t) - {
- _root = CopyTree(t._root);
- }
-
- BSTree
& operator=(BSTree t) - {
- swap(_root, t._root);
- return *this;
- }
-
- ~BSTree()
- {
- DestoryTree(_root);
- _root == nullptr;
- }
- //迭代实现
- bool Insert(const K& key, const V& value = key)
- {
- if (_root == nullptr)
- {
- _root = new Node(key, value);
- 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, value);
- if (parent->_key > key)
- {
- parent->_left = cur;
- }
- else
- {
- parent->_right = cur;
- }
- return true;
- }
- Node* Find(const K& key)
- {
- Node* cur = _root;
- while (cur)
- {
- if (cur->_key < key)
- {
- cur = cur->_right;
- }
- else if (cur->_key > key)
- {
- cur = cur->_left;
- }
- else
- {
- return cur;
- }
- }
- return nullptr;
- }
- 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 (cur == _root)
- {
- _root = cur->_right;
- }
- else
- {
- if (cur == parent->_left)
- {
- parent->_left = cur->_right;
- }
- else
- {
- parent->_right = cur->_right;
- }
- }
- delete cur;
- }
- else if (cur->_right == nullptr)
- {
- if (cur == _root)
- {
- _root = cur->_left;
- }
- else
- {
- if (cur == parent->_left)
- {
- parent->_left = cur->_left;
- }
- else
- {
- parent->_right = cur->_left;
- }
- }
- delete cur;
- }
- else
- {
- Node* minparent = cur;
- Node* minRight = cur->_right;
- while (minRight->_left)
- {
- minparent = minRight;
- minRight = minRight->_left;
- }
-
- swap(cur->_key, minRight->_key);
-
- if (minparent->_left == minRight)
- {
- minparent->_left = minRight->_right;
- }
- else
- {
- minparent->_right = minRight->_right;
- }
- delete minRight;
- }
- return true;
- }
- }
- return false;
- }
- void InOrder()
- {
- _InOrder(_root);
- }
-
- //递归实现
- bool FindR(const K& key, const V& value)
- {
- return _FindR(_root, key, value);
- }
- bool EraseR(const K& key)
- {
- return _EraseR(_root, key);
- }
- bool InsertR(const K& key, const V& value)
- {
- return _InsertR(_root, key, value);
- }
- private:
- bool _FindR(Node* root, const K& key, const V& value)
- {
- if (root == nullptr)
- {
- return false;
- }
- if (root->_key > key)
- {
- return _FindR(_root, key, value);
- }
- else if (root->_key < key)
- {
- return _FindR(_root, key, value);
- }
- else
- {
- return true;
- }
- }
- 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
- {
- Node* minRight = root->_right;
- while (minRight->_left)
- {
- minRight = minRight->_left;
- }
- swap(root->_key, minRight->_key);
-
- return _EraseR(root->_right, key);
- }
- delete del;
- return true;
- }
- }
- bool _InsertR(Node*& root,const K& key, const V& value)
- {
- if (root == nullptr)
- {
- root = new Node(key, value);
- return true;
- }
- if (root->_key > key)
- {
- return _InsertR(root->_left, key, value);
- }
- else if (root->_key < key)
- {
- return _InsertR(root->_right, key, value);
- }
- else
- {
- return false;
- }
- }
- void _InOrder(Node* root)
- {
- if (root == nullptr)
- {
- return;
- }
- _InOrder(root->_left);
- cout << root->_vaule << " ";
- _InOrder(root->_right);
- }
- void DestoryTree(Node* root)
- {
- if (root == nullptr)
- {
- return;
- }
- DestoryTree(root->_left);
- DestoryTree(root->_right);
- delete root;
- }
- Node* CopyTree(Node* root)
- {
- if (root == nullptr)
- {
- return nullptr;
- }
- Node* copyNode = new Node(root->_key, root->_vaule);
- copyNode->_left = CopyTree(root->_left);
- copyNode->_right = CopyTree(root->_right);
-
- return copyNode;
- }
- private:
- Node* _root = nullptr;
- };