下面是一个例子,它使用C语言实现了二叉树来表示表达式并求值的功能:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// 定义表达式二叉树的结点结构
typedef struct node {
char data; // 结点的数据域,用来存储运算符或者操作数
struct node *left; // 左子树指针
struct node *right; // 右子树指针
} Node;
// 创建表达式二叉树的函数
Node *create_expression_tree(const char *expression) {
// 定义栈,用来存储结点
Node *stack[100];
int top = -1; // 栈顶指针
// 遍历表达式的每一个字符
for (int i = 0; expression[i] != '\0'; i++) {
// 如果是数字,创建一个结点并将其压入栈中
if (isdigit(expression[i])) {
Node *node = (Node *) malloc(sizeof(Node));
node->data = expression[i];
node->left = node->right = NULL;
stack[++top] = node;
} else {
// 如果是运算符,从栈中弹出两个结点作为运算符的两个操作数
Node *node = (Node *) malloc(sizeof(Node));
node->data = expression[i];
node->right = stack[top--];
node->left = stack[top--];
// 将新创建的结点压入栈中
stack[++top] = node;
}
}
// 返回表达式二叉树的根结点
return stack[0];
}
// 使用后缀表达式的方式遍历二叉树并求值的函数
int evaluate(Node *root) {
// 如果是叶子结点,直接返回数据域中存储的数字
if (root->left == NULL && root->right == NULL) {
return root->data - '0';
}
// 如果是二元运算符,则递归计算左右子树的值并进行计算
int left = evaluate(root->left);
int right = evaluate(root->right);
switch (root->data) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return left / right;
}
// 其他情况返回0
return 0;
}
int main() {
// 创建表达式二叉树
Node root = create_expression_tree("9+52-4/2");
// 计算并打印结果
int result = evaluate(root);
printf("Result: %d\n", result);
return 0;
}
希望Al有所帮助
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Define a node in the binary tree
typedef struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// Function prototypes
TreeNode *createNode(char data);
void insertNode(TreeNode **root, char data);
int evaluateExpression(TreeNode *root);
void freeTree(TreeNode *root);
int main() {
// Create an empty binary tree
TreeNode *root = NULL;
// Read in the expression
char expression[100];
printf("Enter an expression: ");
scanf("%s", expression);
// Build the binary tree by inserting each character of the expression
for (int i = 0; i < strlen(expression); i++) {
insertNode(&root, expression[i]);
}
// Evaluate the expression and print the result
int result = evaluateExpression(root);
printf("Result: %d\n", result);
// Free the tree
freeTree(root);
return 0;
}
// Function to create a new tree node
TreeNode *createNode(char data) {
TreeNode *node = (TreeNode*) malloc(sizeof(TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
// Function to insert a new node into the binary tree
void insertNode(TreeNode **root, char data) {
// If the tree is empty, create a new node
if (*root == NULL) {
*root = createNode(data);
return;
}
// Check if the data is an operator or operand
if (isdigit(data)) {
// If it's an operand, insert it as the left child of the root node
insertNode(&(*root)->left, data);
} else {
// If it's an operator, insert it as the right child of the root node
insertNode(&(*root)->right, data);
}
}
// Function to evaluate the expression represented by the binary tree
int evaluateExpression(TreeNode *root) {
// If the root node is NULL, return 0
if (root == NULL) {
return 0;
}
// If the root node is an operand, return its value
if (isdigit(root->data)) {
return root->data - '0';
}
// If the root node is an operator, evaluate the expression for the left and right subtrees
int left = evaluateExpression(root->left);
int right = evaluateExpression(root->right);
// Perform the operation and return the result
if (root->data == '+') {
return left + right;
} else if (root->data == '-') {
return left - right;
} else if (root->data == '*') {
return left * right;
} elseif (root->data == '/') {
return left / right;
} else if (root->data == '%') {
return left % right;
}
// If the operator is not recognized, return 0
return 0;
// Function to free the memory used by the tree
void freeTree(TreeNode *root) {
if (root == NULL) {
return;
}
freeTree(root->left);
freeTree(root->right);
free(root);
}
法二:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Define a node in the binary tree
typedef struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// Create a new tree node
TreeNode* createNode(char data) {
TreeNode* node = (TreeNode*) malloc(sizeof(TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
// Create the binary tree from the given expression
TreeNode* createTree(char* expression) {
// Stack for holding the tree nodes
TreeNode* stack[strlen(expression)];
int stackPointer = -1;
// Iterate through the expression
for (int i = 0; i < strlen(expression); i++) {
// Get the current character
char ch = expression[i];
// If it is a number or a variable, create a new node and push it to the stack
if (isdigit(ch) || isalpha(ch)) {
stack[++stackPointer] = createNode(ch);
}
// If it is an operator, create a new node and set its left and right children to be the last two nodes on the stack
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
TreeNode* operatorNode = createNode(ch);
operatorNode->right = stack[stackPointer--];
operatorNode->left = stack[stackPointer--];
stack[++stackPointer] = operatorNode;
}
}
// The final node on the stack is the root of the tree
return stack[stackPointer];
}
// Evaluate the value of the tree
int evaluateTree(TreeNode* root) {
// If the node is a leaf, return its value
if (!root->left && !root->right) {
if (isdigit(root->data)) {
return root->data - '0';
}
else {
// Assume that the node is a variable with a value of 10
return 10;
}
}
// If the node is an operator, evaluate its left and right children and perform the corresponding operation
else {
int leftValue = evaluateTree(root->left);
int rightValue = evaluateTree(root->right);
if (root->data == '+') {
return leftValue + rightValue;
}
else if (root->data == '-') {
return leftValue - rightValue;
}
else if (root->data == '*') {
return leftValue * rightValue;
}
else if (root->data == '/') {
return leftValue / rightValue;
}
}
}
int main() {
// Create the tree from the expression
char* expression = "4*5-6/2+3";
TreeNode* root = createTree(expression);
// Evaluate the tree and print the result
int result = evaluateTree(root);
printf("Result: %d\n", result);
return 0;
}
此代码首先使用 createTree 函数从给定表达式创建二叉树。然后,它使用 evaluateTree 函数遍历树并计算表达式。最后,它将结果打印到控制台。
您可以通过使用表达式“4*5-6/2+3”编译和运行该程序来测试该程序。结果应为 11。