二叉树的基本知识

基本代码:

#pragma once
#include <iostream>
#include <queue>
#include <assert.h>
#include <stack>
using namespace std;

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

	BinaryTreeNode(const T& x)
		: _data(x)
		,_left(NULL)
		, _right(NULL)		
	{}
};

template <class T>
class BinaryTree 
{
	typedef BinaryTreeNode<T> Node;
public:

	BinaryTree()
		:_root(NULL)
	{}

	BinaryTree(T* a, size_t n, const T& invalid)
	{
		size_t index = 0;
		_root = CreateTree(a, n, invalid, index);
	}

	Node* CreateTree(T* a, size_t n, const T& invalid, size_t& index)
	{
		Node* root = NULL;
		if (index < n && a[index] != invalid )
		{
			root = new Node(a[index]);
			root->_left = CreateTree(a, n, invalid, ++index);
			root->_right = CreateTree(a, n, invalid, ++index);
		}
		return root;
	}

	~BinaryTree()
	{
		Destroy(_root);
	}

	void Destroy(Node* root)       //后序删除
	{
		if (root == NULL)
		{
			return;
		}
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}


	//t2(t1)
	BinaryTree(const BinaryTree<T>&  t)	         //拷贝构造
	{
		_root = _Copy(t._root);
	}

	Node* _Copy(Node* root)
	{
		if (root == NULL)
		{
			return NULL;
		}
		Node* newRoot = new Node(root->_data);
		newRoot->_left = _Copy(root->_left);
		newRoot->_right = _Copy(root->_right);
		return newRoot;
	}

	//传统写法
	BinaryTree<T>& operator=(const BinaryTree<T>& t) //赋值运算符的重载
	{
		if (this != &t)
		{
			Destroy(_root);
			_root = _Copy(t._root);
		}
		return *this;
	}


	//现代写法
	/*BinaryTree<T>& operator=(BinaryTree<T> t)//调用拷贝构造函数拷贝t
	{
		swap(_root, t._root);
		return *this;
	}*/

	void PrevOrder()
	{
		return _PrevOrder(_root);
	}

	void InOrder()
	{
		return _InOrder(_root);
	}

	void PostOrder()
	{
		return _PostOrder(_root);
	}

	void LevelOrder()					
	{
		return _LevelOrder();
	}

	void PrevOrderNonR()				//非递归<利用栈>
 	{
		stack<Node*> s;
		Node* cur = _root;

		while (cur || !s.empty())	   //入栈。栈里存的是节点的地址
		{
			while (cur)
			{
				cout << cur->_data << " ";
				s.push(cur);
				cur = cur->_left;
			}
			Node* Front = s.top();
			s.pop();

			//子问题的方式访问右树
			cur = Front->_right;
		}
	}
	void InOrderNonR()
	{
		stack<Node*> s;
		Node* cur = _root;
		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}

			Node* Front = s.top();
			cout << Front->_data << " ";
			s.pop();
			//访问右树,子问题
			cur = Front->_right;
		}
	}
	void PostOrderNonR()
	{
		Node* cur = _root;
		stack<Node*> s;
		Node* prev = NULL;
		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}

			Node* Front = s.top();
			if (Front->_right == NULL || Front->_right== prev)
			{
				cout << Front->_data << " ";
				prev = Front;
				s.pop();
			}
			else
			{
				//子问题
				cur = Front->_right;
			}
		}
		cout << endl;
	}

	size_t GetKLevel(size_t k)						//K层叶子节点数
	{
		assert(k>0);
		return _GetKLevel(_root,k);
	}

	size_t Depth()
	{
		return _Depth(_root);
	}
	size_t Size()
	{
		return _Size(_root);
	}	
	size_t LeafSize()			//求叶子节点数
	{
		return _LeafSize(_root);
	}

	Node* Find(const T& x)
	{
		return _Find(_root, x);
	}
protected:
	void _PrevOrder(Node* root)		//前序
	{
		if (root == NULL)
		{
			return ;
		}
		cout << root->_data << " ";
		_PrevOrder(root->_left);
		_PrevOrder(root->_right);
		
	}
	void _InOrder(Node* root)		//中序
	{
		if (root == NULL)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_data << " ";
		_InOrder(root->_right);
	
	}

	void _PostOrder(Node* root)		//后序
	{
		if (root == NULL)
		{
			return;
		}
		_PostOrder(root->_left);
		_PostOrder(root->_right);
		cout << root->_data << " ";
	}
	//宽度
	//大数

	void _LevelOrder()					  // 层序//思想:用队列的先进先出//在写一遍
	{
		queue<Node*> q;
		if (_root)
		{
			q.push(_root);
		}
		while (!q.empty())
		{
			Node* front = q.front();
			cout << front->_data << " ";
			if (front->_left)
			{
				q.push(front->_left);
			}
			if (front->_right)
			{
				q.push(front->_right);
			}
			q.pop();
		}
		cout << endl;
	}

	bool IsCompleteTree()  //判断是否为完全树		// 层序//思想:用队列的先进先出//在写一遍
	{
		queue<Node*> q;
		if (_root)
		{
			q.push(_root);
		}
		bool tag = true;
		while (!q.empty())
		{
			Node* front = q.front();
			q.pop();
			if (front->_left)
			{
				if (tag == false)
				{
					return false;
				}
				q.push(front->_left);
			}
			else
			{
				tag = false;
			}
			if (front->_right)
			{
				if (tag == false)
				{
					return false;
				}
				q.push(front->_right);
			}
			else
			{
				tag = false;
			}
		}
	}
	size_t _GetKLevel(Node* root, size_t k)			//第K层的节点数
	{
		if (root == NULL)               		  //空树
		{
			return 0;
		}
		if (k == 1)
		{
			return 1;
		}
		return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
	}
	size_t _Depth(Node* root)                        //求二叉树的深度
	{
		if (root == NULL)
		{
			return 0;
		}
		if (root->_left == NULL && root->_right == NULL)
		{
			return 1;
		}
		size_t left = _Depth(root->_left);
		size_t right = _Depth(root->_right);
		return (left > right) ? left + 1 : right + 1;
	}

	size_t _Size(Node* root)		//求总节点个数
	{
		if (root == NULL)
		{
			return 0;
		}
		return _Size(root->_left) + _Size(_root->_right) + 1;
	}

	size_t _LeafSize(Node* root)			//求叶子节点数
	{
		if (root == NULL)					//空树
		{
			return 0;
		}
		else						  //不是空树
		{
			if (root->_left == NULL && root->_right == NULL)
			{
				return 1;
			}
			return _LeafSize(root->_left) + _LeafSize(root->_right);
		}
	}


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

		if (root->_data == x)
		{
			return root;
		}
		Node* ret = _Find(root->_left, x);//要接受返回值
		if (ret)	//ret不等于空说明找到了
		{
			return ret;
		}
		else
		{
			return _Find(root->_right,x);
		}
	}
	bool IsInTree(Node* root, const T& x)   //思考
	{
		if (root == NULL)
		{
			return false;
		}
		if (root->_data == x)
		{
			return true;
		}
		/*if (IsInTree(root->_left,x))
		{
			return true;
		}
		return IsInTree(root->_right,x);	*/
		//或者
		return IsInTree(root->_left, x) || IsInTree(root->_right, x);
	}
	Node* _root;
};
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值