二叉树遍历
递归算法
//
// Created by Administrator on 2021/10/25.
//
#ifndef TEST_BINARYTREE_H
#define TEST_BINARYTREE_H
#include <iostream>
using namespace std;
typedef char ElemType;
typedef struct BiNode {
ElemType data;
BiNode *left;
BiNode *right;
BiNode(ElemType val) {
data = val;
left = nullptr;
right = nullptr;
}
} BiNode, *BiTree;
class BinaryTree {
public:
void Create();
int getSize();
int getHeight();
void preOrder();
void inOrder();
void postOrder();
void destroy();
private:
BiTree create();
void preOrder(BiTree root);
void inOrder(BiTree root);
void postOrder(BiTree root);
void destroy(BiTree root);
int getHeight(BiTree root);
void AddNode(ElemType key, int direction, BiTree root);
BiTree m_root;
int size;
};
#endif //TEST_BINARYTREE_H
//
// Created by Administrator on 2021/10/25.
//
#include "BinaryTree.h"
void BinaryTree::Create() {
size = 0;
m_root = create();
}
int BinaryTree::getSize() {
return size;
}
int BinaryTree::getHeight() {
return getHeight(m_root);
}
void BinaryTree::preOrder() {
cout << "先序遍历:" << endl;
preOrder(m_root);
cout << endl;
}
void BinaryTree::inOrder() {
cout << "中序遍历:" << endl;
inOrder(m_root);
cout << endl;
}
void BinaryTree::postOrder() {
cout << "后序遍历:" << endl;
postOrder(m_root);
cout << endl;
}
void BinaryTree::destroy() {
destroy(m_root);
}
BiTree BinaryTree::create() {
BiTree current = nullptr;
ElemType val;
// 输入值
cin >> val;
if (val == '#') {
// 当前子树为空
return nullptr;
} else {
// 递归创建左右子树
size++;
current = new BiNode(val);
current->left = create();
current->right = create();
return current;
}
}
void BinaryTree::preOrder(BiTree root) {
if (root == nullptr)
return;
else {
cout << root->data << " --> "; // 先打印根节点
preOrder(root->left); // 遍历左子树
preOrder(root->right); // 遍历右子树
}
}
void BinaryTree::inOrder(BiTree root) {
if (root == nullptr)
return;
else {
inOrder(root->left);
cout << root->data << " --> ";
inOrder(root->right);
}
}
void BinaryTree::postOrder(BiTree root) {
if (root == nullptr)
return;
else {
postOrder(root->left);
postOrder(root->right);
cout << root->data << " --> ";
}
}
void BinaryTree::destroy(BiTree root) {
if (root) {
destroy(root->left);
destroy(root->right);
// 释放节点
delete root;
root = nullptr;
size = 0;
}
}
/**
*
* @param root
* @return 递归得到树高
*/
int BinaryTree::getHeight(BiTree root) {
if (root == nullptr)
return 0;
int left_height = getHeight(root->left);
int right_height = getHeight(root->right);
return (left_height > right_height) ? (left_height + 1) : (right_height + 1);
}
/**
* 前序遍历的方式创建
* @param key 值
* @param direction 0是从左子树插入,1是从右子树插入
* @param root 被插入的根节点
*/
void BinaryTree::AddNode(const ElemType key, int direction, BiTree root) {
if (direction == 0) {
// 插入到左子树
if (root->left == nullptr)
root->left = new BiNode(key);
else
AddNode(key, direction, root->left);
} else if (direction == 1) {
// 插入到右子树
if (root->right == nullptr)
root->right = new BiNode(key);
else
AddNode(key, direction, root->right);
}
}
#include <iostream>
#include "BinaryTree.h"
using namespace std;
int main() {
BinaryTree tree{};
cout << "按先序遍历方式创建树:" << endl;
// ABCD##E##FG###HI#J##K##
tree.Create();
cout << "树的高度为:" << tree.getHeight() << endl;
cout << "树的节点为:" << tree.getSize() << endl;
tree.preOrder();
tree.inOrder();
tree.postOrder();
tree.destroy();
return 0;
}