主题:二叉树问题
问好都是不懂的,请各位大虾解解惑
#include <iostream>
const int MAX = 200;
using namespace std;
//定义二叉树的结点
typedef struct BiTNode
{
char D;
struct BiTNode *L, *R;
} BiTNode, *BiTree;//typedef这个东西是个嘛意思,后面那个指针又是咋回事???
//接受输入
char str[MAX];
int i = -1;
//二叉树的建立
void Creat(BiTree &T) //为啥要在T前加&???这样有什么作用???
{
i++;
if (str[i] != '\0')
{
if (str[i] == '#')
{
T = NULL;
}
else
{
T = new BiTNode;
T->D = str[i];
Creat(T->L);
Creat(T->R);
}
}
else
{
T = NULL;
}
}
//先序遍历
void PreOrder(BiTree T) //为什么此处又不用&???
{
if (T)
{
cout << T->D << " ";
PreOrder(T->L);
PreOrder(T->R);
}
}
//中序遍历
void MidOrder(BiTree T)
{
if (T)
{
MidOrder(T->L);
cout << T->D << " ";
MidOrder(T->R);
}
}
//后序遍历
void PostOrder(BiTree T)
{
if (T)
{
PostOrder(T->L);
PostOrder(T->R);
cout << T->D << " ";
}
}
//主函数
int main()
{
BiTree T;//bitree不是个指针变量吗,能定义t这个变量吗??
cout << "建立二叉树:" << endl;
cout << "请输入二叉树的先序遍历序列" << endl;
cout << "空节点用#表示,最大" << MAX << "字符" << endl;
cin >> str;
Creat(T);
cout << "先序遍历输出:";
PreOrder(T);
cout << endl;
cout << "中序遍历输出:";
MidOrder(T);
cout << endl;
cout << "后序遍历输出:";
PostOrder(T);
cout << endl;
return 0;
}
#include <iostream>
const int MAX = 200;
using namespace std;
//定义二叉树的结点
typedef struct BiTNode
{
char D;
struct BiTNode *L, *R;
} BiTNode, *BiTree;//typedef这个东西是个嘛意思,后面那个指针又是咋回事???
//接受输入
char str[MAX];
int i = -1;
//二叉树的建立
void Creat(BiTree &T) //为啥要在T前加&???这样有什么作用???
{
i++;
if (str[i] != '\0')
{
if (str[i] == '#')
{
T = NULL;
}
else
{
T = new BiTNode;
T->D = str[i];
Creat(T->L);
Creat(T->R);
}
}
else
{
T = NULL;
}
}
//先序遍历
void PreOrder(BiTree T) //为什么此处又不用&???
{
if (T)
{
cout << T->D << " ";
PreOrder(T->L);
PreOrder(T->R);
}
}
//中序遍历
void MidOrder(BiTree T)
{
if (T)
{
MidOrder(T->L);
cout << T->D << " ";
MidOrder(T->R);
}
}
//后序遍历
void PostOrder(BiTree T)
{
if (T)
{
PostOrder(T->L);
PostOrder(T->R);
cout << T->D << " ";
}
}
//主函数
int main()
{
BiTree T;//bitree不是个指针变量吗,能定义t这个变量吗??
cout << "建立二叉树:" << endl;
cout << "请输入二叉树的先序遍历序列" << endl;
cout << "空节点用#表示,最大" << MAX << "字符" << endl;
cin >> str;
Creat(T);
cout << "先序遍历输出:";
PreOrder(T);
cout << endl;
cout << "中序遍历输出:";
MidOrder(T);
cout << endl;
cout << "后序遍历输出:";
PostOrder(T);
cout << endl;
return 0;
}