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

来源:互联网 发布:淘宝网摩托车专卖区 编辑:程序博客网 时间:2024/04/28 19:14

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

#include <stdio.h>#define MAX 100#define EXIST           0#define IS_ROOT        -1#define PARAMETER_ERR  -2#define NOT_EXIST      -3#define ERROR          -4typedef 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;elseif(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;elsereturn 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;}


原创粉丝点击