#ifndef BINARYTREE_H
#define BINARYTREE_H
#include<vector>
#include<iostream>
using namespace std;
template<class ElemType>
struct BinaryTreeNode
{
ElemType data;
BinaryTreeNode<ElemType>* LChild;
BinaryTreeNode<ElemType>* RChild;
BinaryTreeNode<ElemType>* parent;
BinaryTreeNode(ElemType item = ElemType(), BinaryTreeNode<ElemType>* L = nullptr, BinaryTreeNode<ElemType>* R = nullptr, BinaryTreeNode<ElemType>* P = nullptr)
: data(item), LChild(L), RChild(R), parent(P) {}
};
template<class ElemType>
class BinaryTree
{
private:
BinaryTreeNode<ElemType>* root;
void destroy(BinaryTreeNode<ElemType>*& node)
{
if (node != nullptr)
{
destroy(node->LChild);
destroy(node->RChild);
delete node;
node = nullptr;
}
}
void preorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
result.push_back(node->data);
preorder(node->LChild, result);
preorder(node->RChild, result);
}
}
void inorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
inorder(node->LChild, result);
result.push_back(node->data);
inorder(node->RChild, result);
}
}
void postorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
postorder(node->LChild, result);
postorder(node->RChild, result);
result.push_back(node->data);
}
}
public:
BinaryTree() : root(nullptr) {}
~BinaryTree()
{
destroy(root);
}
void createFromLevelOrder(vector<ElemType> elements, ElemType empty)
{
if (elements.empty() || elements[0] == empty)
return;
queue<BinaryTreeNode<ElemType>**> q;
root = new BinaryTreeNode<ElemType>(elements[0]);
q.push(&root);
size_t i = 1;
while (i < elements.size()) {
BinaryTreeNode<ElemType>** parent = q.front();
q.pop();
if (elements[i] != empty) {
(*parent)->LChild = new BinaryTreeNode<ElemType>(elements[i]);
(*parent)->LChild->parent = *parent;
q.push(&((*parent)->LChild));
}
++i;
if (i < elements.size() && elements[i] != empty) {
(*parent)->RChild = new BinaryTreeNode<ElemType>(elements[i]);
(*parent)->RChild->parent = *parent;
q.push(&((*parent)->RChild));
}
++i;
}
}
BinaryTreeNode<ElemType>* convertToLinkedList() {
if (root == nullptr) {
return nullptr;
}
// 找到链表头节点
BinaryTreeNode<ElemType>* head = root;
while (head->LChild != nullptr) {
head = head->LChild;
}
// 中序遍历构建链表
BinaryTreeNode<ElemType>* current = head;
while (current != nullptr) {
if (current->RChild != nullptr) {
BinaryTreeNode<ElemType>* next = current->RChild;
while (next->LChild != nullptr) {
next = next->LChild;
}
next->LChild = current;
}
current = current->RChild;
}
return head;
}
void printPreorder()
{
vector<ElemType> result;
preorder(root, result);
printResult(result);
}
void printInorder()
{
vector<ElemType> result;
inorder(root, result);
printResult(result);
}
bool isBST() {
if (root == nullptr) {
return true;
}
vector<ElemType> result;
inorder(root, result);
for (size_t i = 1; i < result.size(); ++i) {
if (result[i] <= result[i - 1]) {
return false;
}
}
return true;
}
void printPostorder()
{
vector<ElemType> result;
postorder(root, result);
printResult(result);
}
void printResult(const vector<ElemType>& result)
{
if (!result.empty())
{
cout << result[0];
for (size_t i = 1; i < result.size(); ++i)
{
cout << ',' << result[i];
}
}
cout << endl;
}
};
#endif
如果想进一步分解为头文件和源文件
头文件
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <vector>
#include <iostream>
using namespace std;
template<class ElemType>
struct BinaryTreeNode
{
ElemType data;
BinaryTreeNode<ElemType>* LChild;
BinaryTreeNode<ElemType>* RChild;
BinaryTreeNode<ElemType>* parent;
BinaryTreeNode(ElemType item = ElemType(), BinaryTreeNode<ElemType>* L = nullptr, BinaryTreeNode<ElemType>* R = nullptr, BinaryTreeNode<ElemType>* P = nullptr)
: data(item), LChild(L), RChild(R), parent(P) {}
};
template<class ElemType>
class BinaryTree
{
private:
BinaryTreeNode<ElemType>* root;
void destroy(BinaryTreeNode<ElemType>*& node);
void preorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result);
void inorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result);
void postorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result);
public:
BinaryTree();
~BinaryTree();
void createFromLevelOrder(vector<ElemType> elements, ElemType empty);
BinaryTreeNode<ElemType>* convertToLinkedList();
void printPreorder();
void printInorder();
bool isBST();
void printPostorder();
void printResult(const vector<ElemType>& result);
};
#include "BinaryTree.cpp" // 包含实现文件
#endif
源文件
#include "BinaryTree.h"
#include <queue>
template<class ElemType>
BinaryTree<ElemType>::BinaryTree() : root(nullptr) {}
template<class ElemType>
BinaryTree<ElemType>::~BinaryTree()
{
destroy(root);
}
template<class ElemType>
void BinaryTree<ElemType>::destroy(BinaryTreeNode<ElemType>*& node)
{
if (node != nullptr)
{
destroy(node->LChild);
destroy(node->RChild);
delete node;
node = nullptr;
}
}
template<class ElemType>
void BinaryTree<ElemType>::preorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
result.push_back(node->data);
preorder(node->LChild, result);
preorder(node->RChild, result);
}
}
template<class ElemType>
void BinaryTree<ElemType>::inorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
inorder(node->LChild, result);
result.push_back(node->data);
inorder(node->RChild, result);
}
}
template<class ElemType>
void BinaryTree<ElemType>::postorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
postorder(node->LChild, result);
postorder(node->RChild, result);
result.push_back(node->data);
}
}
template<class ElemType>
void BinaryTree<ElemType>::createFromLevelOrder(vector<ElemType> elements, ElemType empty)
{
if (elements.empty() || elements[0] == empty)
return;
queue<BinaryTreeNode<ElemType>**> q;
root = new BinaryTreeNode<ElemType>(elements[0]);
q.push(&root);
size_t i = 1;
while (i < elements.size()) {
BinaryTreeNode<ElemType>** parent = q.front();
q.pop();
if (elements[i] != empty) {
(*parent)->LChild = new BinaryTreeNode<ElemType>(elements[i]);
(*parent)->LChild->parent = *parent;
q.push(&((*parent)->LChild));
}
++i;
if (i < elements.size() && elements[i] != empty) {
(*parent)->RChild = new BinaryTreeNode<ElemType>(elements[i]);
(*parent)->RChild->parent = *parent;
q.push(&((*parent)->RChild));
}
++i;
}
}
template<class ElemType>
BinaryTreeNode<ElemType>* BinaryTree<ElemType>::convertToLinkedList() {
if (root == nullptr) {
return nullptr;
}
BinaryTreeNode<ElemType>* head = root;
while (head->LChild != nullptr) {
head = head->LChild;
}
BinaryTreeNode<ElemType>* current = head;
while (current != nullptr) {
if (current->RChild != nullptr) {
BinaryTreeNode<ElemType>* next = current->RChild;
while (next->LChild != nullptr) {
next = next->LChild;
}
next->LChild = current;
}
current = current->RChild;
}
return head;
}
template<class ElemType>
void BinaryTree<ElemType>::printPreorder()
{
vector<ElemType> result;
preorder(root, result);
printResult(result);
}
template<class ElemType>
void BinaryTree<ElemType>::printInorder()
{
vector<ElemType> result;
inorder(root, result);
printResult(result);
}
template<class ElemType>
bool BinaryTree<ElemType>::isBST() {
if (root == nullptr) {
return true;
}
vector<ElemType> result;
inorder(root, result);
for (size_t i = 1; i < result.size(); ++i) {
if (result[i] <= result[i - 1]) {
return false;
}
}
return true;
}
template<class ElemType>
void BinaryTree<ElemType>::printPostorder()
{
vector<ElemType> result;
postorder(root, result);
printResult(result);
}
template<class ElemType>
void BinaryTree<ElemType>::printResult(const vector<ElemType>& result)
{
if (!result.empty())
{
cout << result[0];
for (size_t i = 1; i < result.size(); ++i)
{
cout << ',' << result[i];
}
}
cout << endl;
}