二叉树的c++实现
// 2020/02/20 Tealer.Guo
// 二叉树链表描述
#include <iostream>
#include <queue>
template<typename T>
struct binaryTreeNode {
T element;
binaryTreeNode<T>* left_child; // 左子树
binaryTreeNode<T>* right_child; // 右子树
binaryTreeNode() { left_child = right_child = nullptr; }
binaryTreeNode(const T& this_element) {
element = this_element;
left_child = right_child = nullptr;
}
binaryTreeNode(const T& this_element, binaryTreeNode<T>* this_left_child, binaryTreeNode<T>* this_right_child) {
element = this_element;
left_child = this_left_child;
right_child = this_right_child;
}
};
template<typename T>
class linkedBinaryTree {
public:
linkedBinaryTree() { root = nullptr; tree_size = 0; }
~linkedBinaryTree() { }
bool empty() const { return tree_size == 0; }
int size() const { return tree_size; }
// void erase(binaryTreeNode<T>* this_node) { preOrder(dispose); root = nullptr; tree_size = 0; }
void makeTree(const T& this_element, linkedBinaryTree<T>& left, linkedBinaryTree<T>& right);
void preOrder() { preOrder(this -> root); }
void inOrder() { inOrder(this -> root); }
void postOrder() { postOrder(this -> root); }
void levelOrder() { levelOrder(this -> root); }
private:
binaryTreeNode<T>* root; // 根
int tree_size; // 树的节点个数
void visit(binaryTreeNode<T>* this_node) const { std::cout << this_node -> element << " "; } // 访问element
// void dispose(binaryTreeNode<T>* this_node) { delete this_node; }
void preOrder(binaryTreeNode<T>* this_node); // 前序遍历
void inOrder(binaryTreeNode<T>* this_node); // 中序遍历
void postOrder(binaryTreeNode<T>* this_node); // 后序遍历
void levelOrder(binaryTreeNode<T>* this_node); // 层次遍历
};
template<typename T>
void linkedBinaryTree<T>::preOrder(binaryTreeNode<T>* this_node) {
// 前序遍历
if(this_node != nullptr) {
visit(this_node);
preOrder(this_node -> left_child);
preOrder(this_node -> right_child);
}
}
template<typename T>
void linkedBinaryTree<T>::inOrder(binaryTreeNode<T>* this_node) {
// 中序遍历
if(this_node != nullptr) {
inOrder(this_node -> left_child);
visit(this_node);
inOrder(this_node -> right_child);
}
}
template<typename T>
void linkedBinaryTree<T>::postOrder(binaryTreeNode<T>* this_node) {
// 后序遍历
if(this_node != nullptr) {
postOrder(this_node -> left_child);
postOrder(this_node -> right_child);
visit(this_node);
}
}
template<typename T>
void linkedBinaryTree<T>::levelOrder(binaryTreeNode<T>* this_node) {
// 层次遍历
// 实现每层从左到右遍历元素
std::queue<binaryTreeNode<T>*> q;
while(this_node != nullptr) {
visit(this_node);
// 如果有左右子树则将左右子树加入队列
if(this_node -> left_child != nullptr)
q.push(this_node -> left_child);
if(this_node -> right_child != nullptr)
q.push(this_node -> right_child);
// 提取队列中最前面的元素
if(q.empty() != true)
this_node = q.front();
else
return;
// 使队列中最前面的元素出队列
q.pop();
}
}
template<typename T>
void linkedBinaryTree<T>::makeTree(const T& this_element, linkedBinaryTree<T>& left, linkedBinaryTree<T>& right) {
// 构建二叉树
root = new binaryTreeNode<T>(this_element, left.root, right.root);
tree_size = left.tree_size + right.tree_size + 1;
left.root = right.root = nullptr;
left.tree_size = right.tree_size = 0;
}
int main() {
using namespace std;
linkedBinaryTree<int> a,x,y,z;
y.makeTree(1,a,a);
z.makeTree(2,a,a);
x.makeTree(3,y,z);
y.makeTree(4,x,a);
cout << y.size() << endl;
cout << y.empty() << endl;
y.postOrder();
cout << endl;
y.inOrder();
cout << endl;
y.preOrder();
cout << endl;
y.levelOrder();
}
// 二叉树为
// 4
// /
// 3
// / \
// 1 2