广工anyview数据结构第七章(2021.12)

广工anyview数据结构习题第七章,

在学习过程中部分题目参考了Giyn 、戮漠、雁过留痕等大佬的代码,在此感谢。

题目解法不是最优解,但希望能给大家有所启发。同时也发了文档资源,需要可自取。

如果对你有帮助,可以给卑微的博主留个赞、关注、收藏   (不是) 

(骗一下数据,说不定以后面试就过了,拜谢)

目录

DC07PE15

DC07PE17

DC07PE22

DC07PE24

DC07PE26

DC07PE63


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;
}

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凛_Lin~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值