数据结构-二叉树(包含递归和非递归版本)

本文介绍了一种通用的二叉树模板类实现,包括递归与非递归的先序、中序、后序遍历方法,以及层序遍历等。此外,还提供了创建二叉树、深拷贝、计算节点数量、树深度等功能。
#pragma once

#include <iostream>
#include <assert.h>

#include <stack>

using namespace std;

template <typename T>
struct BinaryTreeNode
{
	T _value;
	BinaryTreeNode* _leftchild;
	BinaryTreeNode* _rightchild;

	BinaryTreeNode(const T& x)
		: _value(x)
		, _leftchild(NULL)
		, _rightchild(NULL)
	{}
};

template <typename T>
class BinaryTree
{
	typedef BinaryTreeNode<T> Node;
public:
	BinaryTree()
		:_root(NULL)
	{}
	BinaryTree(const T* arr, size_t size, const T& invalid=T())
	{
		assert(arr);
		size_t index = 0;
		_root = _CreatTree(arr, size, index, invalid);
	}

	BinaryTree(const BinaryTree<T>& bt)
	{
		_root = _Copy(bt._root);
	}

	BinaryTree<T>& operator=(BinaryTree bt)
	{
		swap(_root, bt._root);
		return *this;
	}

	~BinaryTree()
	{
		_Destroy(_root);
	}

	void PrevOrder()  //先序遍历
	{
		_PrevOrder(_root);
		cout << endl;
	}

	void PrevOrderNonR() //先序非递归
	{
		stack<Node*> s;
		Node* cur = _root;

		while (!s.empty() || cur)
		{
			while (cur)  //吧该数的左字数入栈
			{
				cout << cur->_value << " ";  //访问根节点
				s.push(cur);
				cur = cur->_leftchild;
			}
			Node* top = s.top(); //到这,top的左子树和自己已经访问
			s.pop();

			cur = top->_rightchild; //转化为子问题来解决访问以top节点的右子树
		}
		
		cout << endl;
	}

	void InOrder() //中序遍历
	{
		_InOrder(_root);
		cout << endl;
	}

	void InOrderNonR() //中序非递归
	{
		stack<Node*> s;
		Node* cur = _root;

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

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

			cur = top->_rightchild;
		}
		cout << endl;
	}

	void PostOrder() //后序遍历
	{
		_PostOrder(_root);
		cout << endl;
	}

	void PostOrderNonR() //后序非递归
	{
		stack<Node*> s;
		Node* cur = _root;
		Node* prev = NULL;

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

			Node* top = s.top();

			if (top->_rightchild == NULL || top->_rightchild == prev)
			{
				cout << top->_value << " ";
				s.pop();
				prev = top;
			}
			else
			{
				cur = top->_rightchild;
			}
		}
		cout << endl;
	}

	void LevelOrder() //层序遍历
	{
		queue<Node*> q;
		Node* tmp = _root;

		if (tmp == NULL)
		{
			return;
		}
		q.push(tmp);

		while (!q.empty())
		{
			tmp = q.front();
			cout << tmp->_value << " ";
			q.pop();
			if (tmp->_leftchild != NULL)
			{
				q.push(tmp->_leftchild);		
			}
			if (tmp->_rightchild != NULL)
			{
				q.push(tmp->_rightchild);
			}		
		}
		cout << endl;
	}

	size_t Size()
	{
		return _Size(_root);
	}

	size_t Depth()
	{
		return _Depth(_root);
	}

	size_t GetKLevelSize(size_t k)  //求第k层节点个数(根节点为第1层)
	{
		return _GetKLevelSize(_root,k);
	}

	size_t GetLeafSize()  //求叶子节点的个数
	{
		return _GetLeafSize(_root);
	}

	Node* Find(const T& x)
	{
		return _Find(_root, x);
	}
protected:

	Node* _CreatTree(const T* arr, size_t size, size_t& index, const T& invalid = T())
	{
		Node* tmp = NULL;
		if (index<size && arr[index] != invalid)
		{
			tmp = new Node(arr[index]);  //int arr[15] = { 1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8 };
			tmp->_leftchild = _CreatTree(arr, size, ++index, invalid);
			tmp->_rightchild = _CreatTree(arr, size, ++index, invalid);
		}
	
		return tmp;
	}

	Node* _Copy(Node* root)
	{
		if (root == NULL)
		{
			return NULL;
		}

		Node* newroot = new Node(root->_value);
		newroot->_leftchild = _Copy(root->_leftchild);
		newroot->_rightchild = _Copy(root->_rightchild);

		return newroot;
	}

	void _Destroy(Node* root)
	{
		if (root != NULL)
		{
			_Destroy(root->_leftchild);
			_Destroy(root->_rightchild);
			delete root;
			root = NULL;
		}	
	}

	void _PrevOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		cout << root->_value<<" ";
		_PrevOrder(root->_leftchild);
		_PrevOrder(root->_rightchild);
	}

	void _InOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		_InOrder(root->_leftchild);
		cout << root->_value << " ";
		_InOrder(root->_rightchild);
	}

	void _PostOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		_PostOrder(root->_leftchild);
		_PostOrder(root->_rightchild);
		cout << root->_value << " ";
	}

	size_t _Size(Node* root)
	{
		if (root == NULL)
		{
			return 0;
		}

		return _Size(root->_leftchild) + _Size(root->_rightchild) + 1;
	}

	size_t _Depth(Node* root)
	{
		if (root == NULL)
		{
			return 0;
		}
		size_t left = _Depth(root->_leftchild);
		size_t right = _Depth(root->_rightchild);

		return (left > right ? left : right)+1;
	}

	size_t _GetKLevelSize(Node* root, size_t k)
	{
		if (root == NULL)
		{
			return 0;
		}
		if (k == 1)
		{
			return 1;
		}
		
		return _GetKLevelSize(root->_leftchild,k-1) + _GetKLevelSize(root->_rightchild,k-1);
	}

	size_t _GetLeafSize(Node* root)
	{
		if (root == NULL)
		{
			return 0;
		}

		if ((root->_leftchild == root->_rightchild) && root->_leftchild == NULL)
		{
			return 1;
		}

		return _GetLeafSize(root->_leftchild) + _GetLeafSize(root->_rightchild);
	}

	Node* _Find(Node* root, const T& x)
	{
		if (root == NULL)
		{
			return NULL;
		}

		if (root->_value == x)
		{
			return root;
		}

		Node* tmp = _Find(root->_leftchild, x);
		if (tmp == NULL)
		{
			tmp = _Find(root->_rightchild, x);
		}

		return tmp;
	}
protected:
	Node* _root; //根
};

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值