搜索二叉树又称二叉排序树,二叉搜索树,它或者是一棵空树,或者是具有以下性质的二叉树:
若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
它的左右子树也分别为搜索二叉树。
a、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
b、最多查找高度次,走到到空,还没找到,这个值不存在。
- template <class K>
- bool BSTree
::Find(const K& key) - {
- node* cur = _root;
- while (cur)
- {
- if (key < cur->_key)//小就往左走
- {
- cur = cur->_left;
- }
- else if (key > cur->_key)//大就往右走
- {
- cur = cur->_right;
- }
- else//找到了
- {
- return true;
- }
- }
- return false;
- }
a. 树为空,则直接新增节点,赋值给root指针
b. 树不空,按搜索二叉树性质查找插入位置,插入新节点
- template <class K>
- bool BSTree
::Insert(const K& key) - {
- //树为空,则直接新增节点,赋值给root指针
- if (_root == nullptr)
- {
- _root = new node(key);
- return true;
- }
- node* parent = nullptr;
- node* cur = _root;
- while (cur)//找到key该去的位置
- {
- parent = cur;
- if (cur->_key < key)//大就往右走
- {
- cur = cur->_right;
- }
- else if (cur->_key > key)//小就往左走
- {
- cur = cur->_left;
- }
- else//有相等的值了无法再插入了
- {
- return false;
- }
- }
- if (parent->_key < key)
- {
- parent->_right = new node(key);
- }
- else
- {
- parent->_left = new node(key);
- }
-
- return true;
- }
删除的情况最为复杂,首先查找元素是否在搜索二叉树中,如果不存在,则返回, 否则要删除的结点分下面四种情况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点
c. 要删除的结点只有右孩子结点
d. 要删除的结点有左、右孩子结点
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程 如下:
情况a:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除
情况b:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除
情况c:在它的右子树中寻找中序下的第一个结点(关键码最小),或者在它的左子树中寻找中序下的第一个结点(关键码最大)用它的值填补到被删除节点中,再来处理该结点的删除问题--替换法删除。
- template <class K>
- bool BSTree
::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//找到了就跳出循环
- {
- break;
- }
- }
- if (cur == nullptr)//cur走到空就意味着没找到
- {
- return false;
- }
- if (cur->_left == nullptr)//左为空
- {
- if (cur == _root)
- {
- _root = cur->_right;
- }
- else if (cur == parent->_left)
- {
- parent->_left = cur->_right;
- }
- else if (cur == parent->_right)
- {
- parent->_right = cur->_right;
- }
- delete cur;
- return true;
- }
- else if (cur->_right == nullptr)//右为空
- {
- if (cur == _root)
- {
- _root = cur->_left;
- }
- else if (cur == parent->_left)
- {
- parent->_left = cur->_left;
- }
- else if (cur == parent->_right)
- {
- parent->_right = cur->_left;
- }
- delete cur;
- return true;
- }
- else//左右都不为空,去找它左树最大的节点替换它的值,再删除左树最大的节点
- //下面的图有做说明
- {
- node* parent = nullptr;
- node* leftMax = cur;
- while (leftMax->_right)//找到左树最大的节点
- {
- parent = leftMax;
- leftMax = leftMax->_right;
- }
- swap(cur->_key, leftMax->_key);//交换值
- if (parent->_left == leftMax)
- {
- parent->_left = leftMax->_left;
- }
- else
- {
- parent->_right = leftMax->_left;
- }
- delete leftMax;
- return true;
- }
- return false;
- }
- #pragma once
-
- #include
- using namespace std;
-
-
- template <class K>
- struct BSTreeNode
- {
- BSTreeNode* _left;
- BSTreeNode* _right;
- K _key;
-
- BSTreeNode(const K& key)
- :_left(nullptr)
- ,_right(nullptr)
- ,_key(key)
- {}
- };
-
- template <class K>
- class BSTree
- {
- private:
- BSTreeNode
* _root; - typedef BSTreeNode
node; - public:
- BSTree()
- :_root(nullptr)
- {
-
- }
- ~BSTree()
- {
- Destroy(_root);
- }
- //增删查
- bool Insert(const K& key);
- bool Find(const K& key);
- bool Erase(const K& key);
- //中序遍历
- void InOrder();
- void _InOrder(node* root);
- //增删查的递归实现
- bool InsertR(const K& key);
- bool _InsertR(const K& key, node*& root);
- //为了对节点进行修改,这里的插入和删除的节点必须用引用传,这里是一个细节
- bool EraseR(const K& key);
- bool _EraseR(const K& key, node*& root);
- bool FindR(const K& key);
- bool _FindR(const K& key, node* root);
-
- void Destroy(node* root);
- };
-
-
- template <class K>
- bool BSTree
::Insert(const K& key) - {
- //树为空,则直接新增节点,赋值给root指针
- if (_root == nullptr)
- {
- _root = new node(key);
- return true;
- }
- node* parent = nullptr;
- node* cur = _root;
- while (cur)//找到key该去的位置
- {
- parent = cur;
- if (cur->_key < key)//大就往右走
- {
- cur = cur->_right;
- }
- else if (cur->_key > key)//小就往左走
- {
- cur = cur->_left;
- }
- else//有相等的值了无法再插入了
- {
- return false;
- }
- }
- if (parent->_key < key)
- {
- parent->_right = new node(key);
- }
- else
- {
- parent->_left = new node(key);
- }
-
- return true;
- }
-
- template <class K>
- bool BSTree
::Find(const K& key) - {
- node* cur = _root;
- while (cur)
- {
- if (key < cur->_key)//小就往左走
- {
- cur = cur->_left;
- }
- else if (key > cur->_key)//大就往右走
- {
- cur = cur->_right;
- }
- else//找到了
- {
- return true;
- }
- }
- return false;
- }
-
- template <class K>
- bool BSTree
::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//找到了就跳出循环
- {
- break;
- }
- }
- if (cur == nullptr)//cur走到空就意味着没找到
- {
- return false;
- }
- if (cur->_left == nullptr)//左为空
- {
- if (cur == _root)
- {
- _root = cur->_right;
- }
- else if (cur == parent->_left)
- {
- parent->_left = cur->_right;
- }
- else if (cur == parent->_right)
- {
- parent->_right = cur->_right;
- }
- delete cur;
- return true;
- }
- else if (cur->_right == nullptr)//右为空
- {
- if (cur == _root)
- {
- _root = cur->_left;
- }
- else if (cur == parent->_left)
- {
- parent->_left = cur->_left;
- }
- else if (cur == parent->_right)
- {
- parent->_right = cur->_left;
- }
- delete cur;
- return true;
- }
- else//左右都不为空,去找它左树最大的节点替换它的值,再删除左树最大的节点
- {
- node* parent = nullptr;
- node* leftMax = cur;
- while (leftMax->_right)//找到左树最大的节点
- {
- parent = leftMax;
- leftMax = leftMax->_right;
- }
- swap(cur->_key, leftMax->_key);//交换值
- if (parent->_left == leftMax)
- {
- parent->_left = leftMax->_left;
- }
- else
- {
- parent->_right = leftMax->_left;
- }
- delete leftMax;
- return true;
- }
- return false;
- }
-
- template <class K>
- void BSTree
::_InOrder(node* root) - {
- if (root == nullptr)
- return;
- _InOrder(root->_left);
- cout << root->_key << " ";
- _InOrder(root->_right);
- }
-
- template <class K>
- void BSTree
::InOrder() - {
- _InOrder(_root);
- cout << endl;
- }
-
-
- template <class K>
- bool BSTree
::EraseR(const K& key) - {
- return _EraseR(key, _root);
- }
-
-
- template <class K>
- bool BSTree
::_EraseR(const K& key, node*& root) - {
- if (root == nullptr)
- return false;
- if (root->_key < key)
- {
- _EraseR(key, root->_right);
- }
- else if (root->_key > key)
- {
- _EraseR(key, root->_left);
- }
- else//找到要删除的节点了
- {
- //准备开始删除
- node* del = root;
- if (root->_left == nullptr)
- {
- root = root->_right;
- }
- else if (root->_right == nullptr)
- {
- root = root->_left;
- }
- else
- {
- node* leftMax = root->_left;
- while (leftMax->_right)
- {
- leftMax = leftMax->_right;
- }
- swap(root->_key, leftMax->_key);
- return _EraseR(key, root->_left);//交换完后去要删除节点的左子树删除最大的节点
- }
- delete del;
- }
- return true;
- }
-
- template <class K>
- bool BSTree
::FindR(const K& key) - {
- return _FindR(key, _root);
- }
-
- template <class K>
- bool BSTree
::_FindR(const K& key, node* root) - {
- if (root == nullptr)
- {
- return false;
- }
- if (root->_key < key)
- {
- return _FindR(key, root->_right);
- }
- else if (root->_key > key)
- {
- return _FindR(key, root->_left);
- }
- else
- {
- return true;
- }
- }
-
- template <class K>
- bool BSTree
::InsertR(const K& key) - {
- return _InsertR(key, _root);
- }
- template <class K>
- bool BSTree
::_InsertR(const K& key, node*& root) - {
- if (root == nullptr)
- {
- root = new node(key);
- return true;
- }
- if (root->_key < key)
- {
- return _InsertR(key, root->_right);
- }
- else if (root->_key > key)
- {
- return _InsertR(key, root->_left);
- }
- else
- {
- return false;
- }
- }
-
- template <class K>
- void BSTree
::Destroy(node* root) - {
- if (root == nullptr)
- return;
- Destroy(root->_left);
- Destroy(root->_right);
- delete root;
- }
- //test.c
-
- #include "BinarySearchTree.h"
-
- int main()
- {
- BSTree<int> bs;
-
- int arr[] = { 1,3,6,4,7,8,10,14,13 };
- for (auto e : arr)
- {
- bs.Insert(e);
- }
- bs.InOrder();
-
- bs.EraseR(1);
- bs.InOrder();
-
- bs.Insert(20);
- bs.InsertR(9);
- bs.InOrder();
-
- bool ret = bs.FindR(20);
- cout << ret << endl;
-
-
- return 0;
- }