二叉树四种遍历的实例实现

内容:

二叉树初始化(先序输入)

层序遍历(非递归的队列方式)

先序遍历(递归)

中序遍历(递归)

后序遍历(递归)

代码:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#define nodedata char

using namespace std;
typedef struct treenode
{
	nodedata data;
	treenode* lchild;
	treenode* rchild;
	//treenode* parent;

}treenode, *tree;
#define quedata treenode* 
typedef struct quenode
{
	quedata data;
	quenode* next;
}quenode;
typedef struct queque
{
	quenode* head;

	quenode* tail;

}queue;
void _init(queue &Q)
{
	//Q.head = NULL;
	Q.head = (quenode*)malloc(sizeof(quenode));
	Q.tail = Q.head;
}
bool _empty(queue Q)
{
	if (Q.head == Q.tail)return true;
	else
		return false;
}
quedata firstnode(queue Q)
{
	if(!_empty(Q))
	return Q.head->next->data;
}
void qpush(queue &Q, quedata e)
{
	quenode* s;
	s = (quenode*)malloc(sizeof(quenode));
	s->data = e;
	s->next = NULL;
	Q.tail->next = s;
	Q.tail = s;

}
quedata qpop(queue &Q)
{
	if (Q.head == Q.tail)return false;
	
	quenode* s;
	s = (quenode*)malloc(sizeof(quenode));
	s = Q.head->next;
	quedata e = s->data;
	Q.head->next=s->next;
	if (Q.tail == s)Q.tail = Q.head;
	free(s);
	
	return e;
}


void initroot(tree &root)
{
	root = NULL;
}
void  initchild(tree &root)//前序输入初始化
{

	
	
	nodedata data1;
	cin >> data1;
	if (data1 == '#')root = NULL;
	else{
		
		
		root = (treenode*)malloc(sizeof(treenode));
	
		root->data = data1;
		initchild(root->lchild);
		initchild(root->rchild);

			
		}

		
}
	
	


void visitallpre(tree tree1)//前序
{
	
	

	
	if (tree1 != NULL)
	{
		
		cout << tree1->data ;
		
		visitallpre(tree1->lchild);
		//cout << "\t\t\t";
		
		visitallpre(tree1->rchild);
	}



}
void visitallmid(tree tree1)//中
{

	

	if (tree1 != NULL)
	{

		

		visitallmid(tree1->lchild);
		//cout << "\t\t\t";
		cout << tree1->data;
		visitallmid(tree1->rchild);
	}



}
void visitallaft(tree tree1)//后序
{

	

	if (tree1 != NULL)
	{
		visitallaft(tree1->lchild);
		

		
		//cout << "\t\t\t";

		visitallaft(tree1->rchild);
		cout << tree1->data;
	}



}
void visitallfloor(tree tree1)//层次
{

	queue Q;
	_init(Q);
	if (tree1!=NULL)
	{
		qpush(Q, tree1);
	}
	while (!_empty(Q))
	{
		
		
		cout << firstnode(Q)->data;
		if (firstnode(Q)->lchild!=NULL)
		{
			qpush(Q, firstnode(Q)->lchild);
		}
		if (firstnode(Q)->rchild != NULL)
		{
			qpush(Q, firstnode(Q)->rchild);
		}
		qpop(Q);
	}


}

int main()
{

	tree testtree;
	initroot(testtree);
	cout << "测试树前序序列为:ABD##E##CF###(#代表空)" << endl;
	cout << "前序输入初始化" << endl;
	initchild(testtree); 
	cout <<"\n";
	cout << "前序遍历:" << endl;
	visitallpre(testtree);
	cout << "\n";
	cout << "中序遍历:" << endl;
	visitallmid(testtree);
	cout << "\n";
	cout << "后序遍历:" << endl;
	visitallaft(testtree);
	cout << "\n";
	
	cout << "层次遍历:" << endl;
	visitallfloor(testtree);
	cout << "\n";
}

程序截图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值