遍历二叉树——递归遍历

“test.cpp”

<strong><span style="font-size:18px;">#include<iostream>
using namespace std;

template<class T>
struct BinaryTreeNode
{
	T _data;
	BinaryTreeNode<T>* _left;
	BinaryTreeNode<T>* _right;

	BinaryTreeNode(const T& data = T())
		:_data(data)
		,_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 index = 0;
		_root = _GreateTree(arr,size,index,invalid);
	}
	~BinaryTree()
	{
		_Destory(_root);
	}

	void PrevOrder()
	{
		_PrevOrder(_root);
		cout<<endl;
	}
	void InOrder()
	{
		_InOrder(_root);
		cout<<endl;
	}
	void PostOrder()
	{
		_PostOrder(_root);
		cout<<endl;
	}
	size_t Size()
	{
		size_t count = _Size(_root);
		return count;
	}
	size_t Depth()
	{
		size_t depth = _Depth(_root);
		return depth;
	}
	size_t LeafSize()
	{
		size_t count = _LeafSize(_root);
		return count;
	}
protected:
	size_t _LeafSize(Node* root)
	{
		size_t count = 0;

		if(root->_left == NULL && root->_right == NULL)
		{
			count++;
			return count;
		}
		if(root->_left)
		{
			count += _LeafSize(root->_left);
		}
		if(root->_right)
		{
			count += _LeafSize(root->_right);
		}
		return count;
	}
	size_t _Depth(Node* root)
	{
		if(root == NULL)
		{
			return 0;
		}

		size_t leftcount = _Depth(root->_left) + 1;
		size_t rightcount = _Depth(root->_right) + 1;

		return leftcount > rightcount ? leftcount : rightcount;
	}
	size_t _Size(Node* root)
	{
		if(root == NULL)
		{
			return 0;
		}
		
		return 1 + _Size(root->_left) + _Size(root->_right);
	}
	void _Destory(Node* root)
	{
		if(root == NULL)
		{
			return;
		}

		Node* del = root;
	
		_Destory(root->_left);
		_Destory(root->_right);
		delete del;
	}
	void _PostOrder(Node* root)
	{
		if(root == NULL)
		{
			return;
		}

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

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

		cout<<root->_data<<" ";
		_PrevOrder(root->_left);
		_PrevOrder(root->_right);
	}
	Node* _GreateTree(const T* arr,size_t size,size_t& index,const T& invalid)
	{
		Node* root = NULL;

		if((index < size) && (arr[index] != invalid))
		{
			root = new Node(arr[index]);

			root->_left = _GreateTree(arr,size,++index,invalid);
			root->_right = _GreateTree(arr,size,++index,invalid);
		}

		return root;
	}
private:
	Node* _root;
};
void test()
{
	int arr[10] = {1,2,3,'#','#',4,'#','#',5,6};
	int size = sizeof(arr)/sizeof(arr[0]);

	BinaryTree<int> bt(arr,size,'#');
	bt.PrevOrder();
	bt.InOrder();
	bt.PostOrder();
	cout<<"size: "<<bt.Size()<<endl;
	cout<<"depth: "<<bt.Depth()<<endl;
	cout<<"leafsize: "<<bt.LeafSize()<<endl;
}
int main()
{
	test();
	return 0;
}</span></strong>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值