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