数据结构-树的hai子链表存储结构层次遍历

 为什么孩子是违规词汇。。

#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;
			}
		}
	}
}

实验样例

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜的大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值