二叉树三种遍历(递归)

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗯嗯,是楠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值