IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> C++实现树的遍历 -> 正文阅读

[数据结构与算法]C++实现树的遍历

树的遍历

1 遍历方式

1.1 前序遍历

前序遍历定义:在前序遍历中,先访问根节点,然后递归地前序遍历左子树,最后递归地的前序遍历右子树。
前序遍历

1.2 中序遍历

中序遍历定义:在中序遍历中,先递归地中序遍历左子树,然后访问根节点,最后递归地中序遍历右子树。
在这里插入图片描述

1.3 后序遍历

后序遍历定义:在后序遍历中,先递归地后序遍历左子树,然后递归地后序遍历右子树,最后访问根节点。
在这里插入图片描述

1.4 层序遍历

层序遍历定义:按层从左至右
在这里插入图片描述

2 案例

在这里插入图片描述

3 代码实现

#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
//二叉树类
class BinaryTree {
public:
	BinaryTree(string val) {
		this->val = val;
		this->leftChild = nullptr;
		this->rightChild = nullptr;
	}

	void insertLeft(string newNode) {
		if (this->leftChild == nullptr) {
			this->leftChild = new BinaryTree(newNode);
		}
		else {
			BinaryTree* t = new BinaryTree(newNode);
			t->leftChild = this->leftChild;
			this->leftChild = t;
		}
	}

	void insertRight(string newNode) {
		if (this->rightChild == nullptr) {
			this->rightChild = new BinaryTree(newNode);
		}
		else {
			BinaryTree* t = new BinaryTree(newNode);
			t->rightChild = this->rightChild;
			this->rightChild = t;
		}
	}

	BinaryTree* getleftChild() {
		return this->leftChild;
	}

	BinaryTree* getRightChild() {
		return this->rightChild;
	}

	void setRootVal(string val) {
		this->val = val;
	}

	string getRootVal() {
		return this->val;
	}

	string val;
	BinaryTree* leftChild;
	BinaryTree* rightChild;
};

//把数据以二叉树的形式存储
BinaryTree* buildBinaryTree(BinaryTree* root, vector<string>& vec, int i) {
	if (i < vec.size()) {
		if (vec[i] == "#") {
			return nullptr;
		}
		else {
			BinaryTree* root = new BinaryTree(vec[i]);
			root->leftChild = buildBinaryTree(root->leftChild, vec, 2 * i + 1);
			root->rightChild = buildBinaryTree(root->rightChild, vec, 2 * i + 2);
			return root;
		}
	}
	return root;
}

//前序遍历
void preOrder(BinaryTree* root) {
	if (root != nullptr) {            //访问根节点,判断其是否空
		cout << root->val << endl;    //访问根节点, 并输出每个节点的值
		preOrder(root->leftChild);    //递归地前序遍历左子树
		preOrder(root->rightChild);   //递归地前序遍历右子树
	}
}

//中序遍历
void inOrder(BinaryTree* root) {
	if (root != nullptr) {            //访问根节点,判断其是否空
		inOrder(root->leftChild);     //递归地前序遍历左子树
		cout << root->val << endl;    //访问根节点, 并输出每个节点的值
		inOrder(root->rightChild);    //递归地前序遍历右子树
	}
}

//后序遍历
void postOrder(BinaryTree* root) {
	if (root != nullptr) {            //访问根节点,判断其是否空
		postOrder(root->leftChild);   //递归地前序遍历左子树
		postOrder(root->rightChild);  //递归地前序遍历右子树
		cout << root->val << endl;    //访问根节点, 并输出每个节点的值
	}
}

//层序遍历  使用队列完成
void layerOrder(BinaryTree* root) {
	if (root == nullptr) {
		return;
	}
	queue<BinaryTree*> que;
	que.push(root);
	while (!que.empty()) {
		cout << que.front()->val << endl;
		if (que.front()->leftChild != nullptr) {
			que.push(que.front()->leftChild);
		}
		if (que.front()->rightChild != nullptr) {
			que.push(que.front()->rightChild);
		}
		que.pop();
	}
}

void test01() {
	vector<string> myVec = { "书", "第1章", "第2章", "1.1节", "1.2节", "2.1节", "2.2节", "#", "#", "1.2.1节", "1.2.2节", "#", "#", "2.2.1节", "2.2.2节", "#", "#",
			  "#", "#", "#", "#", "#", "#" };
	BinaryTree* root = buildBinaryTree(nullptr, myVec, 0);
	cout << "前序遍历:" << endl;
	preOrder(root);
	cout << endl;
	cout << "中序遍历:" << endl;
	inOrder(root);
	cout << endl;
	cout << "后序遍历:" << endl;
	postOrder(root);
	cout << endl;
	cout << "层序遍历:" << endl;
	layerOrder(root);
	cout << endl;
}

void main() {
	test01();
	system("pause");
}

输出结果:
前序遍历:

第1章
1.1节
1.2节
1.2.1节
1.2.2节
第2章
2.1节
2.2节
2.2.1节
2.2.2节

中序遍历:
1.1节
第1章
1.2.1节
1.2节
1.2.2节

2.1节
第2章
2.2.1节
2.2节
2.2.2节

后序遍历:
1.1节
1.2.1节
1.2.2节
1.2节
第1章
2.1节
2.2.1节
2.2.2节
2.2节
第2章

层序遍历:

第1章
第2章
1.1节
1.2节
2.1节
2.2节
1.2.1节
1.2.2节
2.2.1节
2.2.2节

4 总结

总结:前、中、后序遍历用递归实现,层序遍历用队列

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-09-14 13:36:45  更:2021-09-14 13:37:56 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 13:20:56-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码