二叉排序树递归非递归算法性能对比

 

 

 

 

 

 

 #include<iostream>

#include<ctime>

using namespace std;

 

typedef long ElemType;

typedef class BinarySearchTree_Node

{

   public:

       ElemTypedata;

   BinarySearchTree_Node *left, *right;

}BinarySearchTree_T, *BinarySearchTree_P;

 

BinarySearchTree_PSearch_BinarySearchTree(BinarySearchTree_P root, ElemType key)//非递归查找

{

   BinarySearchTree_P p = root;

   while (p)

   {      

       if (p->data == key)  return p;

       p = (key < p->data) ?p->left : p->right;

    }

   return NULL;

}

 

BinarySearchTree_PSearch_BinarySearchTree1(BinarySearchTree_P root, ElemType key)//递归查找

{

   if (root == NULL)

       return NULL;

   if (key > root->data) //查找右子树 

       return Search_BinarySearchTree(root->right, key);

   else if (key < root->data) //查找左子树 

       return Search_BinarySearchTree(root->left, key);

   else

       return root;

}

 

voidInsert_BinarySearchTree(BinarySearchTree_P *root, ElemType data)

{

   //初始化插入节点

  BinarySearchTree_P p= (BinarySearchTree_P)malloc(sizeof(structBinarySearchTree_Node));

   if (!p)

              return;

   p->data = data;

   p->left = p->right = NULL;

 

   //空树时,直接作为根节点

   if (*root == NULL)

    {

       *root = p;

       return;

    }

   //是否存在,已存在则返回,不插入

   if (Search_BinarySearchTree(*root, data) != NULL)

              return;

   //进行插入,首先找到要插入的位置的父节点

   BinarySearchTree_P tnode = NULL, troot = *root;

   while (troot)

   {      

       tnode = troot;

       troot = (data < troot->data) ? troot->left : troot->right;

    }

   if (data < tnode->data)

       tnode->left = p;

   else

       tnode->right = p;

}

voidCreateBinarySearchTree(BinarySearchTree_P *T, ElemType ElemTypeger)

{

       Insert_BinarySearchTree(T, ElemTypeger);

}

 

void MidOrderTraverse(BinarySearchTree_P T)

{

   if (T)

    {

       MidOrderTraverse(T->left);

       cout << T->data << " ";

       MidOrderTraverse(T->right);

    }

}

 

int main()

{

  long i,t1,t2,t3,t4;

   BinarySearchTree_P root = NULL;

   cout<<"Create BinarySearchTree ing"<<endl;

   for(i=0;i<1000000;i++){

       //创建二叉排序树

       CreateBinarySearchTree(&root, rand());

    }

 

   

       //MidOrderTraverse(root);

   cout <<"complete"<< endl;

   ElemType n;

   cin>>n;

   BinarySearchTree_P result=0;

   t1=clock();

   for(i=0;i<32767;i++)

   result = Search_BinarySearchTree(root, n);

   t2=clock();

   if (result){

       cout<<"\n非递归Search Data:"<<n;

       cout << "查找结果:\n" <<"指针:" << result << endl <<"指针的值:"

           << result->data<<" time:"<<((double)t2-t1/1000000.0)<<"ms"<< endl;

    }

   else cout<<"error"<<endl;

   t3=clock();

   for(i=0;i<1000000;i++)

   result = Search_BinarySearchTree1(root, n);

   t4=clock();

   if (result){

       cout<<("\n递归Search Data: ");

       cout << "查找结果:\n" <<"指针:" << result << endl <<"指针的值:"

           << result->data<<" time:"<<((double)t4-t3/1000000.0)<<"ms"<< endl;

    }

   else cout<<"error"<<endl;

   return 0;

}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值