[英]Evaluating expression trees in C++ using inordertraversal
This problem is given in daily coding problem #50. 这个问题在每日编码问题#50中给出。 Suppose an arithmetic expression is given as a binary tree. 假设算术表达式作为二叉树给出。 Each leaf is an integer and each internal node is one of '+', '−', '∗', or '/'. 每个叶子是一个整数,每个内部节点是“ +”,“-”,“ *”或“ /”之一。
Given the root to such a tree, write a function to evaluate it. 给定这样一棵树的根,编写一个函数对其求值。
For example, given the following tree : 例如,给定以下树:
*
/ \
+ +
/ \ / \
3 2 4 5
You should return 45, as it is (3 + 2) * (4 + 5). 您应该返回45,因为它是(3 + 2)*(4 + 5)。
I first thought okay, well why don't I get a vector for the inordertraversal representation of this tree and go from there. 我首先想到了好,那为什么不为该树的有序遍历表示取一个矢量,然后从那里去。 I got a bit stuck and glanced at a solution online. 我有点卡住了,瞥了一眼在线解决方案。 I was able to understand it and reproduce it but I am not satisfied with that. 我能够理解并复制它,但对此我并不满意。
What I have so far is a inordertraversal representation of this tree in a vector: [3, +, 2, *, 4, +, 5]. 到目前为止,我所得到的是向量中这棵树的有序遍历表示形式:[3,+,2,*,4,+,5]。
I want to evaluate this from here but I am a bit stuck on the logic. 我想从这里开始对此进行评估,但我在逻辑上有些卡住。
Here is the code I have thus far that does not work. 这是我到目前为止无法使用的代码。 Note that binary_tree_calculate2
is the function I am trying to work on. 请注意, binary_tree_calculate2
是我尝试使用的函数。
// Daily coding problem #50
// This problem was asked by Microsoft.
// Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of
// '+', '−', '∗', or '/'.
// Given the root to such a tree, write a function to evaluate it.
// For example, given the following tree :
// *
// / \
// + +
// / \ / \
// 3 2 4 5
// You should return 45, as it is (3 + 2) * (4 + 5).
#include <cctype>
#include <iostream>
#include <cstring>
#include <utility>
#include <vector>
#include <string>
struct TreeNode
{
std::string val;
std::unique_ptr<TreeNode> left = nullptr;
std::unique_ptr<TreeNode> right = nullptr;
TreeNode(std::string x, std::unique_ptr<TreeNode> &&p = nullptr, std::unique_ptr<TreeNode> &&q = nullptr) :
val(x),
left(std::move(p)),
right(std::move(q)){}
};
int get_num(std::string c)
{
return std::stoi(c);
}
auto inordertraversal(std::unique_ptr<TreeNode>& root)
{
std::vector<std::string> res;
if (!root)
return res;
auto left = inordertraversal(root->left);
auto right = inordertraversal(root->right);
res.insert(res.end(), left.begin(), left.end());
res.push_back(root->val);
res.insert(res.end(), right.begin(), right.end());
}
int binary_tree_calculate1(std::unique_ptr<TreeNode>& root)
{
if (!root)
return 0;
if (!root->left && !root->right)
return get_num(root->val);
int l = binary_tree_calculate1(root->left);
int r = binary_tree_calculate1(root->right);
if (root->val == "+")
return l + r;
if (root->val == "-")
return l - r;
if (root->val == "*")
return l * r;
return l/r;
}
int binary_tree_calculate2(std::unique_ptr<TreeNode>& root)
{
auto tree_node = inordertraversal(root);
int result = 0;
for (int i = 0; i < tree_node.size(); ++i)
{
int num = get_num(tree_node[i]);
if (tree_node[i] == "+")
result += num;
if (tree_node[i] == "-")
result -= num;
if (tree_node[i] == "*")
result *= num;
result /= num;
}
return result;
}
int main()
{
std::unique_ptr<TreeNode> root = std::make_unique<TreeNode>("*");
root->left = std::make_unique<TreeNode>("+");
root->left->left = std::make_unique<TreeNode>("3");
root->left->right = std::make_unique<TreeNode>("2");
root->right = std::make_unique<TreeNode>("+");
root->right->right = std::make_unique<TreeNode>("5");
root->right->left = std::make_unique<TreeNode>("4");
std::cout << binary_tree_calculate1(root) << "\n";
std::cout << binary_tree_calculate2(root) << "\n";
std::cin.get();
}
One obvious error is that in binary_tree_calculate2
, you are taking result
and corrupting it with a division at the end: 一个明显的错误是在binary_tree_calculate2
,您正在获取result
并在末尾用除法破坏它:
for (int i = 0; i < tree_node.size(); ++i)
{
int num = get_num(tree_node[i]);
if (tree_node[i] == "+")
result += num;
if (tree_node[i] == "-")
result -= num;
if (tree_node[i] == "*")
result *= num;
result /= num; // <-- What is this line doing?
}
In short, you are missing else
statements: 简而言之,您缺少else
语句:
for (int i = 0; i < tree_node.size(); ++i)
{
int num = get_num(tree_node[i]);
if (tree_node[i] == "+")
result += num;
else
if (tree_node[i] == "-")
result -= num;
else
if (tree_node[i] == "*")
result *= num;
else
result /= num;
}
Note that the assumption is that tree_node[i]
is going to have a mathematical operation symbol, and for division, num
is not 0. 请注意,假设是tree_node[i]
将具有数学运算符号,并且对于除法, num
不为0。
The difference in binary_tree_calculate1
is that a return
is done immediately after each calculation, thus the error doesn't exist in that function. binary_tree_calculate1
的不同之binary_tree_calculate1
在于,每次计算后都会立即return
,因此该函数中不存在错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.