文章目录
- 二叉搜索树
- 二叉搜索树的概念
- 二叉搜索树的操作
- 搜索二叉树的具体实现
二叉搜索树
二叉搜索树的概念
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
二叉搜索树的操作
-
二叉搜索树的查找
a、从根开始比较,比根大走右边,比根小走左边
b、如果走到到空,还没找到,这个值不存在 -
二叉搜索树的插入
插入的具体过程如下:
a. 树为空,则直接新增节点,赋值给root指针
b. 树不空,按二叉搜索树性质查找插入位置,插入新节点 -
二叉搜索树的删除
首先查找是否在二叉搜索树中,如果不存在则返回,如果存在,则分三种情况:
情况a:如果该节点只有左孩子节点,删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点–直接删除
情况b:如果该节点只有右孩子节点,删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点–直接删除
情况c:如果该节点右左,右孩子节点,在它的右子树中寻找中序下的第一个结点(,用它的值填补到被删除节点中,再来处理该结点的删除问题–替换法删除
搜索二叉树的具体实现
template<class K>struct BSTNode{typedef BSTNode<K> Node;Node* _left;Node* _right;K _key;BSTNode(const K& key):_left(nullptr),_right(nullptr),_key(key){}};template<class K>class BSTree{typedef BSTNode<K> Node;public:BSTree() = default;BSTree(const BSTree<K>& t){_root = _Copy(t._root);}BSTree<int> operator=(BSTree<K> t){swap(_root, t._root);return *this;}~BSTree(){_Destroy(_root);_root = nullptr;}bool Find(const K& key){Node* curr = _root;while (curr){if (curr->_key > key)curr = curr->_left;else if (curr->_key < key)curr = curr->_right;elsereturn true;}return false;}bool Insert(const K& key){if (_root == nullptr){_root = new Node(key);return true;}Node* parent = nullptr;Node* curr = _root;while (curr){if (curr->_key > key){parent = curr;curr = curr->_left;}else if (curr->_key < key){parent = curr;curr = curr->_right;}elsereturn false;}Node* newnode = new Node(key);if (parent->_key < key)parent->_right = newnode;elseparent->_left = newnode;return true;}bool Erase(const K& key){Node* parent = nullptr;Node* curr = _root;while (curr){if (curr->_key > key){parent = curr;curr = curr->_left;}else if (curr->_key < key){parent = curr;curr = curr->_right;}else {//找到了//左子树为空if (curr->_left == nullptr){//如果找到了这个节点且父亲为空,那么这个节点一定是rootif (parent == nullptr){delete _root;_root = nullptr;}else{if (parent->_left == curr)parent->_left = curr->_right;elseparent->_right = curr->_right;delete curr;}}//右子树为空else if (curr->_right == nullptr){//同上if (parent == nullptr){delete _root;_root = nullptr;}else{if (parent->_left == curr)parent->_left = curr->_left;elseparent->_right = curr->_left;delete curr;}}//两颗子树都不为空else{Node* minParent = curr;Node* minNode = curr->_right;while (minNode->_left){minParent = minNode;minNode = minNode->_left;}swap(curr->_key, minNode->_key);if (minParent->_left == minNode)minParent->_left = minNode->_right;elseminParent->_right = minNode->_right;delete minNode;}return true;}}return false;}void InOrder(){_InOrder(_root);cout << '\n';}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);}private:bool _EraseR(Node*& root, const K& key){if (root == nullptr)return false;if (root->_key < key)return _EraseR(root->_right, key);else if (root->_key > key)return _EraseR(root->_left, key);else{//找到了if (root->_left == nullptr){if (root == _root){delete _root;_root = nullptr;}else{root = root->_right;}}else if (root->_right == nullptr){if (root == _root){delete _root;_root = nullptr;}else{root = root->_left;}}else{Node* minParent = root;Node* minNode = root->_right;while (minNode->_left){minParent = minNode;minNode = minNode->_left;}swap(root->_key, minNode->_key);return _EraseR(root->_right, key);}return true;}}//因为传的是引用,所以root是上一栈帧中root的左/右节点指针的别名//修改root,就是修改上一栈帧中root的左右节点指针bool _InsertR(Node*& root, const K& key){if (root == nullptr){root = new Node(key);return true;}if (root->_key < key)return _InsertR(root->_right, key);else if (root->_key > key)return _InsertR(root->_left, key);elsereturn false;}bool _FindR(Node* root, const K& key){if (root == nullptr)return false;if (root->_key > key)return _FindR(root->_left, key);else if (root->_key < key)return _FindR(root->_right, key);elsereturn true;}void _Destroy(Node* root){if (root == nullptr)return;_Destroy(root->_left);_Destroy(root->_right);delete root;}Node* _Copy(Node* root){if (root == nullptr) return nullptr;Node* newnode = new Node(root->_key);newnode->_left = _Copy(root->_left);newnode->_right = _Copy(root->_right);return newnode;}void _InOrder(Node* root){if (root == nullptr)return;_InOrder(root->_left);cout << root->_key << ' ';_InOrder(root->_right);}Node* _root = nullptr;};