|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 MFwxy 于 2019-12-9 11:24 编辑
森林.cpp:
#include <iostream>
#include"TForest.h"
using namespace std;
int main()
{
TForest a;
a.Tree_in();
a.choose();
std::cout << "Hello World!\n";
}
TForest.h:
#pragma once
#include<iostream>
#define ElemenType char
using namespace std;
struct FSNode
{
ElemenType data=NULL;
FSNode* firstchild=NULL;
FSNode* nextsibling=NULL;
};
class TForest
{
public:
TForest();
virtual ~TForest();
//输入数据
void Tree_in();
//各种遍历
void H_BL(FSNode*H);
void B_BL(FSNode* B);
void T_BL(FSNode* T);
bool NodeFound(FSNode* F, ElemenType n); //找到改造后的二叉树中的元素
bool NodeFound2(FSNode* F, ElemenType n, ElemenType m);
//期待中诞生的装载函数:
void GetIn(FSNode* F, ElemenType n, ElemenType m);
void GetIn2(FSNode* F, ElemenType n);
//题目用函数
void choose();
void no1();
void no2();
void no3();
void no4();
void no5();
void no6();
void no7();
private:
FSNode Tdata;
};
TForest.cpp:
#include "TForest.h"
#define maxn 100
TForest::~TForest()
{
}
TForest::TForest()//要完成"读树"以及"转化"成二叉树
{
}
void TForest::H_BL(FSNode* H)
{
cout << H->data << " ";
if (H->firstchild != NULL)
{
H_BL(&(*(H->firstchild)));
}
if (H->nextsibling != NULL)
{
H_BL(&(*(H->nextsibling)));
}
}
void TForest::B_BL(FSNode* B)
{
if (B->firstchild != NULL)
{
B_BL(&(*(B->firstchild)));
}
cout << B->data << " ";
if (B->nextsibling != NULL)
{
B_BL(&(*(B->nextsibling)));
}
}
void TForest::T_BL(FSNode* T)
{
if (T->firstchild != NULL)
{
T_BL(&(*(T->firstchild)));
}
if (T->nextsibling != NULL)
{
T_BL(&(*(T->nextsibling)));
}
cout << T->data << " ";
}
bool TForest::NodeFound(FSNode* F,ElemenType n)
{
if (F->firstchild != NULL)
{
NodeFound(&(*(F->firstchild)), n);
}
if (F->nextsibling != NULL)
{
NodeFound(&(*(F->nextsibling)), n);
}
if (F->data == n)
{
return true;
}
else
{
return false;
}
}
void TForest::GetIn(FSNode* F, ElemenType n,ElemenType m)
{
if (F == NULL)//空就直接上车
{
F = new FSNode;
F->data = n;
F->firstchild = new FSNode;
F->firstchild->data = m;
}
else//非空就放在兄弟节点的位置
{
GetIn(&(*(F->nextsibling)), n,m);
}
}
void TForest::GetIn2(FSNode* F, ElemenType n)
{
if (F == NULL)//空就直接上车
{
F = new FSNode;
F->data = n;
}
else//非空就放在兄弟节点的位置
{
GetIn2(&(*(F->nextsibling)), n);
}
}
bool TForest::NodeFound2(FSNode* F, ElemenType n,ElemenType m)
{
if (F == NULL)
{
return true;
}
else
{
if (F->firstchild != NULL)
{
NodeFound2(&(*(F->firstchild)), n, m);
}
if (F->nextsibling != NULL)
{
NodeFound2(&(*(F->nextsibling)), n, m);
}
if (F->data == n)
{
GetIn2(&(*(F->firstchild)), m);
return true;
}
else
{//别在这里组装,会多次定义的
return false;
}
}
}
void TForest::Tree_in()
{
FSNode *c=NULL;
c = &(this->Tdata);
int i = 0, j = 0;
char a, b;
cout << "input the father and son:(enter '#' and '#' to end)"<<endl;
do
{
cin >> a;
cin >> b;
if (a == '#' || '#' == b)
break;
//此时c没有数据,第一次手动录入
if (NULL == c->data)
c->data = a;
//第一步查找父节点a是否存在,若存在则将b加入到左子树根节点的右节点最右方(如果有的话)
//若不存在,则在当前根的右子树中加入一个a。最后将b加于a左子树根结点处
if (c->data != NULL && NodeFound2(c, a, b))//查找到a就直接接子节点,并且跳过加载a的环节
{//很明显找到了,接好b后直接跳过生成根节点a那一步
}
else
{//没找到a就加载a,并接入子节点.问题是要是c都没有。。。。
GetIn(*(&c->nextsibling), a,b);
}
} while (a != '#' && b != '#');
//数组创建完成之后直接变化成右兄弟左孩子的二叉树
}
void TForest::choose()
{
int aa;
cout << "Please choose the issue you want to solve:" << endl;
label1: cin >> aa;
switch (aa)
{
case 1:
no1();
break;
case 2:
no2();
break;
case 3:
no3();
break;
case 4:
no4();
break;
case 5:
no5();
break;
case 6:
no6();
break;
case 7:
no7();
break;
default:
cout << "Wrong number!\n" << "Please choose again!"<<endl;
goto label1;
break;
}
}
void TForest::no1()
{
cout << "前序遍历:" << endl;
H_BL(&(this->Tdata));
cout << "\n" << "中序遍历:" << endl;
B_BL(&(this->Tdata));
cout << "\n" << "后序遍历:" << endl;
T_BL(&(this->Tdata));
cout << endl;
}
void TForest::no2()
{
}
void TForest::no3()
{
}
void TForest::no4()
{
}
void TForest::no5()
{
}
void TForest::no6()
{
}
void TForest::no7()
{
}
大致问题就是本来想用函数GetIn()将新加入的节点组装到原有叶子上,但是始终不能成功
输入数据:
A B
A C
A D
B E
B F
C G
D H
D I
D J
E K
# #
谢谢大佬!
|
|