二叉查找数的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;
}

 

原创粉丝点击