标识符树的后序计算表达式的值(运算只涉及+、-、*、/)

  1. 定义二叉树的结构如下:
struct tree            //定义结构体
{int data;             //定义一个整型数据域
struct tree *left;    //定义左子树指针
struct tree *right;   //定义右子树指针
};
typedefstruct tree btnode;  //树的结构类型
typedefstruct *bt;          //定义树结点的指针类型
  1. 把算术表达式2*3+6/3的标识符树存入一维数组
  2. 求标识符的前序遍历、中序遍历和后序遍历的序列
  3. 以后序计算标识符树的值

代码展示

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct CBTree {
	char  data;
	struct CBTree* left;
	struct CBTree* right;
}CBTree;

int n;//字符串长度
CBTree* CreateCBTree(int* data, int pos)
{
	CBTree* newnode;
	if (data[pos] == 0 || pos > n)
		return NULL;
	else 
	{
		newnode = (CBTree*)malloc(sizeof(CBTree));
		newnode->data = data[pos];
		newnode->left = CreateCBTree(data, 2 * pos);
		newnode->right = CreateCBTree(data, 2 * pos + 1);
		return newnode;
	}
}
void DLRPrint(CBTree* pnode)//前序输出结点
{
	if (pnode != NULL)
	{
		printf("%c", pnode->data);
		DLRPrint(pnode->left);
		DLRPrint(pnode->right);
	}
}
void LDRPrint(CBTree* pnode)//中序输出结点
{
	if (pnode != NULL)
	{
		LDRPrint(pnode->left);
		printf("%c", pnode->data);
		LDRPrint(pnode->right);
	}
}

void RDLPrint(CBTree* pnode)//后序输出结点
{
	if (pnode != NULL)
	{
		RDLPrint(pnode->left);
		RDLPrint(pnode->right);
		printf("%c", pnode->data);
	}
}

int count(CBTree* pnode)
{
	int operand1 = 0;
	int operand2 = 0;
	int num = 0;
	int getvalue(int op, int operand1, int operand2);
	if (pnode->left == NULL && pnode->right == NULL)
	{
		num = int(((pnode->data) - '0'));
		return num;
	}
	{
		operand1 = count(pnode->left);
		operand2 = count(pnode->right);
		return getvalue(pnode->data, operand1, operand2);
	}
}

int getvalue(int op, int operand1, int operand2)
{
	switch ((char)op)
	{
	case'*':return(operand1 * operand2);
	case'/':
		if (operand2 != 0)
			return (operand1 / operand2);
		else printf("除数不能为0,数学错误");
		return 0;
	case'+':return(operand1 + operand2);
	case'-':return(operand1 - operand2);
	}
}

void main()
{
	CBTree* root = NULL;
	int result;
	int k = 1;
	int data[100] = { ' ' };
	char ch;
	printf("按层次输入结点数据,以回车键表示结束\n");
	while ((ch = getchar()) != '\n')
		data[k++] = ch;
		data[k] = '\0';
		n = k - 1;
	root = CreateCBTree(data, 1);
	printf("\t\n前序表达式:");
	DLRPrint(root);
	printf("\t\n中序表达式:");
	LDRPrint(root);
	printf("\t\n后序表达式:");
	RDLPrint(root);
	result = count(root);
	printf("\t\n表达式结果为:%d\n", result);
}

代码效果呈现
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是利用标识符后序计算表达式的 Python 代码示例: ```python class Node: def __init__(self, value): self.value = value self.left = None self.right = None def postfix_eval(node): stack = [] if node: postfix_eval(node.left) postfix_eval(node.right) if node.value.isdigit(): stack.append(int(node.value)) else: val1 = stack.pop() val2 = stack.pop() result = do_math(val1, val2, node.value) stack.append(result) return stack.pop() def do_math(val1, val2, operator): if operator == "+": return val1 + val2 elif operator == "-": return val2 - val1 elif operator == "*": return val1 * val2 else: return val2 / val1 # 构建标识符 root = Node("*") root.left = Node("+") root.left.left = Node("3") root.left.right = Node("4") root.right = Node("-") root.right.left = Node("5") root.right.right = Node("2") # 计算表达式 result = postfix_eval(root) print(result) ``` 在这个例子中,我们首先定义了一个 `Node` 类来表示标识符的节点,每个节点有一个和左右子节点。然后我们定义了 `postfix_eval` 函数来计算后序表达式。这个函数使用递归的方式遍历标识符,先计算左子和右子,然后根据当前节点的来进行计算,最后将计算结果压入栈中。 在计算过程中,如果遇到操作数,则将其转换为整数并压入栈中。如果遇到操作符,则从栈中弹出两个操作数,进行计算,并将计算结果压入栈中。最后,栈中只剩下一个元素,即为表达式的结果。 最后,我们构建了一个简单的标识符,并调用 `postfix_eval` 函数来计算表达式。在这个例子中,标识符表示的表达式为 `(3 + 4) * (5 - 2)`,计算结果为 `21`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值