#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct BiTNode {
int data;
int number;
int isAsk;
struct BiTNode * lChild, * rChild, * parent;
}BiTNode,*BiTree;
typedef struct LNode{
BiTree stackData;
struct LNode *next;
}LNode, *LinkStack;
int Push(LinkStack &S, BiTree &bt) {
LinkStack p = (LinkStack)malloc(sizeof(LNode));
S->stackData = bt;
p->next = S;
S = p;
return 1;
}
int Pop(LinkStack &S/*, BiTree &bt*/) {
if (!S->next)
{
cout << "栈已空" << endl;
return 0;
}
LinkStack p = S->next;
//bt = p->stackData;
S->next = p->next;
free(p);
}
BiTree GetTopTree(LinkStack S) {
if (S->next)
{
return S->next->stackData;
}
return NULL;
}
int InitBiTree(BiTree &BT,int e) {
BT = (BiTree)malloc(sizeof(BiTNode));
if (!BT)
{
cout << "空间分配失败" << endl;
exit(OVERFLOW);
}
BT->data = e;
BT->number = 0;
BT->isAsk = 0;
BT->lChild = NULL;
BT->rChild = NULL;
BT->parent = NULL;
}
int CreatBiTree(BiTree &BT) {
//创建一个辅助空间栈,用来创建树
LinkStack S = (LinkStack)malloc(sizeof(LNode));
if (!S)
{
cout << "栈空间分配失败" << endl;
exit(OVERFLOW);
}
BiTree tree = BT;
Push(S, tree);
BiTree t;
//创建剩余结点
int e;
int num = 1;
int yesOrNo;
int nodeNum;
cout << "请输入该树有多少个结点:";
cin >> nodeNum;
loop:while (num<nodeNum)
{
t = (BiTree)malloc(sizeof(BiTNode));
t->isAsk = 0;
t->lChild = NULL;
t->rChild = NULL;
cout << "当前结点标号为"<<tree->number<<",该结点是否有左子树,有请输入1,没有请输入0:";
cin >> yesOrNo;
tree->isAsk = (tree->isAsk + 1) * 10;
if (yesOrNo)
{
cout << "请输入当前结点 " << tree->number << " 的左孩子的值:";
cin >> e;
t->data = e;
t->number = num;
//t->parent = GetTopTree(S);
t->parent = tree;
tree->lChild = t;
tree = t;
num++;
Push(S, tree);
goto loop;
}
loopRC:if (tree->isAsk%10 == 0)
{
cout << "当前结点标号为" << tree->number << ",该结点是否有右子树,有请输入1,没有请输入0:";
cin >> yesOrNo;
tree->isAsk++;
if (yesOrNo)
{
cout << "请输入当前结点 " << tree->number << " 的右孩子的值:";
cin >> e;
t->data = e;
t->number = num;
//t->parent = GetTopTree(S);
t->parent = tree;
tree->rChild = t;
tree = t;
num++;
Push(S, tree);
goto loop;
}
else
{
tree = tree->parent;
Pop(S);
goto loopRC;
}
}
else
{
tree = tree->parent;
Pop(S);
goto loopRC;
}
}
}
void VisitBiTree(BiTree BT) {
cout << "\n**********************************************************\n";
cout << "当前结点的编号为:" << BT->number<<", ";
cout << "数据为:" << BT->data << ",\n";
if (BT->parent)
cout << "当前结点有双亲结点,结点编号为:" << BT->parent->number << ",\n";
else
cout << "当前结点为根节点,无双亲结点\n";
if (BT->lChild)
cout << "当前结点有左孩子结点,结点编号为:" << BT->lChild->number << ",\n";
if (BT->rChild)
cout << "当前结点有右孩子结点,结点编号为:" << BT->rChild->number << ",\n";
}
//先序遍历
void PreOrderBiTree(BiTree BT) {
if (BT)
{
VisitBiTree(BT);
PreOrderBiTree(BT->lChild);
PreOrderBiTree(BT->rChild);
}
}
//中序遍历
void InOrderBiTree(BiTree BT) {
if (BT)
{
InOrderBiTree(BT->lChild);
VisitBiTree(BT);
InOrderBiTree(BT->rChild);
}
}
//后序遍历
void PostOrderBiTree(BiTree BT) {
if (BT)
{
PostOrderBiTree(BT->lChild);
PostOrderBiTree(BT->rChild);
VisitBiTree(BT);
}
}
void main() {
BiTree BT;
int e;
cout << "请输入根结点数据 : ";
cin >> e;
InitBiTree(BT, e);
CreatBiTree(BT);
cout << "\n**********************遍历二叉树开始**********************\n" ;
cout << "\n**********************先序遍历二叉树**********************\n";
PreOrderBiTree(BT);
cout << "\n**********************中序遍历二叉树**********************\n";
InOrderBiTree(BT);
cout << "\n**********************后序遍历二叉树**********************\n";
PostOrderBiTree(BT);
}
二叉树三种遍历(递归)
最新推荐文章于 2024-11-02 16:32:57 发布