双亲数组表示法——平时笔记2

博主在学习双亲数组表示法时遇到困难,无法成功调试,期望得到高人的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我也不知道出现什么问题了,反正就是没有调试出来,不知道有没有大神教教我

#include <stdio.h>
#define MAX 100
#define EXIST           0
#define IS_ROOT        -1
#define PARAMETER_ERR  -2
#define NOT_EXIST      -3
#define ERROR          -4
typedef char DataType;
typedef struct
{
	DataType data;
	int parent;
}PATreeNode;
typedef struct
{
	PATreeNode nodes[MAX];
	int n;
}PATree;
int FindParent_PATree(PATree T,int i)
{
	int id_parent;
	DataType val_parent;
	id_parent=T.nodes[i].parent;
	if(IS_ROOT==id_parent)
	{
		printf("The node %d is root,so it has no parent.",i);
	}
	else
	{
		val_parent=T.nodes[id_parent].data;
		printf("The ID of parent of node %d is %d.\n",i,id_parent);
		printf("The value of parent of node %d is %c.\n",i,val_parent);
	}
	return 0;
}
int FindParent_ID_PATree(PATree T,int i)
{
	int id_parent;
	if(i<0||i>=T.n)
	{
		printf("The paramter i is error!");
		return PARAMETER_ERR;
	}
	id_parent=FindParent_PATree(T,i);
	return id_parent;
}
int GetNodeID_PATree(PATree T,DataType val)
{
	int i;
	for(i=0;i<=T.n-1;i++)
	{
		if(val==T.nodes[i].data)
			return i;
	}
	return NOT_EXIST;
}
void FindParent_Val_PATree(PATree T,DataType val)
{
	int id;
	id=GetNodeID_PATree(T,val);
	if(NOT_EXIST==id)
	{
		printf("The tree does not include the node %c.\n",val);
		return;
	}
	FindParent_PATree(T,id);
}
void FindAllAncestor_PATree(PATree T,int i)
{
	int id_parent=i;
	while(IS_ROOT != id_parent)
	{
		id_parent=FindParent_PATree(T,id_parent);
	}
}
void FindAllAncestor_ID_PATree(PATree T,int i)
{
	int id_parent;
	id_parent=FindParent_ID_PATree(T,i);
	if(PARAMETER_ERR==id_parent)
		return;
	FindAllAncestor_PATree(T,id_parent);
}
void FindAllAncestor_Val_PATree(PATree T,DataType val)
{
	int id;
	id=GetNodeID_PATree(T,val);
	if(NOT_EXIST==id)
	{
		printf("The tree does not include the node %c.\n",val);
		return;
	}
	FindAllAncestor_PATree(T,id);
}
int FindChildren_PATree(PATree T,int i)
{
	int k;
	int count_children=0;
	for(k=0;k<=T.n-1;i++)
	{
		if(T.nodes[k].parent==i)
		{
			count_children++;
			printf("This Node is NO.%d child of node %d:\n",count_children,i);
			printf("The ID of the child node is %d, ",k);
			printf("The value of the child node is %c.\n",T.nodes[k].data);
		}
	}
	return count_children;
}
int FindChildren_ID_PATree(PATree T,int i)
{
	if(i<0||i>=T.n)
	{
		printf("The parameter i is error!");
		return PARAMETER_ERR;
	}
	return FindChildren_PATree(T,i);
}
int FindChildren_Val_PATree(PATree T,DataType val)
{
	int k;
	int count_children=0;
	int flag=NOT_EXIST;
	for(k=0;k<=T.n-1;k++)
	{
		if(val==T.nodes[k].data)
			flag=EXIST;
		else
			if(T.nodes[T.nodes[k].parent].data==val)
			{
				count_children ++;
				printf("This Node is No.%d child of node %c:\n",count_children,val);
				printf("The ID of the child node is %d, ",k);
				printf("The value of the child node is %c.\n",T.nodes[k].data);
			}
	}
	if(NOT_EXIST==flag)
		return NOT_EXIST;
	else
		return count_children;
}
int FindChildren_VALID_PATree(PATree T,DataType val)
{
	int id;
	id=GetNodeID_PATree(T,val);
	if(NOT_EXIST==id)
	{
		printf("The tree does not include the node %c.\n",val);
		return NOT_EXIST;
	}
	return FindChildren_PATree(T,id);
}
int main()
{
	PATree t;
	t.n=12;
	t.nodes[0].data='A';
	t.nodes[1].data='B';
	t.nodes[2].data='C';
	t.nodes[3].data='D';
	t.nodes[4].data='E';
	t.nodes[5].data='F';
	t.nodes[6].data='G';
	t.nodes[7].data='H';
	t.nodes[8].data='I';
	t.nodes[9].data='J';
	t.nodes[10].data='K';
	t.nodes[11].data='L';
	t.nodes[0].parent=-1;
	t.nodes[1].parent=0;
	t.nodes[2].parent=0;
	t.nodes[3].parent=0;
	t.nodes[4].parent=1;
	t.nodes[5].parent=1;
	t.nodes[6].parent=3;
	t.nodes[7].parent=3;
	t.nodes[8].parent=3;
	t.nodes[9].parent=3;
	t.nodes[10].parent=5;
	t.nodes[11].parent=7;
	printf("%d\n",FindParent_ID_PATree(t,2));
	FindParent_Val_PATree(t,'H');
	FindAllAncestor_ID_PATree(t,3);
	printf("ok!\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值