查找

来源:互联网 发布:全国各地地址的数据库 编辑:程序博客网 时间:2024/04/30 21:42


#include<iostream>
#include<assert.h>
#include <time.h>
#include <cstdlib>
#include<math.h>
#include"Binarytree.h"
using namespace std;
#define CLOCK_PER_SEC 100;

 

template<class T>
struct BinTreeNode{
 T data;
 BinTreeNode<T> *leftChild, *rightChild;
 BinTreeNode():leftChild(NULL),rightChild(NULL){}
 BinTreeNode(T x,BinTreeNode<T> *l=NULL,BinTreeNode<T> *r=NULL):data(x),leftChild(l),rightChild(r){}
};


class BinaryTree{
public:
 BinaryTree():root(NULL){}
 BinaryTree(int value);//:RefValue(value),root(NULL){}
 BinaryTree(BinaryTree& s)
 {
  this->RefValue=s.RefValue;
  this->root=s.root;
 }
//~BinaryTree(){destroy(root);}
 bool IsEmpty(){return(root==NULL)?true:false;}
 
 BinTreeNode<int> *getRoot()const {return root;}
 bool Insert(int &x){ return Insert(x,root);}
 bool Remove(int &x){ return Remove(x,root);}
 void Search(const int x)
 {
  BinTreeNode<int>*ptr=Search(x,root);
  if(ptr!=NULL)
  {
   cout<<"二叉搜索成功!!!"<<endl;
  }
  else
   cout<<"二叉搜索失败!!!"<<endl;
 }
// void levelOrder(void(visit)(BinTreeNode<T> *p));//层次序遍历
protected:
    BinTreeNode<int> *root;
 int RefValue; //输入数据停止标志
 BinTreeNode<int>* Search(const int x, BinTreeNode<int>* ptr);
 BinTreeNode<int> *Copy(BinTreeNode<int>*orignode);  //复制
 void Traverse(BinTreeNode<int> * subTree,ostream& out);//前序遍历输出
 bool Insert(int &x,BinTreeNode<int> *&ptr);
 bool Remove(int &x,BinTreeNode<int> *&ptr);
 /*friend istream& operator>>(istream& in,BinaryTree& Tree)
 {
     Tree.CreateBinTree(in,Tree.root);
      return in;
    }*/
 friend ostream& operator<<(ostream& out,BinaryTree& Tree)
    {
     Tree.Traverse(Tree.root,out);
     out<<endl;
     return out;
    }
};

 

BinaryTree::BinaryTree(int value)
{
 int i=0;
 int x;
 root=NULL;
 RefValue=value;
 dataList<int> L;
 L.initList();
 L.display();

 x=L.getKey(i+1);
 while(x<L.Length())
 {
  Insert(x,root);
  i++;
  x=L.getKey(i+1);
 }
}


void BinaryTree::Traverse(BinTreeNode<int> *subTree,ostream &out)
{
    if(subTree!=NULL)
 {
  out<<subTree->data<<' ';
  Traverse(subTree->leftChild,out);
  Traverse(subTree->rightChild,out);
 }
};


//void BinaryTree::destroy(BinTreeNode<int> *&subTree) 
//{
//    if(subTree!=NULL)
// {
//  destroy(subTree->leftChild);
//  destroy(subTree->rightChild);
//  delete subTree;
// 
// }
//};

 

BinTreeNode<int>* BinaryTree::Search(const int x, BinTreeNode<int>* ptr)
{
 if(ptr==NULL) return NULL;
 else if(x<ptr->data) return Search(x,ptr->leftChild);
 else if(x>ptr->data) return Search(x,ptr->rightChild);
 else return ptr;
}


bool BinaryTree::Insert(int &x,BinTreeNode<int> *&ptr)
{
 if(ptr==NULL)
 {
  ptr=new BinTreeNode<int>(x);
  if(ptr==NULL)
  {
   cerr<<"OUT OF SPACE!!!"<<endl;
   exit(1);
  }
  return true;
 }
 else if(x<ptr->data)
  Insert(x,ptr->leftChild);
 else if(x>ptr->data)
  Insert(x,ptr->rightChild);
 else return false;//x已在树中;
}


 
void visit(BinTreeNode<int> *p) {
 cout<< p->data<<" ";
}

 

void main()
{
     clock_t  start,end,start2,end2;
  double time ;
 dataList<int> L;
 L.initList();
 L.display();


 int key;
 cout<<"请输入要顺序查找的关键字:";
 cin>>key;
 start=clock();
 int index=L.SequentSearch(key);
 if(index!=0)
 {
  cout<<"关键字搜索成功!";
  cout<<endl<<"关键字在数组中的下标为: "<<index;
 }
 end=clock();
 cout<<endl;
   time=(double)(end-start)/CLOCKS_PER_SEC;
   cout<<"顺序查找时间"<<time<<endl;
 /*int key1;
 cout<<"请输入要顺序查找的关键字:";
 cin>>key1;
 int index1=L.BinarySearch(key1);
 if(index1>0)
 {
  cout<<"关键字搜索成功!";
  cout<<endl<<"关键字在数组中的下标为: "<<index1;
 }
 cout<<endl;*/

 int key2=-1;
 int height,size;
 cout<<"创建tree二叉树"<<endl;
 BinaryTree  tree(key2);
 //
 //
 int value;
     cout<<"输入搜索数据: ";
 cin>>value;
 start2=clock();
 tree.Search(value);
 end2=clock();
 cout<<"二叉树搜索时间"<<(double)(end-start)/CLOCK_PER_SEC;

}