数据结构课设(C++)——二叉树的遍历

对任意给定的二叉树(顶点数自定)建立它的二叉链表存贮结构,并利用栈的五种基本运算(置空栈、进栈、出栈、取栈顶元素、判栈空)实现二叉树的先序、中序、后序三种遍历,输出三种遍历的结果。

#include<iostream>
using namespace std;

#define Stack_Size 50
typedef struct Node
{
	char data;
	struct Node* LChild;
	struct Node* RChild;
}BiTNode, * BiTree;
typedef struct
{
	BiTree elem[Stack_Size];
	int top;
}SeqStack;
void CreateBiTree(BiTree* T)//二叉树的建立
{
	char data;
	cin >> data;
	if (data == '#')
		*T = NULL;
	else
	{
		*T = new BiTNode;
		if (!(*T))
		{
			cout << "内存分配失败" << endl;
			exit(-1);
		}
		(*T)->data = data;
		cout << "输入" << data << "的左子树:";
		CreateBiTree(&(*T)->LChild);
		cout << "输入" << data << "的右子树:";
		CreateBiTree(&(*T)->RChild);
	}
}
void initStack(SeqStack* s)//初始化
{
	for (int i = 0;i < Stack_Size;i++)
	{
		s->elem[i] = new BiTNode;
		s->elem[i]->LChild = NULL;
		s->elem[i]->RChild = NULL;
	}
	s->top = -1;
}
void push(SeqStack* s, BiTree x)//入栈
{
	SeqStack* temp = new SeqStack;
	temp = s;
	if (temp->top == Stack_Size - 1)
		return;
	temp->top++;
	temp->elem[temp->top] = x;
}
void pop(SeqStack* s, BiTree* P)//出栈
{
	if (s->top == -1)
		return;
	else
	{
		*(P) = s->elem[s->top];
		s->elem[s->top] = new BiTNode;
		s->elem[s->top]->LChild = NULL;
		s->elem[s->top]->RChild = NULL;
		s->top--;
	}
}
void getTop(SeqStack* s, BiTree* T)//获取栈顶元素
{
	*T = s->elem[s->top];
}
bool isempty(SeqStack* s)//判空
{
	if (s->top == -1)
		return true;
	else
		return false;
}
bool isfull(SeqStack* s)//判满
{
	if (s->top == Stack_Size - 1)
		return true;
	else
		return false;
}
void empty(SeqStack* s)//置空
{
	for (int i = 0;i < s->top;i++)
		s->elem[i] = NULL;
	s->top = -1;
}
void perOrder(BiTree T)//先序遍历
{
	SeqStack* s = new SeqStack;
	initStack(s);
	BiTree temp = T;
	while (temp != NULL || !isempty(s))
	{
		if (temp != NULL)
		{
			push(s, temp);
			cout << temp->data << " ";
			temp = temp->LChild;
		}
		else
		{
			pop(s, &temp);
			temp = temp->RChild;
		}
	}
	cout << endl;
}
void inOder(BiTree T)//中序遍历
{
	SeqStack* s = new SeqStack;
	initStack(s);
	BiTree P = new BiTNode;
	P = T;
	while (P || s->top != -1)
	{
		if (P)
		{
			push(s, P);
			P = P->LChild;
		}
		else
		{
			pop(s, &P);
			cout << P->data << " ";
			P = P->RChild;
		}
	}
	cout << endl;
}
void postOrder(BiTree T)//后序遍历
{
	SeqStack* s = new SeqStack;
	initStack(s);
	BiTree temp = T;
	int flagStack[Stack_Size] = { 0 };//记录每个节点访问次数的栈
	while (temp != NULL || !isempty(s))
	{
		if (temp != NULL) 
		{   //第一次访问,flag置1,入栈
			push(s, temp);
			flagStack[s->top] = 1;
			temp = temp->LChild;
		}
		else 
		{
			if (flagStack[s->top] == 1) 
			{  //第二次访问,flag置2,取栈顶元素但不出栈
				getTop(s, &temp);
				flagStack[s->top] = 2;
				temp = temp->RChild;
			}
			else 
			{   //第三次访问,出栈
				pop(s, &temp);
				cout << temp->data << " ";//出栈时,访问输出
				temp = NULL;      //p置空,以便继续退栈
			}
		}
	}
	cout << endl;
}
void menu()
{
	cout << "1.建立二叉树" << endl;
	cout << "2.先序遍历" << endl;
	cout << "3.中序遍历" << endl;
	cout << "4.后序遍历" << endl;
	cout << "0.退出程序" << endl;
}
int main()
{
	int flag = 1, x = 0;
	BiTree T = new BiTNode;
	while (flag)
	{
		menu();
		cin >> x;
		switch (x)
		{
		case 1:cout << "输入根节点:";
			CreateBiTree(&T);
			break;
		case 2:perOrder(T);
			break;
		case 3:inOder(T);
			break;
		case 4:postOrder(T);
			break;
		case 0:flag = 0;
			break;
		}
	}
	return 0;
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值