1. 给定整型数组B[m][n],B中数据在每一维方向都按从小到大的次序排列,而且整型变量x在B中存在,找出一对满足B[i][j]==x的i和j,要求比较次数不超过m+n
分析:从右上角的元素B[i][j]开始与x比较,每次有3种情况:(1)相等,则比较结束;(2)B[i][j]<x,则这一行不包含x,搜索下一行;(3)B[i][j]>x,则这一列不包含x,搜索下一列;这样,每次至少使搜索范围减少一行或一列,最多经过m+n次就可以找到x.
void SearchIndex(int B[M][N],int x,int &i,int &j)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i = 0,j= N-1;
while(B[i][j]!=x)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(B[i][j]<x)
i++;
else
j--;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2,将一维数组A[n*n](n<=10)中的元素,按蛇型方式存放在二维数组B[n][n]中,
A[0] A[1] A[5] A[6]。。。
A[2] A[4] A[7] A[13]。。。
A[3] A[8] A[12]。。。
B = A[9] A[11]。。。
A[10]。。。
分析:从B的左上角开始对平行于副对角线的2n-1条对角线的元素交替进行赋值。
#define N 10
void SnakeSet(int A[],int B[][N])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i,j,k,m,flag;//k记录已经完成赋值的元素个数,flag控制赋值方向。
int *p = A;
for(m = 0,flag = 1,k = 0;m<N;m++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(flag >0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i = m,j = 0;
while(j<=m)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
B[i][j] = p[k];
i--;j++;k++;
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i = 0,j = m;
while(i<=m)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
B[i][j] = p[k];
i++;j--;k++;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
flag = -flag;
}
for(m = 1;m<N;m++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(flag >0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i = N-1,j = m;
while(j<=N)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
B[i][j] = p[k];
i--;j++;k++;
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i = m,j = N-1;
while(i<N)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
B[i][j] = p[k];
i++;j--;k++;
}
}
flag = -flag;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.判断二叉树T是否是完全二叉树,T采用二叉链表作为存储结构。
分析:利用层次遍历思想,设置一个标志flag,初值为0,按层次遍历时,若某个结点的左子树或右子树为空,则置flag为1,若此后遍历的结点的左子树和右子树都为空,则是完全二叉树,否则不是。
#define MAXSIZE 100
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
bool BTLink_jurdge(BTNode * T)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
BTNode *Q[MAXSIZE];
BTNode *p = T;
int flag = 0;
if(T==NULL)
return true;
InitQueue(Q);
EnQueue(Q,p);
while(!QueueEmpty(Q))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DeQueue(Q,p);
if(p->lchild!=NULL &&flag!=1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnQueue(Q,p->data);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(p->lchild!=NULL)
return false;
else
return true;
}
if(p->rchild!=NULL &&flag!=1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnQueue(Q,p->data);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(p->rchild!=NULL)
return false;
else
return true;
}
}
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4,二叉树T采用二叉链表作为存储结构,从右向左依次释放所有叶子结点,并且把结点值放到一个数组中。
分析:采用后序遍历的变形方式,先判断二叉树的根结点,再遍历右子树,最后遍历左子树。
void BTLink_DeleteLeaf(BiTree &T,ElemType sava[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
static int i = 0;
if(T==NULL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return i;
}
if(T->lchild==NULL && T->child==NULL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
save[i] = T->data;
i++;
free(T);
T = NULL;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BTLink_DeleteLeaf(T->rchild,save);
BTLink_DeleteLeaf(T->lchild,save);
}
return i;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5.求任一指定结点所在的层次,二叉树T采用二叉链表作为存储结构.
#define MAX 100
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int BTLink_LevelNum(BTNode* T,ElemType x)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
BTNode *Q[MAX],*p;
int front = 0,m,n,Level[MAX];
++rear;
Q[rear] = T;
Level[rear] = 1;
while(front!=rear)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
front = (front+1)%MAX;
p = Q[front];
m = Level[front];
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p->data==x)
{
n = m;
break;
}
if(p->lchild!=NULL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rear = (rear+1)%MAX;
Q[rear] = p->lchild;
Level[rear] = m +1;
}
if(p->rchild!=NULL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rear = (rear+1)%MAX;
Q[rear] = p->rchild;
Level[rear] = m +1;
}
}
return n;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)