数据结构-树的hai子兄弟链表表示法代码实现

#include <iostream>
using namespace std;

typedef char ElemType;
typedef struct CSNode {//孩子兄弟结点
	ElemType data;
	struct CSNode* firstchild, * nextSibling;
}CSNode,*CSTree;
//队列定义及基本操作定义
const int QUEUE_INIT_SIZE = 100;
const int QUEUEINCREMENT = 10;
typedef CSNode* 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(CSTree& T);
void Preorder(CSTree T);
void Postorder(CSTree T);
int TreeDepth(CSTree T);



int main()
{
	CSTree T;
	cout << "即将顺序建立一棵树!" << endl;
	CreateTree(T);
	//CreateCSTree(T);
	cout << "先根遍历:" << endl;
	Preorder(T);//先根遍历
	cout << "\n后根遍历:" << endl;
	Postorder(T);//后根遍历
	cout << endl;
	cout << "树深度为:" << TreeDepth(T) << 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(CSTree& T) {
	T = NULL;
	SqQueue Q; InitQueue(Q);
	char fa, ch;//父与子
	cout << "输入分支(以#为标志):" << endl;
	cin >> fa >> ch;
	CSNode* s = NULL;//s用来接收出队或队首信息
	CSNode* r = NULL;//r用来记录兄弟节点的位置
	while (ch != '#') {
		CSNode* p = new CSNode;
		if (!p)exit(0);
		p->data = ch;
		p->firstchild = p->nextSibling = NULL;
		EnQueue(Q, p);
		if (fa == '#')
			T = p;//若p的父为空,则p为根节点
		else {
			GetHead(Q, s);
			while (s->data != fa) {//循环找到p的父节点
				DeQueue(Q, s);
				GetHead(Q, s);
			}
			if (s->firstchild == NULL) {//若首孩子不存在,则令p为首孩子,r后移记录
				s->firstchild = p;
				r = p;
			}
			else {//若首孩子存在,则根据r记录的位置插入p,r后移记录
				r->nextSibling = p;
				r = p;
			}
		}
		cin >> fa >> ch;
	}
}
void Preorder(CSTree T) {
	if (T) {
		cout << T->data << "  ";
		Preorder(T->firstchild);
		Preorder(T->nextSibling);
	}
}
void Postorder(CSTree T) {
	if (T) {
		Postorder(T->firstchild);
		cout << T->data << "  ";
		Postorder(T->nextSibling);
	}
}
int TreeDepth(CSTree T) {
	int ch, bro;
	if (!T)return 0;
	ch = TreeDepth(T->firstchild);
	bro = TreeDepth(T->nextSibling);
	if (ch + 1 > bro)//首孩子的深度需要+1,兄弟与转化为二叉树后的父节点同级深度相同不需要+1
		return ch + 1;
	return bro;
}

实验样例

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜菜的大鹏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值