各种查找方法比较.docVIP

  • 4
  • 0
  • 约1.21万字
  • 约 16页
  • 2016-09-14 发布于重庆
  • 举报
各种查找方法比较

#includeiostream #includestdlib.h #includemalloc.h //#includetime.h #includewindows.h #define MAX 100010 #define M 100 using namespace std; int order; ///////////////////////////////////////////////////////1.折半查找 //1.折半查找函数 int binarysearch(int data[],int n,int x) { int low=0,high=n-1,mid,count=0; while(low=high) { count++; mid=(low+high)/2; if(xdata[mid])high=mid-1; else if(xdata[mid])low=mid+1; else {return count;} } return -1;//表示查找失败 } ////////////////////////////////////////////////////////2.平衡二叉排序树的查找 typedef struct bnode { int i; bnode *LeftChild; bnode *RightChild; bnode *Parent; }BTnode; //2.求深度 int treetall(BTnode *Root) { if(NULL==Root) return 0; return treetall(Root-LeftChild)treetall(Root-RightChild)?treetall(Root-LeftChild)+1:treetall(Root-RightChild)+1; } //2.判断平衡函数(若平衡返回1,若不平衡返回0) int IsBlance(BTnode *Root) { int bf; if(Root!=NULL) { bf=treetall(Root-LeftChild)-treetall(Root-RightChild); if((bf-1)||(bf1) ) return 0; else { if(IsBlance(Root-LeftChild)IsBlance(Root-RightChild)) return 1; else return 0; } } return 1; } //2.LL型调平衡(右旋) BTnode *R_Rotate(BTnode *Root,BTnode *p) { BTnode *b, *q, *c, *d; q=p-Parent; b=p-LeftChild; c=b-LeftChild; d=b-RightChild; p-LeftChild=d; if(d!=NULL) d-Parent=p; b-RightChild=p; p-Parent=b; if(q==NULL) { Root=b; b-Parent=NULL; //b的父结点为空,即b就是根结点 } else if(q-LeftChild==p) //如果a是父结点的左孩子 { q-LeftChild=b; //将b赋值给q的左孩子 b-Parent=q; //b的父结点是q } else if(q-RightChild==p) //如果a是父结点的右孩子 { q-RightChild=b; //将b赋值给q的右孩子 b-Parent=q; //b的父结点是q } return Root; } //2.RR型调平衡 BTnode *L_Rotate(BTnode *Root, BTnode *p) { BTnode *b, *q, *c, *d; q=p-Parent; b=p-RightChild; c=b-RightChild; d=b-LeftChild; p-RightChild=d; if(d!=NULL) d-Parent=p; b-LeftChild=p; p-Parent=b; if(q==NULL) { Root=b; //二叉树的根结点就是b,把b赋值给树Root b-Parent=NULL; //b的父结点为空

文档评论(0)

1亿VIP精品文档

相关文档