【数据结构】实验作业06-设计算法实现求解森林的高度

forest.h

#pragma once

#ifndef forest_
#define forest_
#include<iostream>
#include<queue>
using namespace std;
template<class T>
struct forestNode {
	T data;
	forestNode<T>* firstson;
	forestNode<T>* nextbrother;
	forestNode(T data, forestNode<T>* firstson = NULL, forestNode<T>*nextbrother = NULL) {
		this->data = data;
		this->firstson = firstson;
		this->nextbrother = nextbrother;
	}
};
template<class T>
class forest
{
public:
	forest();
	~forest();
	forestNode<T>* getFirstTreeRoot();
	void create();
	int LevelTraversal_Height();//层次遍历求高度
	int Recursion_Height();
	void erase(forestNode<T>* t);
	void preorderOutput();
	void postorderOutput();
	
private:
	forestNode<T>* firstTreeRoot;
	forestNode<T>* create(forestNode<T>* t);
	int height(forestNode<T>* t);//递归遍历求高度
	void preorder(forestNode<T>* t);//先序遍历
	void postorder(forestNode<T>* t);//后序遍历
};
#endif


forest.cpp

#include "forest.h"
template<class T>
forest<T>::forest()
{
	firstTreeRoot = NULL;
}
template<class T>
forest<T>::~forest()
{
	erase(firstTreeRoot);
	firstTreeRoot = NULL;
}
template<class T>
forestNode<T>* forest<T>::getFirstTreeRoot()
{
	return firstTreeRoot;
}
template<class T>
void forest<T>::create()
{
	firstTreeRoot = create(firstTreeRoot);
}
template<class T>
int forest<T>::LevelTraversal_Height()
{
	queue<forestNode<T>*>q;
	forestNode<T>* cur;
	q.push(firstTreeRoot);
	int count = 0,len;
	while (true) {
		len = q.size();
		if (len == 0)
			break;
		count++;
		for (int i = 0; i < len; i++) {
			cur = q.front();
			while (cur != NULL) {
				q.push(cur);
				cur = cur->nextbrother;
			}
			q.pop();
		}
		len = q.size();
		for (int i = 0; i < len; i++) {
			cur = q.front();
			cur = cur->firstson;
			if (cur != NULL)
				q.push(cur);
			q.pop();
			
		}

	}
	return count;

	
	

	
	
}
template<class T>
int forest<T>::Recursion_Height()
{
	return height(firstTreeRoot);
}
template<class T>
void forest<T>::erase(forestNode<T>* t)
{
	if (t != NULL) {
		erase(t->firstson);
		erase(t->nextbrother);
		delete t;
	}
}

template<class T>
void forest<T>::preorderOutput()
{
	preorder(firstTreeRoot);
	cout << "\n";
}

template<class T>
void forest<T>::postorderOutput()
{
	postorder(firstTreeRoot);
	cout << "\n";
}

template<class T>
forestNode<T>* forest<T>::create(forestNode<T>* t)
{
	T element;
	cin >> element;
	if (element == '#') {
		return NULL;
	}
	else {
		t = new forestNode<T>(element);
		t->firstson = create(t->firstson);
		t->nextbrother = create(t->nextbrother);
		
		
	}
	return t;
}

template<class T>
int forest<T>::height(forestNode<T>* t)
{
	if (t == NULL) {
		return 0;
	}
	else {
		int firstson_height = height(t->firstson);
		int nextbrother_height = height(t->nextbrother);
		return max(firstson_height + 1, nextbrother_height);
	}
}

template<class T>
void forest<T>::preorder(forestNode<T>* t)
{
	if (t != NULL) {
		cout << t->data << " ";
		preorder(t->firstson);
		preorder(t->nextbrother);
	}
}

template<class T>
void forest<T>::postorder(forestNode<T>* t)
{
	if (t != NULL) {
		postorder(t->firstson);
		cout << t->data << " ";
		postorder(t->nextbrother);
	}
}

test.cpp

#include<iostream>
#include"forest.h"
#include"forest.cpp"
using namespace std;
int main() {
	
	forest<char> f;
	f.create();
	cout << "先序遍历输出结果:";
	f.preorderOutput();
	cout << "后序遍历输出结果:";
	f.postorderOutput();
	cout << "递归遍历求森林的高度为:" << f.Recursion_Height()<<"\n";
	cout << "层次遍历求森林的高度为:" << f.LevelTraversal_Height();

	return 0;
}
//ABEK##F##CG##DH#I#J###L#MN###

在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dsogi-spll(Discrete Stationary Optimal Gradient Iterative - Splitting Proximal-like algorithm)是一种用于求解具有拆分代价函数的离散优化问题的算法。该算法是通过分割变量以及应用次梯度和最优步长的方法,以迭代的方式逼近问题的最优解。 dsogi-spll算法实现步骤如下: 1. 初始化优化变量和算法参数:设定初始变量值和算法参数,如收敛容忍度、最大迭代次数等。 2. 迭代更新:重复以下步骤直到满足停止准则: - 计算次梯度:根据当前变量值,计算次梯度。次梯度是代价函数在当前变量值处的次梯度向量,用于指示函数的凹凸性质。 - 更新变量:根据次梯度和最优步长,更新优化变量。最优步长是基于次梯度和步长参数计算得到的最优步长值。 - 对变量进行拆分:将新的变量拆分为多个子变量,以更好地利用问题的结构和优化求解方式。拆分的方式可以根据问题的特点进行调整。 - 更新子变量:针对每个子变量,根据次梯度和最优步长,更新子变量的值。 - 合并子变量:将更新后的子变量合并为新的优化变量。 - 检查收敛性:根据收敛准则,判断是否满足算法停止条件。如果满足,则停止迭代;否则,继续迭代。 3. 返回最优解:返回迭代过程中得到的最优变量作为离散优化问题的最优解。 dsogi-spll算法可以应用于各种离散优化问题,如图像重建、压缩感知等。通过拆分变量和应用次梯度、最优步长等技术,该算法能够在合理的计算复杂度下,有效地求解这些问题,并得到较好的优化结果。具体的实现细节可以根据具体的问题进行调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值