一、二叉搜索树概念
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树
不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右
子树也分别为二叉排序树。
搜索,插入,删除的复杂度等于树高,O(log(n)).
二、操作
(1)插入
向一个二叉排序树b中插入一个结点s的算法,过程为:
若b是空树,则将s所指结点作为根结点插入,否则:
若s->data等于b的根结点的数据域之值,则返回,否则:
若s->data小于b的根结点的数据域之值,则把s所指结点插入到左子树中,否则:
把s所指结点插入到右子树中
(2)删除
(3)查找
在二叉排序树b中查找x的过程为:
若b是空树,则搜索失败,否则:
若x等于b的根结点的数据域之值,则查找成功;否则:
若x小于b的根结点的数据域之值,则搜索左子树;否则:
三、实现如下:
#pragma once
#include<iostream>
using namespace std;
template<class K,class V>
struct BSTreeNode
{
BSTreeNode(K key,V value)
:_key(key)
,_value(value)
,_left(NULL)
,_right(NULL)
,_parent(NULL)
{}
BSTreeNode<K,V>* _left;
BSTreeNode<K,V>* _right;
BSTreeNode<K,V>* _parent;
K _key;
V _value;
};
template<class K,class V>
class BSTree
{
public:
typedef BSTreeNode<K,V> Node;
BSTree()
:_root(NULL)
{}
/*bool Insert(K key,V value)
{
if(_root==NULL)
{
_root=new Node(key,value);
return true;
}
Node* cur=_root;
Node* parent=NULL;
while(cur)
{
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->_left=new Node(key,value);
parent->_left->_parent=parent;
}
else if(parent->_key<key)
{
parent->_right=new Node(key,value);
parent->_right->_parent=parent;
}
else
{
return false;
}
return true;
}*/
//void InsertR(K key,V value) //递归插入
//{
// Node* parent=NULL;
// _root=_InsertR(_root,parent,key,value);
//}
bool InsertR(K key,V value) //递归插入
{
Node* parent=NULL;
return _InsertR(_root,parent,key,value);
}
/*Node* Find(K key)
{
if(_root==NULL)
{
return false;
}
Node* cur=_root;
while(cur)
{
if(cur->_key>key)
{
cur=cur->_left;
}
else if(cur->_key<key)
{
cur=cur->_right;
}
else
{
return cur;
}
}
return false;
}
bool Remove(Node* cur)
{
if(cur==NULL)
{
return false;
}
Node* parent=cur->_parent;
Node* del;
if(cur->_left==NULL)
{
if(parent==NULL)
{
_root=cur->_right;
}
else
{
if(parent->_left==cur)
{
parent->_left=cur->_right;
}
else
{
parent->_right=cur->_right;
}
}
if(cur->_right)
cur->_right->_parent=parent;
del=cur;
}
else if(cur->_right==NULL)
{
if(parent==NULL)
{
_root=cur->_left;
}
else
{
if(parent->_left==cur)
{
parent->_left=cur->_left;
}
else
{
parent->_right=cur->_left;
}
}
if(cur->_left)
{
cur->_left->_parent=parent;
}
del=cur;
}
else
{
parent=cur;
Node* firstLeft=cur->_right;
while(firstLeft->_left)
{
parent=firstLeft;
firstLeft=firstLeft->_left;
}
del=firstLeft;
cur->_key=firstLeft->_key;
cur->_value=firstLeft->_value;
if(parent->_left==firstLeft)
{
parent->_left=firstLeft->_right;
}
else
{
parent->_right=firstLeft->_right;
}
if(firstLeft->_right)
{
firstLeft->_right->_parent=parent;
}
}
delete del;
del=NULL;
return true;
}*/
Node* Find(K key) //递归实现
{
return _Find(_root, key);
}
bool RemoveR(K key) //递归实现
{
return _RemoveR(_root,key);
}
void InOrder()
{
_InOrder(_root);
}
private:
//Node* _InsertR(Node* root,Node* parent ,K key,V value) //引用的作用
//{
// if(root==NULL)
// {
// root=new Node(key,value);
// root->_parent=parent;
// return root;
// }
// if(root->_key>key)
// {
// root->_left=_InsertR(root->_left,root,key,value);
// return root;
// }
// else if(root->_key<key)
// {
// root->_right=_InsertR(root->_right,root,key,value);
// return root;
// }
// else
// {
// return false;
// }
//}
bool _InsertR(Node* &root,Node* parent,K key,V value)
{
if(root==NULL)
{
root=new Node(key,value);
root->_parent=parent;
return true;
}
if(root->_key>key)
{
return _InsertR(root->_left,root,key,value);
}
else if(root->_key<key)
{
return _InsertR(root->_right,root,key,value);
}
}
Node* _Find(Node* root,K key)
{
if(root==NULL)
{
return NULL;
}
if(root->_key>key)
{
return _Find(root->_left,key);
}
else if(root->_key<key)
{
return _Find(root->_right,key);
}
else
{
return root;
}
}
bool _RemoveR(Node*& root,K key) //引用
{
if(root==NULL)
{
return false;
}
if(root->_key>key)
{
return _RemoveR(root->_left,key);
}
else if(root->_key<key)
{
return _RemoveR(root->_right,key);
}
else
{
Node* del=root;
Node* parent=root->_parent;
if(root->_left==NULL)
{
root=root->_right;
if(root)
{
root->_parent=parent;
}
}
else if(root->_right==NULL)
{
root=root->_left;
if(root)
{
root->_parent=parent;
}
}
else
{
Node* firstLeft=root->_right;
while(firstLeft->_left)
{
firstLeft=firstLeft->_left;
}
swap(root->_key,firstLeft->_key);
swap(root->_value,firstLeft->_value);
return _RemoveR(root->_right,key);
}
delete del;
del=NULL;
}
return true;
}
void _InOrder(Node* root)
{
if(root==NULL)
{
return;
}
_InOrder(root->_left);
cout<<root->_key<<"->";
_InOrder(root->_right);
}
private:
Node* _root;
};
#include "SeaTree.h"
int main()
{
int a[]={5,3,4,1,7,8,2,6,0,9};
BSTree<int,int> t1;
for(int i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
t1.InsertR(a[i],i);
}
t1.InOrder();
cout<<endl;
BSTreeNode<int,int>* ret=t1.Find(7);
cout<<ret->_key<<":"<<ret->_value<<endl;
/*t1.Remove(t1.Find(0));
t1.Remove(t1.Find(1));
t1.Remove(t1.Find(2));
t1.Remove(t1.Find(3));
t1.Remove(t1.Find(4));
t1.Remove(t1.Find(5));
t1.Remove(t1.Find(6));
t1.Remove(t1.Find(7));
t1.Remove(t1.Find(8));
t1.Remove(t1.Find(9));*/
//t1.RemoveR(0);
t1.RemoveR(1);
//t1.RemoveR(2);
t1.RemoveR(3);
t1.RemoveR(4);
t1.RemoveR(5);
t1.RemoveR(6);
t1.RemoveR(7);
t1.RemoveR(8);
t1.RemoveR(9);
t1.InOrder();
cout<<endl;
system("pause");
return 0;
}
四、结果: