遍历二叉树——递归和非递归(栈和队列的应用)实现

"BinaryTree.h"

<strong><span style="font-size:18px;">#pragma once
#include <queue>
#include <stack>

template<class T>
class BinaryTreeNode
{
public:
	T _date;

	BinaryTreeNode<T>* _left;
	BinaryTreeNode<T>* _right;
public:
	BinaryTreeNode(const T& date)
		:_date(date)
		,_left(NULL)
		,_right(NULL)
	{}
};

template<class T>
class BinaryTree
{
	typedef BinaryTreeNode<T> Node;
public:
	BinaryTree()
		:_root(NULL)
	{}
	BinaryTree(const T* arr,size_t size,const T& invalid)
	{
		size_t inder = 0;
		_root = _CreateTree(arr,size,invalid,inder);
	}
	void PrevOrder()//递归 前序遍历
	{
		_PrevOrder(_root);
		cout<<endl;
	}
	void InOrder()//递归 中序遍历
	{
		_InOrder(_root);
		cout<<endl;
	}
	void PostOrder()//递归 后序遍历
	{
		_PostOrder(_root);
		cout<<endl;
	}
	void QueueLevelOrder()//非递归实现(队列的应用)层序遍历
	{
		queue<Node* > q;
		if (_root)
		{
			q.push(_root);
		}

		while (!q.empty())//当队列不为空时进行循环
		{
			Node* _front = q.front();
			q.pop();
			cout<<_front->_date<<" ";

			if (_front->_left)
			{
				q.push(_front->_left);
			}
			if (_front->_right)
			{
				q.push(_front->_right);
			}
		}
		cout<<endl;
	}

	void StackPrevOrder()//非递归实现(栈的应用)前序遍历
	{
		stack<Node* > s;
		if (_root)
		{
			s.push(_root);
		}

		while (!s.empty())
		{
			Node* _top = s.top();
			cout<<_top->_date<<" ";
			s.pop();
			if (_top->_right)
			{
				s.push(_top->_right);
			}
			if (_top->_left)
			{
				s.push(_top->_left);
			}
		}
		cout<<endl;
	}

	void StackInOrder()//非递归实现(栈的应用)中序遍历
	{
		stack<Node* > s;
		if (_root == NULL)
		{
			return;
		}

		Node* cur = _root;

		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}

			Node* _top = s.top();
			cout<<_top->_date<<" ";
			s.pop();

			cur = _top->_right;
		}
		cout<<endl;
	}

	void StackPostOrder()//非递归实现(栈的应用)后序遍历
	{
		stack<Node*> s;
		if (_root == NULL)
		{
			return;
		}
		Node* cur = _root;
		Node* prev = NULL;
		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}

			Node* _top = s.top();
			if (_top->_right == NULL || _top->_right == prev)
			{
				cout<<_top->_date<<" ";
				s.pop();
				prev = _top;
			} 
			else
			{
				cur = _top->_right;
			}
		}
		cout<<endl;
	}
protected:
	void _PostOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}

		_PostOrder(root->_left);
		_PostOrder(root->_right);
		cout<<root->_date<<" ";
	}
	void _InOrder(Node* root)
	{
		if (root == NULL)
		{
			return ;
		}

		_InOrder(root->_left);
		cout<<root->_date<<" ";
		_InOrder(root->_right);
	}
	void _PrevOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}

		cout<<root->_date<< " ";
		_PrevOrder(root->_left);
		_PrevOrder(root->_right);
	}
	Node* _CreateTree(const T* arr,size_t size,const T& invalid,size_t& inder)
	{
		Node* root = NULL;
		if (arr[inder] != invalid && inder < size)
		{
			root = new Node(arr[inder]);
			root->_left = _CreateTree(arr,size,invalid,++inder);
			root->_right = _CreateTree(arr,size,invalid,++inder);
		}
		return root;
	}
private:
	Node* _root;
};

</span></strong>


 

"test.cpp"

<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;
#include "BBinaryTree.h";

void TestBinaryTree()
{
	int arr[10] = {1, 2, 3, '#', '#', 4, '#' , '#', 5, 6};
	size_t size = sizeof(arr)/sizeof(arr[0]);
	BinaryTree<int> b(arr,size,'#');
	cout<<"递归实现:"<<endl;
	cout<<"前序遍历:";
	b.PrevOrder();
	cout<<"中序遍历:";
	b.InOrder();
	cout<<"后序遍历:";
	b.PostOrder();
	cout<<"非递归实现:"<<endl;
	cout<<"层序遍历:";
	b.QueueLevelOrder();
	cout<<"前序遍历:";
	b.StackPrevOrder();
	cout<<"中序遍历:";
	b.StackInOrder();
	cout<<"后序遍历:";
	b.StackPostOrder();
}

int main()
{
	TestBinaryTree();
	system("pause");
	return 0;
}</span></strong>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值