是否二叉搜索树
这题只要是了解二叉查找树的特点。。。。
bool IsBST ( BinTree T )
{
if(!T)return true;
else if(!T->Left&&!T->Right)return true;
else if(T->Left&&!T->Right&&T->Left->Data>T->Data)return false;
else if(!T->Left&&T->Right&&T->Right<T->Data)return false;
else if(T->Left&&T->Right&&T->Left->Data>T->Data||T->Left&&T->Right&&T->Right->Data<T->Data)return false;
else
{
BinTree Left1=T->Left,Right1=T->Right;
while(Left1)
{
if(Left1->Left&&!Left1->Right&&Left1->Left->Data>T->Data)
return false;
else if(!Left1->Left&&Left1->Right&&Left1->Right->Data>T->Data)
return false;
else if(Left1->Left&&Left1->Right&&Left1->Left->Data>T->Data||Left1->Left&&Left1->Right&&Left1->Right->Data>T->Data)
return false;
Left1=Left1->Left;
}
while(Right1)
{
if(Right1->Left&&!Right1->Right&&Right1->Left->Data<T->Data)
return false;
else if(!Right1->Left&&Right1->Right&&Right1->Right->Data<T->Data)
return false;
else if(Right1->Left&&Right1->Right&&Right1->Left->Data<T->Data||Right1->Left&&Right1->Right&&Right1->Right->Data<T->Data)
return false;
Right1=Right1->Right;
}
return IsBST(T->Left)&&IsBST(T->Right);
}
}
线性探测法的查找函数
主要注意哈希函数,,这里那个常量是真的坑,,卡了半天。。。
Position Find( HashTable H, ElementType Key )
{
int t=Hash(Key,H->TableSize);
if(H->Cells[t].Info==Empty)return t;
while(H->Cells[t].Data!=Key)
{
t=Hash(t+1,H->TableSize);
if(H->Cells[t].Info==Empty)
{
return t;
}
if(t==Hash(Key,H->TableSize))
return ERROR;
}
H->Cells[t].Info = Legitimate;
return t;
}
分离链接法的删除操作函数
循环跑一次就行了
bool Delete( HashTable H, ElementType Key )
{
int t=Hash(Key,H->TableSize);
if(H->Heads[t].Next==NULL)return false;
else
{
List p=H->Heads[t].Next,q=p;
if(strcmp(p->Data,Key)==0)
{
H->Heads[t].Next=H->Heads[t].Next->Next;
printf("%s is deleted from list Heads[%d]\n",Key,t);
free(p);
return true;
}
while(p)
{
if(strcmp(p->Data,Key)==0)
{
printf("%s is deleted from list Heads[%d]\n",Key,t);
free(p);
return true;
}
q=p;
p=p->Next;
}
return false;
}
}
邻接矩阵存储图的深度优先遍历
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) )
{
Visit(V);
Visited[V]=true;
for(int i=0;i<Graph->Nv;i++)
{
if((V!=i)&&Graph->G[V][i]!=INFINITY&&(Visited[i]==false))
{
DFS(Graph,i,Visit);
}
}
}
最大子列和问题
#include<stdio.h>
int main()
{
int a[1000000];
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",a+i);
int max=0,sum=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
if(sum>max)
max=sum;
if(sum<0)
sum=0;
}
printf("%d\n",max);
}
}
数组循环左移
#include<stdio.h>
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
m%=n;
int a[1000];
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
}
for(int i=0;i<m;i++)
{
int t=a[0];
for(int j=1;j<n;j++)
a[j-1]=a[j];
a[n-1]=t;
}
for(int i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d",a[n-1]);
}
}