二叉树(Binary_tree)C++ 模板

二叉树节点

Binary_node.h

template<typename Entry>
struct Binary_node
{
public:
	Binary_node();
	Binary_node(const Entry &x);
public:
	Entry data;
	Binary_node<Entry> *left;
	Binary_node<Entry> *right;
};

template<typename Entry>
Binary_node<Entry>::Binary_node()
{
	left = right = nullptr;
}

template<typename Entry>
Binary_node<Entry>::Binary_node(const Entry &x)
{
	data = x;
	left = right = nullptr;
}

二叉树类
前序、中序、后序遍历
拷贝构造函数、赋值重载函数、析构函数

#include "Binary_node.h"
#include <stack>
using std::stack;

template<typename Entry>
class Binary_tree
{
public:
	Binary_tree() :root(nullptr), count(0) {}
	Binary_tree(const Binary_tree<Entry> &);
	Binary_tree<Entry> &operator=(const Binary_tree<Entry> &);
	~Binary_tree();
	bool empty() const { return root == nullptr; }
	void preorder(void(*visit)(Entry &));
	void inorder(void(*visit)(Entry &));
	void postorder(void(*visit)(Entry &));
	int size() const { return count; }
	void clear();
	int height() const; //只适用完全二叉树
	void insert(const Entry &);
protected:
	void recursive_preorder(Binary_node<Entry> *, void(*visit)(Entry &));
	void recursive_inorder(Binary_node<Entry> *, void(*visit)(Entry &));
	void recursive_postorder(Binary_node<Entry> *, void(*visit)(Entry &));
	Binary_node<Entry> *recursive_copy(Binary_node<Entry> *sub_root);
	void recursive_clear(Binary_node<Entry> *&);
protected:
	Binary_node<Entry> *root;
	int count;
};

template<typename Entry>
Binary_tree<Entry>::Binary_tree(const Binary_tree<Entry> &original)
{
	root = recursive_copy(original.root);
	count = original.count;
}

template<typename Entry>
Binary_tree<Entry> &Binary_tree<Entry>::operator=(const Binary_tree<Entry> &original)
{
	Binary_tree<Entry> new_copy(original);
	clear();
	root = new_copy.root;
	new_copy.root = nullptr;
	return *this;
}

template<typename Entry>
Binary_tree<Entry>::~Binary_tree()
{
	clear();
}

template<typename Entry>
void Binary_tree<Entry>::preorder(void(*visit)(Entry &))
{
	recursive_preorder(root, visit);
}

template<typename Entry>
void Binary_tree<Entry>::recursive_preorder(Binary_node<Entry> *sub_root, void(*visit)(Entry &))
{
	if (sub_root != nullptr)
	{
		visit(sub_root->data);
		recursive_preorder(sub_root->left, visit);
		recursive_preorder(sub_root->right, visit);
	}
}

template<typename Entry>
void Binary_tree<Entry>::inorder(void(*visit)(Entry &))
{
	recursive_inorder(root, visit);
}

template<typename Entry>
void Binary_tree<Entry>::recursive_inorder(Binary_node<Entry> *sub_root, void(*visit)(Entry &))
{
	if (sub_root != nullptr)
	{
		recursive_inorder(sub_root->left, visit);
		visit(sub_root->data);
		recursive_inorder(sub_root->right, visit);
	}
}

template<typename Entry>
void Binary_tree<Entry>::postorder(void(*visit)(Entry &))
{
	recursive_postorder(root, visit);
}

template<typename Entry>
void Binary_tree<Entry>::recursive_postorder(Binary_node<Entry> *sub_root, void(*visit)(Entry &))
{
	if (sub_root != nullptr)
	{
		recursive_postorder(sub_root->left, visit);
		recursive_postorder(sub_root->right, visit);
		visit(sub_root->data);
	}
}

template<typename Entry>
int Binary_tree<Entry>::height() const
{
	if (count == 0) return -1;
	int i = 0;
	for (int tmp = 1; tmp <= count; tmp *= 2)
		++i;
	return i - 1;
}

template<typename Entry>
void Binary_tree<Entry>::insert(const Entry &x)
{
	if (empty())
	{
		root = new Binary_node<Entry>(x);
		++count;
		return;
	}
	stack<int> numbers;
	int tmpcount = size();
	while (tmpcount > 0)
	{
		if (tmpcount & 1) numbers.push(1);
		else numbers.push(2);
		tmpcount = (tmpcount - 1) / 2;
	}
	Binary_node<Entry> *current = root;
	while (numbers.size() > 1)
	{
		if (numbers.top() == 1) current = current->left;
		if (numbers.top() == 2) current = current->right;
		numbers.pop();
	}
	if (numbers.top() == 1) current->left = new Binary_node<Entry>(x);
	if (numbers.top() == 2) current->right = new Binary_node<Entry>(x);
	++count;
}

template<typename Entry>
void Binary_tree<Entry>::clear()
{
	recursive_clear(root);
	count = 0;
}

template<typename Entry>
void Binary_tree<Entry>::recursive_clear(Binary_node<Entry> *&sub_root)
{
	Binary_node<Entry> *temp = sub_root;
	if (sub_root == nullptr) return;
	recursive_clear(sub_root->left);
	recursive_clear(sub_root->right);
	sub_root = nullptr;
	delete temp;
}

template<typename Entry>
Binary_node<Entry> *Binary_tree<Entry>::recursive_copy(Binary_node<Entry> *sub_root)
{
	if (sub_root == nullptr) return nullptr;
	Binary_node<Entry> *temp = new Binary_node<Entry>(sub_root->data);
	temp->left = recursive_copy(sub_root->left);
	temp->right = recursive_copy(sub_root->right);
	return temp;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值