为什么孩子是违规词汇。。
#include <iostream>
using namespace std;
const int MAXSIZE = 20;
typedef char ElemType;
typedef struct CTNode {//孩子结点
int child;
struct CTNode* next;
}*Childptr;
typedef struct {//双亲结点
ElemType data;
Childptr firstchild;
}CTBox;
typedef struct {//树结构
CTBox nodes[MAXSIZE];
int n, root;//结点数,根节点的位置
}CTree;
//队列定义及基本操作定义
const int QUEUE_INIT_SIZE = 100;
const int QUEUEINCREMENT = 10;
typedef int QElemType;
typedef struct
{
QElemType* data;
int front;
int rear;
int queuesize;
int incresize;
}SqQueue;
bool InitQueue(SqQueue& Q, int = QUEUE_INIT_SIZE, int = QUEUEINCREMENT);//初始化循环队列
int QueueLength(SqQueue Q);//返回队列长度
bool DeQueue(SqQueue& Q, QElemType& e);//将队首元素出队,用e返回
bool EnQueue(SqQueue& Q, QElemType e);//将元素e放入循环队列
bool GetHead(SqQueue Q, QElemType& e);//取队首元素,用e返回
bool incrementQueuesize(SqQueue& Q);//当循环队列空间不足时,动态扩充空间
void TraverseQueue(SqQueue Q);//遍历队列
bool QueueEmpty(SqQueue Q);//判断队列是否为空
bool ClearQueue(SqQueue& Q);//清空队列
//树的定义及基本操作定义
void CreateTree(CTree& T);//创建孩子链表结构的树
int LocateNode(CTree& T, ElemType e);//返回元素e在树中的结点位次,若不存在则返回-1
void PrintCTree(CTree& T);//打印树
void Traverse(CTree T);
int main()
{
CTree T;
cout << "---------------------树(孩子链表)---------------------" << endl;
CreateTree(T);
cout << "-------------------该树的孩子链表为:-------------------" << endl;
PrintCTree(T);
cout << "层次遍历:";
Traverse(T);
cout << endl;
cout << "--------------------------------------------------------" << endl;
return 0;
}
//队列基本操作的实现
bool InitQueue(SqQueue& Q, int maxsize, int incresize) {
Q.data = new QElemType[maxsize];
if (!Q.data)return 0;
Q.front = Q.rear = 0;
Q.queuesize = maxsize;
Q.incresize = incresize;
return 1;
}
int QueueLength(SqQueue Q) {
return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
}
bool DeQueue(SqQueue& Q, QElemType& e) {
if (Q.front == Q.rear)
return 0;
e = Q.data[Q.front];
Q.front = (Q.front + 1) % Q.queuesize;
return 1;
}
bool EnQueue(SqQueue& Q, QElemType e) {
if ((Q.rear + 1) % Q.queuesize == Q.front)
if (!incrementQueuesize(Q))
return 0;
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % Q.queuesize;
return 1;
}
bool GetHead(SqQueue Q, QElemType& e) {
if (Q.rear == Q.front)
return 0;
e = Q.data[Q.front];
return 1;
}
bool incrementQueuesize(SqQueue& Q) {
QElemType* newdata = new QElemType[Q.queuesize + Q.incresize];
if (!newdata)return 0;
for (int i = 0; i < Q.queuesize; i++)
newdata[i] = Q.data[(Q.front + i) % Q.queuesize];
delete[] Q.data;
Q.data = newdata;
Q.front = 0; Q.rear = Q.queuesize - 1;
Q.queuesize += Q.incresize;
return 1;
}
void TraverseQueue(SqQueue Q) {
while (Q.front != Q.rear) {
cout << Q.data[Q.front] << " ";
Q.front = (Q.front + 1) % Q.queuesize;
}
cout << endl;
}
bool QueueEmpty(SqQueue Q) {
if (Q.front == Q.rear)
return 1;
return 0;
}
bool ClearQueue(SqQueue& Q) {
Q.front = Q.rear;
return 1;
}
//树的基本操作的实现
void CreateTree(CTree& T) {
ElemType e1, e2;
int i, j;
cout << "请输入树的结点数:";
cin >> T.n;
cout << "请输入各结点信息:";
for (i = 0; i < T.n; i++) {
cin >> T.nodes[i].data;
T.nodes[i].firstchild = NULL;//每个结点的首孩子初始设为NULL
}
cout << "请输入各分支(以##结束):";
while (1) {
cin >> e1 >> e2;
if (e1 == '#') {
if (e2 != '#') {//判断输入为根节点或输入结束
T.root = LocateNode(T, e2);
continue;
}
else break;
}
i = LocateNode(T, e1);
j = LocateNode(T, e2);//将输入的元素转为在结点中的位次,便于运算
Childptr temp = new CTNode;//构建孩子结点
if (!temp)exit(0);
temp->child = j;
temp->next = NULL;
Childptr tempchild = T.nodes[i].firstchild;//令tempchild为第i个结点的首孩子
if (tempchild)//若首孩子存在,寻找到尾部加入temp
{
while (tempchild->next)
{
tempchild = tempchild->next;
}
tempchild->next = temp;
}
else {//若不存在则将首孩子设为temp
T.nodes[i].firstchild = temp;
}
}
}
int LocateNode(CTree& T, ElemType e){
for (int i = 0; i < T.n; i++)
if (T.nodes[i].data == e)
return i;
return -1;
}
void PrintCTree(CTree& T){
for (int i = 0; i < T.n; i++){
cout << T.nodes[i].data << "->";
Childptr p = T.nodes[i].firstchild;
while (p){
cout << LocateNode(T, T.nodes[p->child].data) << " ";
p = p->next;
}
cout << endl;
}
}
void Traverse(CTree T) {
bool visited[MAXSIZE];//用来记录结点是否访问过
for (int i = 0; i < T.n; i++)
visited[i] = false;//初始均设为未访问
SqQueue S; InitQueue(S);
if (!visited[T.root]) {
visited[T.root] = true;
EnQueue(S, T.root);
while (!QueueEmpty(S)) {
QElemType e;
DeQueue(S, e);//利用e接收队首信息
cout << T.nodes[e].data << " ";
Childptr temp = T.nodes[e].firstchild;
while (temp) {//将这一层的元素全部入队
if (!visited[temp->child]) {
visited[temp->child] = true;
EnQueue(S, temp->child);
}
temp = temp->next;
}
}
}
}
实验样例