数据结构实验一——用孩子兄弟表示法表示树并求树的高度和树的度

本文详细介绍了如何使用C++实现孩子兄弟表示法来表示树结构,包括树的高度和度的计算方法,以及在VisualStudio环境下创建和测试二叉树类模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用孩子兄弟表示法表示树并求树的高度和树的度 实验

1.1 实验内容

孩子兄弟表示法是树的一种链式存储结构,在这种结构中,链表中一个结点代表书中的一个结点,除了信息域外,还有两个指针域firstChild、nextSibling分别指向该结点的第一个孩子结点和下一个兄弟结点。

在这种存储结构上要查找某结点的孩子结点的操作是比较方便的,通过结点的孩子指针可以直接找到它的第一个孩子结点,再由孩子结点的兄弟指针很容易找到它的其他孩子结点。

1.2文件结构、开发环境等说明

开发环境版本

VisualStudio 2022

工程文件名

c6-tree.sln

头文件个数

2个

源程序文件个数

1个

文件名

文件类型

功能简介

备注

csnode.h

头文件

结点类模板头文件,包括数据成员及成员函数的声明与定义

tree.h

头文件

二叉树类模板头文件,包括数据成员及成员函数的声明与定义

text.cpp

源文件

测试文件

包含树的高度和度的测试

1.3 实现技术

1、二叉树类模板BinaryTree中,数据成员为结点类CSNode的数组root,CSNode的数据成员中有数据域、两个指针域firstchild、nextsibling。

在二叉树类声明了两个求树的高度的函数:heighttree()、height(CSNode<ElemType>* p)const和两个求树的度的函数:degreetree()、Degree(CSNode<ElemType>* p)const

结点类:

template<class ElemType>
class CSNode
{
private://数据成员
	ElemType data;
	CSNode* firstchild;
	CSNode* nextsibling;

public:
	CSNode();//构造函数1
	CSNode(CSNode* fc, CSNode* ns, ElemType dt);//构造函数2
	ElemType getData();
};

二叉树类:

//二叉树类
template<class ElemType>
class BinaryTree
{
private://数据成员
	CSNode<ElemType>* root;
public:
	BinaryTree();
	BinaryTree(const ElemType& e);//构造以e为根的二叉树
	virtual~BinaryTree();
	void createTree();
	void create(CSNode<ElemType>*& p);
	void preOrderTraverse();//先序遍历 
	void preOrder(CSNode<ElemType>* p);
	int heighttree();
    int degreetree();
	void destoryTree();

protected://辅助函数
	void Destory(CSNode<ElemType>*& p);	// 删除以r为根二叉树
	int height(CSNode<ElemType>* p)const;//求树的高度
	int Degree(CSNode<ElemType>* p)const;//求树的度
	void destory(CSNode<ElemType>*& p, int& num);
};

2、在模板外定义了求树的高度的函数:

template<class ElemType>
int BinaryTree<ElemType>::heighttree()
{
	return height(root);
}

template<class ElemType>
int BinaryTree<ElemType>::height(CSNode<ElemType>* p) const
{
	int maxHeight = 0;
	if (!p) 
	{
		return 0;
	}
	if (!p->firstchild)//结点是叶子结点的情况
	{
		return 1;
	}
	for (CSNode<ElemType>* pNode = p->firstchild; pNode; pNode = pNode->nextsibling)//结点是有多个孩子的情况
	{
		int h = height(pNode) + 1;
		if (h > maxHeight)
		{
			maxHeight = h;
		}
	}
	return maxHeight;
}

height()函数中先用两个if条件考虑了结点为空或者是叶子结点的情况,后结合了递归和for循环,层层递归得到树的高度并返回。

3、在模板外定义了求树的度的函数:

template<class ElemType>
int BinaryTree<ElemType>::degreetree()
{
	return Degree(root);
}

template<class ElemType>
int BinaryTree<ElemType>::Degree(CSNode<ElemType>* p) const
{
	int num = 0;
	if (!p)//递归出口一 -- 结点为空
	{
		return 0;
	}
	if (!p->firstchild)//递归出口二 -- 结点为叶子结点
	{
		return 1;
	}
	for (CSNode<ElemType>* pNode = p->firstchild; pNode; pNode = pNode->nextsibling)//处理自身,计算自己孩子的个数 -- 结点有孩子
	{
		num++;
	}
	for (CSNode<ElemType>* pNode = p->firstchild; pNode; pNode = pNode->nextsibling)//计算自己的孩子的度,并与自己的度相比较,返回最大度
	{
		int num1 = Degree(pNode);
		if (num1 > num)
		{
			num = num1;
		}
	}
	return num;
}
4、测试代码
int main()
{
	BinaryTree<char> Tree1;
	cout << "测试实例1:输入结点建立树:" << endl;
	Tree1.createTree();
	cout << "树的高度为:" << Tree1.heighttree() << endl;
	cout << "树的度为:" << Tree1.degreetree() << endl;


	cout << "测试实例2:输入结点建立树:" << endl;
	Tree1.createTree();
	cout << "树的高度为:" << Tree1.heighttree() << endl;
	cout << "树的度为:" << Tree1.degreetree() << endl;

	Tree1.destoryTree();
	system("pause");
	return 1;
}

1.4 测试情况

1、测试实例:

2、测试运行结果:

完整代码可看资源区,在实验一的c6-tree文件夹中

创作不易~麻烦点个赞~~谢谢大家~~~ 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值