二叉搜索树

来源:互联网 发布:w判断矩阵 编辑:程序博客网 时间:2024/05/17 04:26
#include<iostream>#include<stdlib.h>using namespace std;template<class E>struct BSTNode{                                       //二叉树结点类      E data;                                         //数据域   BSTNode<E> *left,*right;                           BSTNode():left(NULL),right(NULL){}                 BSTNode(const E d,BSTNode<E> *L=NULL,BSTNode<E> *R=NULL):data(d),left(L),right(R){}   ~BSTNode(){}};template<class E>class BST{                                               //类定义public: BST():root(NULL){} void Build(E value);                  //建树 bool Search(const E x)                    //搜索 {  return (Search(x,root)!=NULL)?true:false; }    BST<E>& operator =(const BST<E>& R);         //赋值 void makeEmpty(){makeEmpty(root);root=NULL;}     //置空    void PrintTree(){PrintTree(root);}          //输出 E Min(){return Min(root)->data;}                 //求最小 E Max(){return Max(root)->data;}                 //求最大 bool Insert(const E& el){return Insert(el,root);} //插入新元素 bool Remove(const E x){return Remove(x,root);}    //删除含x的结点 BSTNode<E>*Getroot(){return root;}//获取根节点private: BSTNode<E> *root; char RefValue; BSTNode<E> *Search(const E x,BSTNode<E> *ptr); void makeEmpty(BSTNode<E> *& ptr); void PrintTree(BSTNode<E> *ptr); BSTNode<E> *Copy(const BSTNode<E> *ptr); BSTNode<E> *Min(BSTNode<E> *ptr)const; BSTNode<E> *Max(BSTNode<E> *ptr)const; bool Insert(const E el,BSTNode<E> *& ptr); bool Remove(const E x,BSTNode<E> *& ptr);};template<class E>BSTNode<E> *BST<E>::Search(const E x,BSTNode<E> *ptr){//递归搜索算法    if(ptr==NULL)  return NULL; else  if(x<ptr->data)   return Search(x,ptr->left);    else if(x>ptr->data)  return Search(x,ptr->right); else  return ptr;};template<class E>bool BST<E>::Insert(const E el,BSTNode<E> *& ptr){    if(ptr==NULL) {     ptr=new BSTNode<E>(el);     if(ptr==NULL)  {      cerr<<"Out of space"<<endl;   exit(1);  }  return true; }    else  if(el<ptr->data)   Insert(el,ptr->left);  else   if(el>ptr->data)    Insert(el,ptr->right);   else    return false;};template<class E>void BST<E>::Build(E value){//输入一个元素序列,建立一棵二叉搜索树       E x;    int i=1;    root=NULL;    RefValue=value;    cout<<"请输入第1个元素(0为结束符):"<<endl;    cin>>x;       while(x!=RefValue)    {     cout<<"请输入第"<<i+1<<"个元素(0为结束符):"<<endl;        Insert(x,root);        cin>>x;      i++;    }};template<class E>bool BST<E>::Remove(const E x,BSTNode<E> *& ptr){//删除算法      BSTNode<E> *temp;      if(ptr!=NULL)   {       if(x<ptr->data)     Remove(x,ptr->left);        else      if(x>ptr->data)       Remove(x,ptr->right);      else       if(ptr->left!=NULL&&ptr->right!=NULL)       {            temp=ptr->right;            while(temp->left!=NULL)       temp=temp->left;      ptr->data=temp->data;      Remove(ptr->data,ptr->right);       }       else       {            temp=ptr;      if(ptr->left==NULL)       ptr=ptr->right;      else       ptr=ptr->left;      delete temp;      return true;       }   }   else      return false;};template<class E>void BST<E>::PrintTree(BSTNode<E> *subTree)//输出二叉树{        if(subTree!=NULL)  {     cout<<subTree->data<<" ";  PrintTree(subTree->left);  PrintTree(subTree->right);  }}void main(){     int choice=0;  BST<int>tree;     while(choice!=6)  {       cout<<endl;    cout<<"1、建立二叉搜索树:\n";       cout<<"2、插入元素:\n";       cout<<"3、查找元素:\n";    cout<<"4、删除元素:\n";    cout<<"5、输出二叉树:\n";    cout<<"6、退出!\n";    cout<<endl;    cout<<"请输入选项:"<<endl;    cin>>choice;    switch(choice)    {    case 1:                tree.Build(0);break;//'0'作为结束标志    case 2:        int x;        cout<<"输入要插入的元素:"<<endl;     cin>>x;     if(tree.Insert(x))      cout<<"插入成功!"<<endl;     else      cout<<"插入失败!"<<endl;     break;    case 3:        int s;     cout<<"输入要查找的元素:"<<endl;     cin>>s;     if(tree.Search(s)==NULL)      cout<<"二叉树中不存在该元素!"<<endl;     else     {      cout<<"二叉树存在该元素:"<<endl;     }     break;    case 4:        int n;     cout<<"输入要删除的元素:"<<endl;     cin>>n;     if(tree.Remove(n))      cout<<"删除成功!"<<endl;     else      cout<<"删除失败!"<<endl;     break;    case 5:        tree.PrintTree();     break;    case 6:        exit(1);    default:        cout<<"输入错误,请重新输入!"<<endl;     break;    }  }}/*   二叉搜索树,主要的功能就是搜索功能。刚开始做时,直接搬代码,可是发现很多都是没用的,浪费人力物力,然后不断的删除代码,精简代码,增加功能,完善二叉树。  书上的模板是template<class E,class K>,然后E的定义是很长的一个类,不是很懂这样做的意思...然后我就直接删了E,用K来定义就可以了。 总的来说,这个实验做的时间不是很多,总体和二叉树差不多,直接搬二叉树再结合书上的内容,基本上是没有什么问题的。  我现在比较纠结的就是,如果叫我自己写,我肯定写不出这样的好代码,只能搬书、理解书上的内容。*/


原创粉丝点击