二叉树节点
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;
}