目录
- 一、二叉树的结构
- 二、二叉树的遍历方式
- 三、源码
一、二叉树的结构
二、二叉树的遍历方式
- 先序遍历: 根–>左–>右
- 中序遍历: 左–>根–>右
- 后序遍历:左–>右–>根
- 层次遍历:顶层–>底层
三、源码
注:关于二叉树中先序、中序和后序遍历算法的实现暂时只用了递归方式,后去会补充非递归的实现方式。
/*本项目主要用于二叉树的基础遍历算法测试。1.binarytree.h包含对二叉树的结构体定义以及二叉树的先序、中序、后序以及层次遍历。2.binarytree.cpp包含对各个遍历算法的实现。3.main.cpp包含了对于二叉树遍历算法的测试。
*/
binarytree.h
#ifndef BINARYTREE_H
#define BINARYTREE_H#include <QObject>// 二叉树结点定义
struct TreeNode {int data;TreeNode* left;TreeNode* right;TreeNode(int val) : data(val), left(nullptr), right(nullptr) {}
};// 二叉树类
class BinaryTree : public QObject{Q_OBJECT
public:TreeNode* root;int seq;public:BinaryTree();/* 二叉树节点插入 */void insertNode(int val);/* 初始化二叉树 */void initBinaryTree();/* 二叉树先序遍历 */void preOrderTravel(TreeNode* node);/* 二叉树中序遍历 */void inOrderTravel(TreeNode* node);/* 二叉树后序遍历 */void postOrderTravel(TreeNode* node);/* 二叉树层次遍历 */void levelOrderTravel();
};#endif // BINARYTREE_H
binarytree.cpp
#include "binarytree.h"
#include <iostream>
#include <queue>using namespace std;BinaryTree::BinaryTree()
{root = nullptr;seq = 0;
}
// 插入结点
void BinaryTree::insertNode(int val) {TreeNode* newNode = new TreeNode(val);if (root == nullptr) {root = newNode;return;}TreeNode* curr = root;while (true) {if (val < curr->data) {if (curr->left == nullptr) {curr->left = newNode;break;} else {curr = curr->left;}} else {if (curr->right == nullptr) {curr->right = newNode;break;} else {curr = curr->right;}}}
}void BinaryTree::initBinaryTree()
{int _data[7] = {4,2,6,1,3,5,7};cout<<"初始化节点序列:";for(int i = 0;i < 7;i ++){insertNode(_data[i]);cout<<_data[i]<<" ";}cout<<endl;
}void BinaryTree::preOrderTravel(TreeNode *node)
{if (node == nullptr)return;cout<<"第"<<seq++<<"个节点:"<<node->data<<endl;if(node->left != nullptr)preOrderTravel(node->left);if(node->right != nullptr)preOrderTravel(node->right);
}void BinaryTree::inOrderTravel(TreeNode *node)
{if (node == nullptr)return;if(node->left != nullptr)inOrderTravel(node->left);cout<<"第"<<seq++<<"个节点:"<<node->data<<endl;if(node->right != nullptr)inOrderTravel(node->right);
}void BinaryTree::postOrderTravel(TreeNode *node)
{if (node == nullptr)return;if(node->left != nullptr)postOrderTravel(node->left);if(node->right != nullptr)postOrderTravel(node->right);cout<<"第"<<seq++<<"个节点:"<<node->data<<endl;
}void BinaryTree::levelOrderTravel()
{if (root == nullptr)return;queue<TreeNode*> _q;_q.push(root);while(!_q.empty()){TreeNode* child = _q.front();cout<<"第"<<seq++<<"个节点:"<<child->data<<endl;_q.pop();if(child->left != nullptr)_q.push(child->left);if(child->right != nullptr)_q.push(child->right);}cout<<endl;
}
main.cpp
#include <QCoreApplication>
#include <iostream>
#include <binarytree.h>using namespace std;int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);BinaryTree *mTree = new BinaryTree;cout<<"开始初始化二叉树!"<<endl;mTree->initBinaryTree();//初始化二叉树cout<<"开始执行二叉树的先序遍历!"<<endl;mTree->preOrderTravel(mTree->root);mTree->seq = 0;cout<<"开始执行二叉树的中序遍历!"<<endl;mTree->inOrderTravel(mTree->root);mTree->seq = 0;cout<<"开始执行二叉树的后序遍历!"<<endl;mTree->postOrderTravel(mTree->root);mTree->seq = 0;cout<<"开始执行二叉树的层次遍历!"<<endl;mTree->levelOrderTravel();delete mTree;mTree = nullptr;return a.exec();
}
效果如下图: