c++实现搜索二叉树
2016-10-18 18:15
218 查看
#include<iostream>
#include<cstdlib>
#include<cassert>
using namespace std;
template<class T>
struct BinaryTreeNode
{
T _key;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;
BinaryTreeNode(const T& x)
:_key(x)
,left(NULL)
,right(NULL)
{}
};
template<class T>
class SearchBinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
SearchBinaryTree()
:_root(NULL)
{}
SearchBinaryTree(const SearchBinaryTree& t)
:_root(t._root)
{}
~ SearchBinaryTree()
{
delete _root;
}
bool Insert(T t )
{
Node* key=new Node(t);
if(_root==NULL)
_root=key;
else
{
Node* cur=_root;
Node* prev=NULL;
while(cur!=NULL)
{
if(cur->_key>key->_key)
{
prev=cur;
cur=cur->left;
}
else if(cur->_key<key->_key)
{
prev=cur;
cur=cur->right;
}
}
if(prev->_key>key->_key)
{
prev->left=key;
}
else
prev->right=key;
}
return false;
}
void Remove(const T t)
{
if(_root==NULL)
return ;
int ret= Find(t);
Node* cur=_root;
Node* prev=NULL;
if(ret==true)
{
while(cur->_key!=t&&cur)
{
if(cur->_key<t)
{
prev=cur;
cur=cur->right;
}
else if(cur->_key>t)
{
prev=cur;
cur=cur->left;
}
else
break;
}
if(cur->left==NULL&&cur->right==NULL)
{
delete cur;
}
else if(cur->left==NULL)
{
if(cur->right->_key<prev->right->_key)
{
prev->left=cur->right;
delete cur;
}
else
{
prev->right=cur->right;
}
}
else if(cur->right==NULL)
{
prev->left=cur->left;
delete cur;
}
else
{
Node* p=cur;
cur=cur->right;
while(cur->left)
{
prev=cur;
cur=cur->left;
}
p->_key=cur->_key;
delete cur;
}
}
else
cout<<"false";
}
bool Find(const T t)
{
if(_root==NULL)
return false;
Node* cur=_root;
while(cur)
{
if(cur->_key<t)
cur=cur->right;
else if(cur->_key>t)
cur=cur->left;
else
return true;
}
}
void InOrder()
{
_InOrder(_root);
cout<<endl;
}
protected:
void _InOrder(Node* root) //中序遍历
{
Node* cur=root;
if(cur==NULL)
return;
_InOrder(cur->left);
cout<<cur->_key<<" ";
_InOrder(cur->right);
}
private:
Node* _root;
};
测试
#include"SearchBinaryTree.h"
int main()
{
SearchBinaryTree<int> tree;
int ret=0;
tree.Insert(5);
tree.Insert(4);
tree.Insert(1);
tree.Insert(7);
tree.Insert(6);
tree.Insert(3);
tree.Insert(0);
ret=tree.Find(7);
tree.Remove(4);
tree.InOrder();
if(ret==true)
cout<<"find"<<endl;
else
cout<<"not find"<<endl;
system("pause");
return 0;
}
#include<cstdlib>
#include<cassert>
using namespace std;
template<class T>
struct BinaryTreeNode
{
T _key;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;
BinaryTreeNode(const T& x)
:_key(x)
,left(NULL)
,right(NULL)
{}
};
template<class T>
class SearchBinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
SearchBinaryTree()
:_root(NULL)
{}
SearchBinaryTree(const SearchBinaryTree& t)
:_root(t._root)
{}
~ SearchBinaryTree()
{
delete _root;
}
bool Insert(T t )
{
Node* key=new Node(t);
if(_root==NULL)
_root=key;
else
{
Node* cur=_root;
Node* prev=NULL;
while(cur!=NULL)
{
if(cur->_key>key->_key)
{
prev=cur;
cur=cur->left;
}
else if(cur->_key<key->_key)
{
prev=cur;
cur=cur->right;
}
}
if(prev->_key>key->_key)
{
prev->left=key;
}
else
prev->right=key;
}
return false;
}
void Remove(const T t)
{
if(_root==NULL)
return ;
int ret= Find(t);
Node* cur=_root;
Node* prev=NULL;
if(ret==true)
{
while(cur->_key!=t&&cur)
{
if(cur->_key<t)
{
prev=cur;
cur=cur->right;
}
else if(cur->_key>t)
{
prev=cur;
cur=cur->left;
}
else
break;
}
if(cur->left==NULL&&cur->right==NULL)
{
delete cur;
}
else if(cur->left==NULL)
{
if(cur->right->_key<prev->right->_key)
{
prev->left=cur->right;
delete cur;
}
else
{
prev->right=cur->right;
}
}
else if(cur->right==NULL)
{
prev->left=cur->left;
delete cur;
}
else
{
Node* p=cur;
cur=cur->right;
while(cur->left)
{
prev=cur;
cur=cur->left;
}
p->_key=cur->_key;
delete cur;
}
}
else
cout<<"false";
}
bool Find(const T t)
{
if(_root==NULL)
return false;
Node* cur=_root;
while(cur)
{
if(cur->_key<t)
cur=cur->right;
else if(cur->_key>t)
cur=cur->left;
else
return true;
}
}
void InOrder()
{
_InOrder(_root);
cout<<endl;
}
protected:
void _InOrder(Node* root) //中序遍历
{
Node* cur=root;
if(cur==NULL)
return;
_InOrder(cur->left);
cout<<cur->_key<<" ";
_InOrder(cur->right);
}
private:
Node* _root;
};
测试
#include"SearchBinaryTree.h"
int main()
{
SearchBinaryTree<int> tree;
int ret=0;
tree.Insert(5);
tree.Insert(4);
tree.Insert(1);
tree.Insert(7);
tree.Insert(6);
tree.Insert(3);
tree.Insert(0);
ret=tree.Find(7);
tree.Remove(4);
tree.InOrder();
if(ret==true)
cout<<"find"<<endl;
else
cout<<"not find"<<endl;
system("pause");
return 0;
}
相关文章推荐
- 搜索二叉树 C++的实现
- 算法导论第12章-搜索二叉树伪代码的C++程序全实现
- 《数据结构与算法分析C++描述》 搜索二叉树的C++实现
- C++实现搜索二叉树
- c++实现搜索二叉树
- C++实现搜索二叉树
- 二叉树(c++实现)
- C++链表实现二叉树
- 使用C++实现简单二叉树
- C++非递归队列实现二叉树的广度优先遍历
- C++实现禁忌搜索解决TSP问题
- (七)数据结构之搜索二叉树的简单实现
- 二叉树的C++实现
- C++实现二叉树相关操作
- C++实现——二叉树的四种遍历(非递归写法)
- C++实现二叉树及其线索化和遍历
- 二叉树的链表表示 C++实现
- C++实现二叉树,运用模板,界面友好,操作方便 运行流畅
- 二叉树以及 二叉树的C++实现
- c++实现二叉树(递归)