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

目录

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

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;
}

结果输出

相关推荐
wainyz9 分钟前
Java NIO操作
java·开发语言·nio
喵叔哟17 分钟前
重构代码之用委托替代继承
开发语言·重构
lzb_kkk23 分钟前
【JavaEE】JUC的常见类
java·开发语言·java-ee
SEEONTIME23 分钟前
python-24-一篇文章彻底掌握Python HTTP库Requests
开发语言·python·http·http库requests
Zfox_24 分钟前
【Linux】进程信号全攻略(二)
linux·运维·c语言·c++
shymoy29 分钟前
Radix Sorts
数据结构·算法·排序算法
风影小子37 分钟前
注册登录学生管理系统小项目
算法
黑龙江亿林等保39 分钟前
深入探索哈尔滨二级等保下的负载均衡SLB及其核心算法
运维·算法·负载均衡
起名字真南42 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
少年负剑去42 分钟前
第十五届蓝桥杯C/C++B组题解——数字接龙
c语言·c++·蓝桥杯