广工anyview数据结构习题第七章,
在学习过程中部分题目参考了Giyn 、戮漠、雁过留痕等大佬的代码,在此感谢。
题目解法不是最优解,但希望能给大家有所启发。同时也发了文档资源,需要可自取。
如果对你有帮助,可以给卑微的博主留个赞、关注、收藏 (不是)
(骗一下数据,说不定以后面试就过了,拜谢)
目录
DC07PE15
试编写算法,对一棵以孩子兄弟链表表示的树统计叶子的个数。孩子兄弟链表类型定义∶
typedef struct CSTNode {
TElemType data;
struct CSTNode *firstChild,*nextSibling;
} CSTNode,*CSTree;
要求实现下列函数∶
int Leave(CSTree T); /* 统计树T的叶子数 */
#include "allinclude.h" //DO NOT edit this line
int Leave(CSTree T)
{ // Add your code here
if(T==NULL)
return 0;
if(T->firstChild==NULL)
return (1+Leave(T->nextSibling));
else return ( Leave(T->nextSibling) + Leave(T->firstChild) );
}
DC07PE17
试编写算法,求一棵以孩子兄弟链表表示的树的度。孩子兄弟链表类型定义∶
typedef struct CSTNode {
TElemType data;
struct CSTNode *firstChild, *nextSibling;
} CSTNode,*CSTree;
要求实现下列函数∶
int Degree(CSTree T); /* 求树T的度*/
#include "allinclude.h" //DO NOT edit this line
int Degree(CSTree T)
{ // Add your code here
if(T==NULL)
return 0;
void Degree1(CSTree T, int &maxnum);
int maxnum=0;
Degree1(T,maxnum);
return maxnum;
}
void Degree1(CSTree T, int &maxnum)
{
if(T==NULL)
return ;
if(T->firstChild!=NULL)
{
int n=1;
CSTree temp=T;
temp=temp->firstChild;
while(temp->nextSibling!=NULL)
{
temp=temp->nextSibling;
n++;
}
maxnum= maxnum>n ? maxnum : n;
}
Degree1(T->firstChild,maxnum);
Degree1(T->nextSibling,maxnum);
}
DC07PE22
试编写算法,对以双亲表示法存储的树计算深度。树的双亲表示法的类型定义如下∶
typedef struct {
TElemType data;
int parent; //双亲位置
} PTNode; //结点类型
typedef struct {
PTNode nodes[MAX_TREE_SIZE]; // 结点存储空间
int n,r; // 结点数和根的位置
}PTree;
要求实现以下函数∶
int PTreeDepth(PTree T); /*求树T的深度 */
#include "allinclude.h" //DO NOT edit this line
int PTreeDepth(PTree T)
{ // Add your code here
if(T.n==0)
return 0;
int max=0,i=T.n,num;
PTNode temp;
while(i>0)
{
num=0;
temp=T.nodes[--i];
while(temp.parent!=-1)
{
num++;
temp=T.nodes[temp.parent];
}
if(num>max)
max=num;
}
return max+1; //加上根结点
}
DC07PE24
试编写算法,对以双亲孩子表示法存储的树计算深度。
孩子链表类型定义∶
typedef struct ChildNode {
int childIndex; // 孩子结点
struct ChildNode *nextChild;
}ChildNode; //孩子节点类型
typedef struct {
TElemType data;
int parent; // 双亲位置
struct ChildNode *firstChild; // 孩子链表头指针
}PCTreeNode; // 结点类型
typedef struct {
PCTreeNode *nodes; //结点存储空间
int n,r; //结点数和根的位置
} PCTree;
要求实现下列函数∶
int PCTreeDepth(PCTree T); /* 求树T的深度*/
#include "allinclude.h" //DO NOT edit this line
int PCTreeDepth(PCTree T)
{ // Add your code here
if(T.n==0)
return 0;
int max=0,i=T.n,num;
PCTreeNode temp;
while(i>0)
{
num=0;
temp=T.nodes[--i];
while(temp.parent!=-1)
{
num++;
temp=T.nodes[temp.parent];
}
if(num>max)
max=num;
}
return max+1; //加上根结点
}
DC07PE26
试编写算法,对以孩子-兄弟链表表示的树计算深度。孩子兄弟链表类型定义∶
typedef struct CSTNode {
TElemType data;
struct CSTNode *firstChild,*nextSibling;
} CSTNode,*CSTree;
要求实现下列函数∶
int TreeDepth(CSTree T); /*求树T的深度*/
#include "allinclude.h" //DO NOT edit this line
int TreeDepth(CSTree T)
{ // Add your code here
if(T==NULL)
return 0;
int dep1,dep2,dep;
dep1=TreeDepth(T->firstChild);
dep2=TreeDepth(T->nextSibling);
dep= (dep1+1)>dep2 ? (dep1+1) : dep2;
return dep;
}
DC07PE63
试编写非递归算法,实现并查集带路径压缩的查找操作。并查集的类型定义如下∶
typedef struct {
int *parent;
int n;
}MFSet;
实现下列函数∶
int find(MFSet S,int i);
/* 并查集带路径压缩的查找的非递归实现 */
#include "allinclude.h" //DO NOT edit this line
int find(MFSet S, int i)
{ // Add your code here
if(i<0 || i>=S.n)
return -1;
if(S.parent[i]<0)
return i;
int temp=i,temp1=i;
while(S.parent[temp]>=0)
{
temp=S.parent[temp];
}
while(temp1!=temp)
{
temp1=S.parent[i];
S.parent[i]=temp;
i=temp1;
}
return temp;
}