版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JACK_JYH/article/details/81316473
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<fstream>
#include<map>
using namespace std;
//孩子表示法
#define MAX_TREE_SIZE 100
typedef char TElemType;
typedef struct CTNode
{
int child;
struct CTNode* next;
}*ChildPtr;
typedef struct
{
TElemType data;
ChildPtr firstchild;
}CTBox;
typedef struct{
CTBox nodes[MAX_TREE_SIZE];
int r,n;
}CTree;
//孩子表示法
int main()
{
CTree mCTree;
mCTree.r=0;
mCTree.n=10;
for(int i=0;i<mCTree.n;i++)
{
mCTree.nodes[i].data='A'+i;
}
CTNode ctNode[10];
for(int i=1;i<=9;i++)
{
ctNode[i].child=i;
}
ctNode[1].next=&ctNode[2];
ctNode[4].next=&ctNode[5];
ctNode[6].next=&ctNode[7];
ctNode[7].next=&ctNode[8];
mCTree.nodes[0].firstchild=&ctNode[1];
mCTree.nodes[1].firstchild=&ctNode[3];
mCTree.nodes[2].firstchild=&ctNode[4];
mCTree.nodes[3].firstchild=&ctNode[6];
mCTree.nodes[4].firstchild=&ctNode[9];
//广度搜索直接按序输出CTree.nodes即可
cout<<"广度优先遍历"<<endl;
for(int i=0;i<mCTree.n;i++)
{
cout<<mCTree.nodes[i].data<<" ";
}
cout<<endl;
cout<<"A子节点"<<endl;
CTNode* temp;
temp=mCTree.nodes[0].firstchild;
cout<<mCTree.nodes[temp->child].data<<" ";
while(1)
{
cout<<mCTree.nodes[temp->child].data<<" ";
if(temp->next!=NULL)
{
temp=temp->next;
}else{
break;
}
}
cout<<endl;
return 0;
}