- 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的父结点为空
您可能关注的文档
- 发酵床菌种.doc
- 叔丁醇_性质用途与生产工艺1.doc
- 受力分析-----整体法和隔离法.doc
- 大学物理力热习题集.doc
- 变压器小结变压器中的分布电容与屏蔽.doc
- 变压器套管末屏日常工作注意事项.doc
- 变废为宝分析报告.doc
- 变式教学打造物理高效课堂.doc
- 变式训练专题.doc
- 电厂空冷岛热泵方案.ppt
- 2025-2026学年天津市和平区高三(上)期末数学试卷(含解析).pdf
- 2025-2026学年云南省楚雄州高三(上)期末数学试卷(含答案).pdf
- 2025-2026学年甘肃省天水市张家川实验中学高三(上)期末数学试卷(含答案).docx
- 2025-2026学年福建省厦门市松柏中学高二(上)期末数学试卷(含答案).docx
- 2025-2026学年广西钦州市高一(上)期末物理试卷(含答案).docx
- 2025-2026学年河北省邯郸市临漳县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省石家庄二十三中七年级(上)期末历史试卷(含答案).docx
- 2025-2026学年海南省五指山市九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省唐山市玉田县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省邢台市市区九年级(上)期末化学试卷(含答案).docx
原创力文档

文档评论(0)