BinaryTree

/******************************************************************************************
Copyright (c) Bit Software, Inc.(2016), All rights reserved.

Purpose: 实现二叉树的基本操作     

Author: MaJing

Reviser: yyy

Created Time: 2016-9-5
******************************************************************************************/
#pragma once

#include
#include

// 一般二叉树实现接口实现

template
struct BinaryTreeNode
{
	T _data;					// 数据
	BinaryTreeNode* _left;	// 左孩子
	BinaryTreeNode* _right;	// 右孩子

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

template
class BinaryTree
{
public:
	BinaryTree()
		:_root(NULL)
	{}

	BinaryTree(T array[], size_t size)
	{
		// 1.创建二叉树
		int index = 0;
		_CreateTree(_root, array, size, index);
	}

	//
	// 实现深拷贝二叉树
	//
	BinaryTree(const BinaryTree& t)
	{
		this->_root = _CopyTree(t._root);
	}

	/*BinaryTree& operator=(const BinaryTree& t)
	{
		if (this != &t)
		{
			this->Destory();
			this->_root = _CopyTree(t._root);
		}

		return *this;
	}*/

	BinaryTree& operator=(BinaryTree t)
	{
		swap(_root, t._root);
		return *this;
	}

	~BinaryTree()
	{
		Destory();
	}

	// 2.遍历二叉树(前序、中序、后序、层序)
	void PrevOrder()
	{
		cout<<"PrevOrder:";
		_PrevOrder(_root);
		cout<*> q;
		q.push(_root);

		// 队列为空时遍历完成
		while (!q.empty())
		{
			BinaryTreeNode* root = q.front();
			if (root->_left)
			{
				q.push(root->_left);
			}

			if (root->_right)
			{
				q.push(root->_right);
			}

			cout<_data<<" ";
			q.pop();
		}
		cout<*> s;
		if (_root)
		{
			s.push(_root);
		}

		while (!s.empty())
		{
			//
			// 先访问根节点,先入右节点再入左节点,
			// 出栈时才能先访问到左节点,再访问到右节点。
			//
			BinaryTreeNode* root = s.top();
			cout<_data<<" ";
			s.pop();

			if (root->_right)
			{
				s.push(root->_right);
			}

			if (root->_left)
			{
				s.push(root->_left);
			}
		}

		cout<*> s;

		BinaryTreeNode* cur = _root;

		while (cur || !s.empty())
		{
			// 1.先将左节点全部入栈
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}

			//
			// 2.栈不为空时,访问栈顶节点。
			// 将cur指向栈顶节点的右子树,继续中序遍历右子树。
			//
			if (!s.empty())
			{
				BinaryTreeNode* top = s.top();
				cout<_data<<" ";
				s.pop();
				cur = top->_right;
			}
		}

		cout<*> s;
		BinaryTreeNode* cur = _root;
		BinaryTreeNode* prevVisited = NULL;

		while (cur || !s.empty())
		{
			// 入栈做孩子节点
			while (cur)
			{
				s.push(cur);
				cur = cur->_left;
			}

			// 2.右节点为空/之前右节点已经访问过了的时候访问当前的栈顶节点
			BinaryTreeNode* top = s.top();
			if (top->_right == NULL || prevVisited == top->_right)
			{
				cout<_data<<" ";
				s.pop();
				prevVisited = top;
			}
			else
			{
				cur = top->_right;
			}
		}

		cout<* Find(const T& x)
	{
		return _Find(_root, x);
	}

	int Height()
	{
		return _Height(_root);
	}

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

	void Destory()
	{
		_Destory(_root);
		_root = NULL;
	}

protected:
	// 构建二叉树
	void _CreateTree(BinaryTreeNode*& root, T array[], size_t size, int& index)
	{
		if (index < size && array[index] != '#')
		{
			root = new BinaryTreeNode(array[index]);
			_CreateTree(root->_left, array, size, ++index);
			_CreateTree(root->_right, array, size, ++index);
		}
	}

	void _PrevOrder(BinaryTreeNode* root)
	{
		if (root == NULL)
			return;

		cout<_data<<" ";
		_PrevOrder(root->_left);
		_PrevOrder(root->_right);
	}

	void _InOrder(BinaryTreeNode* root)
	{
		if (root == NULL)
			return;

		_InOrder(root->_left);
		cout<_data<<" ";
		_InOrder(root->_right);
	}

	void _PostOrder(BinaryTreeNode* root)
	{
		if (root == NULL)
			return;

		_PostOrder(root->_left);
		_PostOrder(root->_right);
		cout<_data<<" ";
	}

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

		if (root->_data == x)
			return root;

		BinaryTreeNode* ret = NULL;
		if (ret = _Find(root->_left, x))
		{
			return ret;
		}

		if (ret = _Find(root->_right, x))
		{
			return ret;
		}

		return NULL;
	}

	int _Height(BinaryTreeNode* root)
	{
		if (root == NULL)
		{
			return 0;
		}

		int leftHeight = _Height(root->_left) + 1;
		int rightHeight = _Height(root->_right) + 1;

		return leftHeight > rightHeight ? leftHeight : rightHeight;
	}

	int _Size(BinaryTreeNode* root)
	{
		if (root == NULL)
		{
			return 0;
		}

		return _Size(root->_left) + _Size(root->_right) + 1;
	}

	// 拷贝二叉树
	BinaryTreeNode* _CopyTree(BinaryTreeNode* root)
	{
		BinaryTreeNode* newRoot = NULL;
		if (root)
		{
			newRoot = new BinaryTreeNode(root->_data);
			newRoot->_left = _CopyTree(root->_left);
			newRoot->_right = _CopyTree(root->_right);
		}

		return newRoot;
	}


	void _Destory(BinaryTreeNode* root)
	{
		if (root == NULL)
			return;
		
		_Destory(root->_left);
		_Destory(root->_right);

		delete root;
	}

private:
	BinaryTreeNode* _root;	// 根节点
};

// 测试二叉树
void TestBinaryTree()
{
	cout<<"TestBinaryTree:"< tree(array, 10);

	tree.PrevOrder();
	tree.PrevOrder_NonR();
	tree.InOrder();
	tree.InOrder_NonR();
	tree.PostOrder();
	tree.PostOrder_NonR();
	tree.LevelOrder();

	BinaryTreeNode* ret = tree.Find(3);
	cout<<"Find 3?: "<_data< treeCopy1 = tree;
	treeCopy1.PrevOrder();

	BinaryTree treeCopy2;
	treeCopy2 = tree;
	treeCopy2.PrevOrder();
}

// 二叉树三叉链结构
template
struct BinaryTreeNode_P
{
	T _data;							// 数据
	BinaryTreeNode_P* _left;		// 左孩子
	BinaryTreeNode_P* _right;	// 右孩子
	BinaryTreeNode_P* _parent;	// 父节点

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

template
class BinaryTree_P
{
public:
	BinaryTree_P(T array[], size_t size)
	{
		int index = 0;
		_CreateTree(_root, array, size, index);
	}
protected:
	// 构建二叉树
	void _CreateTree(BinaryTreeNode_P*& root, T array[], size_t size, int& index)
	{
		if (index < size && array[index] != '#')
		{
			root = new BinaryTreeNode_P(array[index]);
			_CreateTree(root->_left, array, size, ++index);
			// 更新父节点
			if (root->_left)
				root->_left->_parent = root;

			_CreateTree(root->_right, array, size, ++index);

			// 更新父节点
			if (root->_right)
				root->_right->_parent = root;
		}
	}
private:
	BinaryTreeNode_P* _root;
};

// 测试二叉树
void TestBinaryTree_P()
{
	int array[20] = {1, 2, 3, '#', '#', 4, '#', '#', 5, 6};
	BinaryTree_P tree(array, 10);
}

你可能感兴趣的:(数据结构)