#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 main()
{
printf("ok!\n");
return 0;
}
双亲数组表示法——平时笔记
最新推荐文章于 2024-04-15 08:23:55 发布