二叉搜索树性质:
- 每个节点都有一个作为搜索依据的关键码(key),所有节点的关键码互不相同。
- 左子树上所有节点的关键码(key)都小于根节点的关键码(key)。
- 右子树上所有节点的关键码(key)都大于根节点的关键码(key)。
- 左右子树都是二叉搜索树。
代码实现:
/*BSTree.h*/
#include <iostream>
using namespace std;
template <class K, class V>
struct BSTreeNode
{
typedef BSTreeNode<K, V> Node;
K _key;
V _value;
Node* _left;
Node* _right;
BSTreeNode(const K& key, const V& value)
:_key(key)
,_value(value)
,_left(NULL)
,_right(NULL)
{}
};
template <class K, class V>
class BSTree
{
typedef BSTreeNode<K, V> Node;
public:
BSTree()
:_root(NULL)
{}
public:
bool Insert(const K& key, const V& value)
{
if(_root == NULL)
_root = new Node(key, value);
Node* cur = _root;
while(cur)
{
if(cur->_key > key) //key小,插入左子树
{
if(cur->_left == NULL)
cur->_left = new Node(key, value);
else
cur = cur->_left;
}
else if(cur->_key < key) //key大,插入右子树
{
if (cur->_right == NULL)
cur->_right = new Node(key, value);
else
cur = cur->_right;
}
else
{
return false;
}
}
return true;
}
Node* Find(const K& key)
{
if(_root == NULL)
{
return NULL;
}
Node* cur = _root;
while(cur)
{
if(key > cur->_key) //key大,走右子树
{
cur = cur->_right;
}
else if(key < cur->_key) //key小,走左子树
{
cur= cur->_left;
}
else
{
return cur;
}
}
return NULL;
}
bool Remove(const K& key)
{
if(_root == NULL) //无节点
{
return false;
}
else if(_root->_left == NULL && _root->_right == NULL) //一个节点
{
if(_root->_key == key) //key值正好是_root->_key,直接删除
{
delete _root;
_root = NULL;
return true;
}
else
{
return false;
}
}
else //两个及两个以上节点
{
Node* cur = _root;
Node* parent = NULL;
while(cur) //找到key
{
if(key > cur->_key)//key大,走右子树
{
parent = cur;
cur = cur->_right;
}
else if(key < cur->_key)//key小,走左子树
{
parent = cur;
cur = cur->_left;
}
else //找到key了
{
Node* del = cur;
if(cur->_left == NULL) //如果cur左子树为空,链上右子树
{ //当cur的父亲为空时,证明cur为根,直接将将根节点指向右子树
if(parent == NULL)
{
_root = cur->_right;
}
//不为空时,确定父亲的左右是cur,然后将右树链到确定好的父亲的孩子上
else
{
if(parent->_left == cur)
parent->_left = cur->_right;
else
parent->_right = cur->_right;
}
}
else if(cur->_right == NULL) //如果cur右子树为空,链上左子树
{ //当cur的父亲为空时,证明cur为根,直接将根节点指向左子树
if(parent == NULL)
{
_root = cur->_left;
}
//不为空时,确定父亲的左右是cur,然后将左树链到确定好的父亲的孩子上
else
{
if(parent->_left == cur)
parent->_left = cur->_left;
else
parent->_right = cur->_left;
}
}
else //左右子树都不为空
{
parent = cur;
//找右子树的最左节点
//与cur进行交换,删除右子树最左节点,将右子树最左节点的右孩子链到parent
Node* firstLeft = cur->_right;
//当firstLeft不为空且它的左孩子不为空时,进去,将来while()循环结束时
//firstLeft就是右树的最左节点
while(firstLeft != NULL && firstLeft->_left != NULL)
{
parent = firstLeft;
firstLeft = firstLeft->_left;
}
del = firstLeft;
//交换
swap(firstLeft->_key, cur->_key);
swap(firstLeft->_value, cur->_value);
//链接
if(parent->_left == firstLeft)
parent->_left = firstLeft->_right;
else
parent->_right = firstLeft->_right;
}
//删除
delete del;
del = NULL;
return true;
}
}
return false;
}
}
void InOrder()
{
_InOrder(_root);
cout<<endl;
}
bool Empty()
{
if(_root == NULL)
{
return true;
}
return false;
}
public:
bool Insert_R(const K& key, const V& value)
{
return _Insert_R(_root, key, value);
}
Node* Find_R(const K& key)
{
return _Find_R(_root, key);
}
bool Remove_R(const K& key)
{
return _Remove_R(_root, key);
}
protected:
//传引用参数,root在递归中就相当于上层的root的左孩子或右孩子
bool _Remove_R(Node*& root, const K& key)
{
if(root == NULL)
{
return false;
}
if(key > root->_key)
return _Remove_R(root->_right, key);
else if(key < root->_key)
return _Remove_R(root->_left, key);
else
{
Node* del = root;
if(root->_left == NULL)
{
root = root->_right;
delete del;
del = NULL;
}
else if(root->_right == NULL)
{
root = root->_left;
delete del;
del = NULL;
}
else
{
Node* firstLeft = root->_right;
while(firstLeft != NULL && firstLeft->_left != NULL)
{
firstLeft = firstLeft->_left;
}
swap(firstLeft->_key, root->_key);
swap(firstLeft->_value, root->_value);
_Remove_R(root->_right, key);
}
}
return false;
}
Node* _Find_R(Node* root, const K& key)
{
if(root == NULL)
{
return NULL;
}
else
{
if(key > root->_key)
return _Find_R(root->_right, key);
else if(key < root->_key)
return _Find_R(root->_left, key);
else
return root;
}
}
bool _Insert_R(Node*& root, const K& key, const V& value)
{
if(root == NULL)
{
root = new Node(key, value);
return true;
}
else
{
if(key > root->_key) //key大,走右子树
_Insert_R(root->_right, key, value);
else if(key < root->_key) //key小,走左子树
_Insert_R(root->_left, key, value);
else
return false;
}
}
void _InOrder(Node* root)
{
if(root == NULL)
return;
_InOrder(root->_left);
cout<<root->_key<<" ";
_InOrder(root->_right);
}
private:
Node* _root;
};
void Test()
{
BSTree<int, int> bst;
bst.Insert(3,1);
bst.Insert(4,1);
bst.Insert(5,1);
bst.Insert(2,1);
bst.Insert(1,1);
bst.Insert(0,1);
bst.Insert(6,1);
bst.Insert(7,1);
bst.Insert(8,1);
bst.Insert(9,1);
cout<<bst.Empty()<<endl;
bst.InOrder();
bst.Remove(8);
bst.InOrder();
bst.Remove(7);
bst.InOrder();
bst.Remove(6);
bst.InOrder();
bst.Remove(5);
bst.InOrder();
bst.Remove(4);
bst.InOrder();
bst.Remove(3);//删除根
bst.InOrder();
bst.Remove(2);
bst.InOrder();
bst.Remove(1);
bst.InOrder();
bst.Remove(0);
bst.InOrder();
bst.Remove(9);
bst.InOrder();
cout<<bst.Empty()<<endl;
}
void Test_R()
{
BSTree<int, int> bst;
bst.Insert_R(3,1);
bst.Insert_R(4,1);
bst.Insert_R(5,1);
bst.Insert_R(2,1);
bst.Insert_R(1,1);
bst.Insert_R(0,1);
bst.Insert_R(6,1);
bst.Insert_R(7,1);
bst.Insert_R(8,1);
bst.Insert_R(9,1);
bst.InOrder();
cout<<bst.Empty()<<endl;
bst.Remove_R(8);
bst.InOrder();
bst.Remove_R(7);
bst.InOrder();
bst.Remove_R(6);
bst.InOrder();
bst.Remove_R(5);
bst.InOrder();
bst.Remove_R(4);
bst.InOrder();
bst.Remove_R(3);//删除根
bst.InOrder();
bst.Remove_R(2);
bst.InOrder();
bst.Remove_R(1);
bst.InOrder();
bst.Remove_R(0);
bst.InOrder();
cout<<"Find_R:9?"<<bst.Find_R(9)->_key<<endl;
bst.Remove_R(9);
bst.InOrder();
cout<<bst.Empty()<<endl;
cout<<bst.Find_R(9)<<endl;
}
/*BSTree.cpp*/
#pragma once
#include "BSTree.h"
int main()
{
//Test();<span style="white-space:pre"> </span>//测试非递归
Test_R();<span style="white-space:pre"> </span>//测试递归
system("pause");
return 0;
}
测试非递归结果:
测试递归结果:
可以从二叉搜索树的中序遍历中可以看出他们都是有序的,因而二叉搜索树又被称为排序树。