二叉搜索树

来源:互联网 发布:电商淘宝是做什么的 编辑:程序博客网 时间:2024/04/30 07:09

#ifndef BST_H

#define BST_H

 

#include <iostream>

using namespace std;

 

template<class K,class T>

struct BSTNode     //二叉搜索树结点类

{

     K key;   //关键码域

     T data;  //数据域

     BSTNode<K,T> *left,*right;  //左子女和右子女

     BSTNode():left(NULL),right(NULL){}

     BSTNode(const K k,const T d,BSTNode<K,T>* L=NULL,BSTNode<K,T>* R=NULL)

         :key(k),data(d),left(L),right(R){}

     bool operator<(const BSTNode<K,T>& bn)

     {

         return (key<bn.key) ? true : false;

     }

     bool operator>(const BSTNode<K,T>& bn)

     {

         return (key>bn.key) ? true : false;

     }

     bool operator==(const BSTNode<K,T>& bn)

     {

         return (key == bn.key) ? true : false;

     }

     template<class K,class T>

     friend istream& operator>>(istream& in,BSTNode<K,T>& bn);

     template<class K,class T>

     friend ostream& operator<<(ostream& out,BSTNode<K,T>& bn);

};

 

template<class K,class T>

class BST //二叉搜索树类定义

{

public:

     BST(const char val='#'):root(NULL),RefValue(val)

     {

         cout<<"请依次输入数据:"<<endl;

         char ch;

         while (1)

         {

              cin.get(ch);

              if (ch == RefValue)

                   break;

              cin.putback(ch);

              BSTNode<K,T> temp;

              cin>>temp.key;

              cin>>temp.data;

              Insert(temp);

         }

     }

     ~BST()

     {

         MakeEmpty();

     }

     bool Search(const K x)const //搜索

     {

         return (Search(x,root) != NULL ? true : false)

     }

     BST<K,T>& operator=(const BST<K,T>& R);   //赋值

     void MakeEmpty()   //置空

     {

         MakeEmpty(root);

         root = NULL;

     }

     void PrintTree()const  //输出

     {

         PrintTree(root);

     }

     T Min()  //求最小

     {

         return Min(root)->data;

     }

     T Max()  //求最大

     {

         return Max(root)->data;

     }

     bool Insert(const BSTNode<K,T>& bn)  //插入新元素

     {

         return Insert(bn,root);

     }

     bool Remove(const K x) //删除

     {

         return Remove(x,root);

     }

private:

     BSTNode<K,T> *root;    //二叉搜索树的根指针

     char RefValue;     //停止输入标志,用于输入

     BSTNode<K,T>* Search(const K x,BSTNode<K,T> *ptr); //递归:搜索

     void MakeEmpty(BSTNode<K,T> *&ptr);  //递归:置空

     void PrintTree(BSTNode<K,T> *ptr)const;   //递归:输出

     BSTNode<K,T>* Copy(const BSTNode<K,T> *ptr);   //递归:复制

     BSTNode<K,T>* Min(BSTNode<K,T> *ptr)const;     //递归:求最小

     BSTNode<K,T>* Max(BSTNode<K,T> *ptr)const;     //递归:求最大

     bool Insert(const BSTNode<K,T>& bn,BSTNode<K,T> *&ptr); //递归:插入

     bool Remove(const K x,BSTNode<K,T> *&ptr);     //递归:删除

};

 

template <class K,class T>

ostream &operator <<(ostream &out,BSTNode<K,T> &bn)

{

     cout<<"key:"<<bn.key<<" "<<"data:"<<bn.data<<endl; 

     return out;

}

 

template <class T,class K>

istream &operator >>(istream &in,BSTNode<K,T> &bn)

{

     cout<<"key:";

     in>>bn.key;

     cout<<"data:";

     in>>bn.data;

     return in;

}

 

//在以ptr为根的二叉搜索树中搜索含有x的结点。若找到,则函数返回该结点的地址,否则函数返回NULL

template<class K,class T>

BSTNode<K,T>* BST<K,T>::Search(const K x, BSTNode<K,T> *ptr)

{

     if(ptr == NULL)

         return NULL;

     else if(x < ptr->key)  //到左子树中继续搜索

         return Search(x,ptr->left);

     else if(x > ptr->key)  //到右子树中继续搜索

         return Search(x,ptr->right);

     else

         return ptr;

}

 

//在以ptr为根的二叉搜索树中插入所含值为bn的结点。若在树中已经有含bn的结点,则不插入

template<class K,class T>

bool BST<K,T>::Insert(const BSTNode<K,T> &bn, BSTNode<K,T> *&ptr)

{

     if (ptr == NULL)   //新结点作为根结点插入

     {

         ptr = new BSTNode<K,T>(bn);

         if (ptr == NULL)

         {

              cerr<<"Out of space!"<<endl;

              exit(1);

         }

         return true;

     }

     else if(bn.key < ptr->key)  //左子树插入

         Insert(bn,ptr->left);

     else if(bn.key > ptr->key)  //右子树插入

         Insert(bn,ptr->right);

     else

         return false; //x已在树中,不再插入

}

 

//在以ptr为根结点的二叉搜索树中删除含x

template<class K,class T>

bool BST<K,T>::Remove(const K x, BSTNode<K,T> *&ptr)

{

     BSTNode<K,T> *temp;

     if (ptr != NULL)

     {

         if(x < ptr->key)

              Remove(x,ptr->left);   //在左子树中执行删除

         else if(x > ptr->key)

              Remove(x,ptr->right);  //在右子树中执行删除

         else if(ptr->left != NULL && ptr->right != NULL)   //ptr指示关键码为x的结点,它有两个子女

         {

              temp = ptr->right; //到右子树中搜寻中序下的第一个结点

              while (temp->left != NULL)

                   temp = temp->left;

              ptr->key = temp->key;  //用该结点数据代替根结点数据

              ptr->data = temp->data;

              Remove(ptr->key,ptr->right);

         }

         else //ptr指示关键码为x的结点,它只有一个或零个子女

         {

              temp = ptr;

              if(ptr->left == NULL)

                   ptr = ptr->right;

              else

                   ptr = ptr->left;

              delete temp;

              return true;

         }

     }

     return false;

}

 

template<class K,class T>

BST<K,T>& BST<K,T>::operator=(const BST<K,T>& R)

{

     root = Copy(R.root);

     return *this;

}

 

template<class K,class T>

BSTNode<K,T>* BST<K,T>::Copy(const BSTNode<K,T> *ptr)

{

     if(ptr == NULL)    //根为空,返回空指针

         return NULL;

     BSTNode<K,T> *temp = new BSTNode<K,T>;    //创建根结点

     temp->key = ptr->key;

     temp->data = ptr->data;

     temp->leftChild = Copy(ptr->leftChild);

     temp->rightChild = Copy(ptr->rightChild);

     return temp;

}

 

template<class K,class T>

void BST<K,T>::MakeEmpty(BSTNode<K,T> *&ptr)

{

     if(ptr!=NULL)

     {

         MakeEmpty(ptr->left);

         MakeEmpty(ptr->right);

         delete ptr;

     }

}

 

template<class K,class T>

void BST<K,T>::PrintTree(BSTNode<K,T> *ptr)const

{

     if(ptr == NULL)

         return;

     PrintTree(ptr->left);

     cout<< (*ptr);

     PrintTree(ptr->right);

}

 

template<class K,class T>

BSTNode<K,T>* BST<K,T>::Min(BSTNode<K,T> *ptr)const

{

     if(ptr == NULL)

         return NULL;

     BSTNode<K,T> *temp = ptr;

     while(temp->left != NULL)

         temp = temp->left;

     return temp;

}

 

template<class K,class T>

BSTNode<K,T>* BST<K,T>::Max(BSTNode<K,T> *ptr)const

{

     if(ptr == NULL)

         return NULL;

     BSTNode<K,T> *temp = ptr;

     while(temp->right != NULL)

         temp = temp->right;

     return temp;

}

 

#endif

原创粉丝点击