树结构及其算法-二叉运算树

目录

树结构及其算法-二叉运算树

C++代码


树结构及其算法-二叉运算树

二叉树的应用实际上相当广泛,例如表达式之间的转换。可以把中序表达式按运算符优先级的顺序建成一棵二叉运算树(Binary Expression Tree,或称为二叉表达式树)。之后按二叉树的特性进行前、中、后序的遍历,即可得到前、中、后序表达式,建立的方法可根据以下两种规则来进行操作:

  1. 考虑表达式中运算符的结合性与优先权,再适当地加上括号。
  2. 由最内层的括号逐步向外,利用运算符当树根,左边操作数当左子树,右边操作数当右子树,其中优先权最低的运算符作为此二叉运算树的树根。

C++代码

cpp 复制代码
#include<iostream>
using namespace std;

struct TreeNode {
	int data;
	TreeNode* leftNode;
	TreeNode* rightNode;
	TreeNode() {
		this->data = ' ';
		this->leftNode = nullptr;
		this->rightNode = nullptr;
	}
	TreeNode(int tempData, TreeNode* tempLeftNode = nullptr, TreeNode* tempRightNode = nullptr) {
		this->data = tempData;
		this->leftNode = tempLeftNode;
		this->rightNode = tempRightNode;
	}
};

namespace Tree {
	TreeNode* CreateExpression(char* sequence, int index, int arraySize) {
		TreeNode* _TreeNode;
		if (sequence[index] == ' ' || index >= arraySize)
			return nullptr;
		else{
			_TreeNode = new TreeNode((int)sequence[index]);
			_TreeNode->leftNode = CreateExpression(sequence, 2 * index, arraySize);
			_TreeNode->rightNode = CreateExpression(sequence, 2 * index + 1, arraySize);
			return _TreeNode;
		}
	}
	void Preorder(TreeNode* tempTree) {
		if (tempTree != nullptr) {
			cout << (char)tempTree->data << " ";
			Preorder(tempTree->leftNode);
			Preorder(tempTree->rightNode);
		}
	}
	void Inorder(TreeNode* tempTree) {
		if (tempTree != nullptr) {
			Inorder(tempTree->leftNode);
			cout << (char)tempTree->data << " ";
			Inorder(tempTree->rightNode);
		}
	}
	void Postorder(TreeNode* tempTree) {
		if (tempTree != nullptr) {
			Postorder(tempTree->leftNode);
			Postorder(tempTree->rightNode);
			cout << (char)tempTree->data << " ";
		}
	}
	int Condition(char tempOperator, int num1, int num2) {
		switch (tempOperator)
		{
		case '*':return (num1 * num2);
		case '/':return (num1 / num2);
		case '+':return (num1 + num2);
		case '-':return (num1 - num2);
		case '%':return (num1 % num2);
		}
	}
	int Answer(TreeNode* tempTreeNode) {
		int num1;
		int num2;
		if (tempTreeNode->rightNode == nullptr && tempTreeNode->leftNode == nullptr)
			return tempTreeNode->data - 48;
		else {
			num1 = Answer(tempTreeNode->leftNode);
			num2 = Answer(tempTreeNode->rightNode);
			return Condition((char)tempTreeNode->data, num1, num2);
		}
	}
};

int main() {
	char data1[]{ ' ', '+', '*', '%', '6', '3', '9', '5' };
	TreeNode* treeNode;
	treeNode = Tree::CreateExpression(data1, 1, 8);
	cout << "前序遍历:" << endl;
	Tree::Preorder(treeNode);
	cout << endl;
	cout << "中序遍历:" << endl;
	Tree::Inorder(treeNode);
	cout << endl;
	cout << "后序遍历:" << endl;
	Tree::Postorder(treeNode);
	cout << endl;
	cout << "二叉运算树结果值:" << endl;
	cout << Tree::Answer(treeNode) << endl;
	return 0;
}

结果输出

相关推荐
сокол7 分钟前
【Python-基础-环境搭建】Ubuntu Python 开发环境搭建:编译、pipenv、pyenv、远程连接
开发语言·python·ubuntu
大侠归来2 小时前
C 语言 | 在函数中操作数组
c语言·c++·算法
liangshanbo12152 小时前
JavaScript 面试题:Object.keys() 的返回顺序是无序的吗?
开发语言·javascript·ecmascript
怕浪猫7 小时前
GEO 优化到底是什么?AI时代内容创作者必须懂的新技能
算法·面试·github
朝朝辞暮i8 小时前
C++ 第 23 课:class —— 开始真正进入面向对象
开发语言·c++·算法
Evand J8 小时前
【MATLAB例程】三维RRT+APF避障路径规划与到达角(AOA)定位算法|三维路径优化与定位仿真例程
算法·matlab·路径规划·代码·定位·rrt·aoa
小师兄吃牛肉9 小时前
C语言篇:宏
c语言·开发语言
郑同学的笔记9 小时前
【c++随笔28】strcmp 和 memcmp对比
开发语言·c++
随意起个昵称9 小时前
【背包dp】输出方案路径
c++·动态规划
言乐610 小时前
Python根据无法识别搜索词找出可能输入内容模型
开发语言·python·django·virtualenv·pygame