功能实现已经介绍过了详见:http://blog.csdn.net/fangfang_666/article/details/52904520
源代码:
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class K>
struct BinarySearchNode
{
BinarySearchNode * _left;
BinarySearchNode * _right;
K _key;
BinarySearchNode(const K& key)
:_left(NULL)
,_right(NULL)
,_key(key)
{}
};
template<class K>
class BinarySearhTree
{
typedef BinarySearchNode<K> Node;
public:
BinarySearhTree()
:_root(NULL)
{}
BinarySearhTree(const BinarySearhTree<K> & t)
:_root(t._root)
{}
~BinarySearhTree()
{
delete _root;
}
bool Insert(const K& key)
{
if(_root == NULL)
{
_root = new Node(key);
return true;
}
Node* cur = _root;
Node* parent = NULL;
//找可插入节点的位置
while(cur)
{
if(cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if(cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
Node* tmp = new Node(key);
if(parent->_key>key)
{
parent->_left=tmp;
}
else
{
parent->_right = tmp;
}
return false;
}
bool InsertR(const K& key)//递归
{
_InsertR(_root,key);
return true;
}
bool Find(const 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 false;
}
return true;
}
bool Remove(const K & key)//
{
_Remove(_root,key);
return true;
}
bool RemoveR(const K &key)
{
_RemoveR(_root,key);
return true;
}
void InOrder()//中序遍历
{
_InOrder(_root);
cout<<endl;
}
Node * FindNode(Node *root,const K & key)
{
if(root == NULL)
{
return NULL;
}
if(root->_key == key)
{
return root;
}
else if(root->_key > key)
{
return FindNode(root->_left,key);
}
else
{
return FindNode(root->_right,key);
}
}
protected:
bool _InsertR(Node *&root,const K & key)
{
if(root == NULL)
{
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);
}
else
{
return false;
}
}
bool _RemoveR(Node *&root,const 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;
if(root->_left == NULL)//待删除节点左树为空
{
root = root->_right;
}
else if(root->_right == NULL)//待删除节点右树为空
{
root = root->_left;
}
delete del;
}
return true;
}
bool _Remove(Node* root,const K & key)//非递归
{
if(root == NULL)
{
return false;
}
Node * cur = root;
Node * parent = NULL;
Node * del = cur;
while(cur&&cur->_key!=key)//找待删除节点
{
if(cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if(cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
}
//待删除节点已找到
if(cur->_left == NULL)//待删除节点左树为空
{
if(cur == root)
{
root = cur->_right;
}
else if(cur == parent->_left)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
del = cur;
}
else if(cur->_right == NULL)//待删除节点右树为空
{
if(cur == root)
{
root = cur->_left;
}
else if(cur == parent->_left)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
del = cur;
}
else//待删除节点左右均不为空
{
Node* minRight = cur->_right;//右子树的最左节点
parent = cur;
while(minRight->_left)
{
parent = minRight;
minRight = minRight->_left;
}
del = minRight;
cur->_key = minRight->_key;//交换对应节点的值
if(parent->_left==minRight)
{
parent->_left = minRight->_right;
}
else
{
parent->_right = minRight->_right;
}
}
delete del;
return true;
}
void _InOrder(Node *root)
{
Node *cur = root;
if(cur == NULL)
{
return;
}
_InOrder(cur->_left);
cout<<cur->_key<<" ";
_InOrder(cur->_right);
}
private:
Node *_root;
};
void Test()
{
BinarySearhTree<int> tree;
/*
BinarySearhTree<int>*tree = new BinarySearhTree<int> ();
int arr[] = {1,2,3,4,5,6,7,8,9,};
size_t size = sizeof(arr)/sizeof(arr[0]);
cout<<"依次添加:";
for(size_t i=0;i<size;i++)
{
cout<<arr[i]<<" ";
tree->InsertR(arr[i]);
}
tree.InOrder();*/
tree.InsertR(5);
tree.InsertR(3);
tree.InsertR(4);
tree.InsertR(1);
tree.InsertR(7);
tree.InsertR(8);
tree.InsertR(2);
tree.InsertR(6);
tree.InsertR(0);
tree.InsertR(9);
tree.InOrder();
int ret=tree.Find(9);
tree.Remove(9);
tree.InOrder();
}
int main()
{
Test();
system("pause");
return 0;
}