二叉查找数的C++实现
来源:互联网 发布:云计算安全防护 编辑:程序博客网 时间:2024/05/11 00:14
///////////////头文件定义
#include <iostream>
using namespace std;
template <class Type>
class BinarySearchTree
{
public:
BinarySearchTree( );
BinarySearchTree (const BinarySearchTree<Type> & rhs);
~BinarySearchTree();
const Type & findMin () const;
const Type & findMax () const;
bool contains ( const Type & x) const;
bool isEmpty () const;
void printTree () const;
void makeEmpty ();
void insert ( const Type & x);
void remove (const Type & x);
const BinarySearchTree<Type> & operator=( const BinarySearchTree<Type>& rhs);
private:
struct BinaryNode
{
Type element;
BinaryNode * left;
BinaryNode * right;
BinaryNode( const Type & theElement,BinaryNode *lt, BinaryNode *rt)
:element ( theElement ),left (lt), right( rt ){}
};
BinaryNode *root;
void insert ( const Type & x,BinaryNode * & t ) const;
void remove (const Type & x,BinaryNode * & t)const;
BinaryNode * findMin( BinaryNode *t) const;
BinaryNode * findMax( BinaryNode *t) const;
bool contains( const Type & x, BinaryNode *t ) const;
void makeEmpty( BinaryNode * & t) ;
void printTree( BinaryNode *t ) const;
BinaryNode* clone( BinaryNode *t )const;
};
/////////////////////////函数实现
#include <string>
#include <cstring>
template <class Type>
BinarySearchTree<Type>::BinarySearchTree()
{
root=NULL;
}
template <class Type>
BinarySearchTree<Type>::BinarySearchTree(const BinarySearchTree<Type>& rhs)
{
root=clone(rhs.root);
}
template <class Type>
BinarySearchTree<Type>::~BinarySearchTree()
{
makeEmpty();
}
template <class Type>
bool BinarySearchTree<Type>::isEmpty( ) const
{
return root==NULL;
}
template <class Type>
const BinarySearchTree<Type> & BinarySearchTree<Type>:: operator=( const BinarySearchTree<Type>& rhs)
{
if(this==&rhs)
return *this;
makeEmpty();
root=rhs.clone(root);
return *this;
}
template <class Type>
const Type & BinarySearchTree<Type>::findMax() const
{
return findMax(root)->element;
}
template <class Type>
typename BinarySearchTree<Type>::BinaryNode*
BinarySearchTree<Type>::findMax(BinaryNode *t) const
{
if(t!=NULL)
while(t->right!=NULL)
t=t->right;
return t;
}
template <class Type>
const Type & BinarySearchTree<Type>::findMin() const
{
return findMin(root)->element;
}
template <class Type>
typename BinarySearchTree<Type>::BinaryNode*
BinarySearchTree<Type>::findMin(BinaryNode *t) const
{
if(t!=NULL)
while(t->left!=NULL)
t=t->left;
return t;
}
template <class Type>
bool BinarySearchTree<Type>::contains(const Type & x) const
{
return contains(x,root);
}
template <class Type>
bool BinarySearchTree<Type>::contains(const Type & x, BinaryNode* t) const
{
if (t==NULL)
return false;
else if(x<t->element)
return contains(x,t->left);
else if(x>t->element)
return contains(x,t->right);
else
return true;
}
template <class Type>
void BinarySearchTree<Type>::printTree() const
{
if(isEmpty())
cout<<"Empty Tree!"<<endl;
else
printTree(root);
}
template <class Type>
void BinarySearchTree<Type>::printTree(BinaryNode *t) const
{
if(t!=NULL)
{
printTree(t->left);
cout<<t->element<<" ";
printTree(t->right);
}
}
template <class Type>
void BinarySearchTree<Type>::insert(const Type &x)
{
insert(x,root);
}
template <class Type>
void BinarySearchTree<Type>::insert(const Type &x, BinaryNode *&t) const
{
if(t==NULL)
t=new BinaryNode(x,NULL,NULL);
else if (x<t->element)
return insert(x,t->left);
else if (x>t->element)
return insert(x,t->right);
else
return ;
}
template <class Type>
void BinarySearchTree<Type>::remove(const Type &x)
{
remove(x,root);
}
template <class Type>
void BinarySearchTree<Type>::remove(const Type &x, BinaryNode *&t) const
{
if(t==NULL)
return;
else if(x<t->element)
return remove(x,t->left);
else if(x>t->element)
return remove(x,t->right);
else if(t->left!=NULL &&t->right!=NULL)
{
t->element=findMin(t->right)->element;
remove(t->element,t->right);
}
else
{
BinaryNode *oldNode=t;
t=(t->left!=NULL) ?t->left : t->right;
delete oldNode;
}
}
template <class Type>
void BinarySearchTree<Type>::makeEmpty()
{
makeEmpty(root);
}
template <class Type>
void BinarySearchTree<Type>::makeEmpty(BinaryNode *&t)
{
if(t!=NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
template <class Type>
typename BinarySearchTree<Type>::BinaryNode*
BinarySearchTree<Type>::clone(BinaryNode *t) const
{
if(t==NULL)
return NULL;
return new BinaryNode(t->element,clone(t->left),clone(t->right));
}
//////////////////////////////////////////测试函数实现
#include "stdafx.h"
#include "stdafx.cpp"
int _tmain(int argc, _TCHAR* argv[])
{
BinarySearchTree<int>coll;
coll.insert(4);
coll.insert(9);
coll.insert(0);
coll.insert (1);
coll.insert(5);
coll.insert(8);
coll.insert(4);
cout<<"coll= ";
coll.printTree();
cout<<endl;
BinarySearchTree<int>tree(coll);
cout<<"tree= ";
tree.printTree();
cout<<endl;
cout<<"coll remove element."<<endl;
coll.remove(9);
coll.remove(4);
coll.remove(2);
cout<<"After remove element."<<endl;
cout<<" coll=";
coll.printTree();
cout<<endl;
cout<<"tree=coll"<<endl;
tree=coll;
cout<<"tree= "<<endl;
tree.printTree();
cout<<endl;
cout<<"clear tree "<<endl;
tree.makeEmpty();
if(tree.isEmpty())
cout<<"tree is Empty"<<endl;
cout<<"Max="<<coll.findMax()<<endl;
cout<<"Min="<<coll.findMin()<<endl;
if(coll.contains(2))
cout<<"find element 2"<<endl;
else
cout<<"Not find element 2."<<endl;
if(coll.contains(5))
cout<<"find element 5"<<endl;
else
cout<<"not find element 5"<<endl;
system("PAUSE");
return 0;
}
- 二叉查找数的实现(算法导论)
- 二叉查找数的C++实现
- 二叉查找树的实现(using C)
- 二叉查找树的C语言实现
- 二叉查找树的实现(c++)
- 二叉查找树的C语言实现
- 二叉查找树的实现(C++)
- 【二叉查找树】c实现
- 数据结构:二叉数查找树基本实现
- 二叉查找树(二叉排序树)的详细实现C版
- 基于c语言实现的二叉查找树
- 二叉查找树的C语言实现(一)
- 二叉查找树的C语言实现(二)
- 二叉查找树(一)之 C语言的实现
- 二叉查找树的基本实现-c语言
- 二叉查找树的简单实现(C语言版)
- 数据结构——二叉查找树的详细实现(c++)
- 详解二叉查找树算法的实现(c语言)
- 《深入理解Linux内核》读书笔记:内存管理
- 一个Linux下自动部署Web服务的脚本
- Mysql命令行导入sql数据
- VB使用WebBrowser控件POST数据
- 四:创建型模式:建造者模式
- 二叉查找数的C++实现
- Drupal钩子实现函数发现机制
- Unable to add window -- token null is not for an application
- ava中定时JOB的实现方式
- 安装kernel API的man手册
- Drupal 7中 page.tpl.php 的可用变量
- Cocos2d-x之LUA脚本引擎深入分析
- 站长做广告联盟和包月广告需要注意些什么?
- Drupal中block直接代码输出