创建递归二叉树?

发布于 2024-10-01 10:17:37 字数 497 浏览 6 评论 0原文

我有两个堆栈,一个带有操作数,另一个带有运算符。我的问题是将这两个堆栈变成二叉树。

例如,表达式(2+3)*(4-3) 将被翻译为后缀(例如 24+43-*),然后放入两个堆栈 3442*-+ 将是堆栈(顶部分别为 3 和 *)。

现在有了这些堆栈,我需要形成一个二叉树,例如

   *
 +    -
2 3  4 3

有没有办法递归地执行此操作?

现在,我有一个像这样的算法:

  • 创建树的根,将根的值分配给运算符堆栈中的第一个运算符。将左右指针设置为空。

  • 创建右侧节点,如果存在则分配下一个运算符的值,如果不存在则为其分配操作数。然后对左节点执行相同的操作。

我的问题是使其递归,或者让它处理许多不同的情况。

感谢您的帮助。

I have two stacks, one with operands, the other with operators. My problem is turning these two stacks into a binary tree.

For example, the expression (2+3)*(4-3)
will be translated into postfix ( such that 24+43-*) and then put into two stacks
3442 and *-+ will be the stacks (with the tops being 3 and * respectively).

Now with these stacks, i need to form a binary tree like

   *
 +    -
2 3  4 3

Is there a way to do this recursively?

Right now, I have an algorithm like so:

  • Create the root of the tree, assign the value of the root to the first operator in the operator-stack. Set the right and left pointers to null.

  • Create the right node, assign the value of the next operator if it exists, if not assign it an operand. Then do the same for the left node.

My problem is making this recursive, or getting it to handle the many different cases.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弄潮 2024-10-08 10:17:37

假设你只有二元运算符

treeNode createNode(operators, operands) {
  // take first operator and create a new treeNode with it. pop it from the operators stack 

  // if it is the last operator in the list then there should be only two operands left in the operands and you can assign them to the left and right child of the treeNode. return this treeNode.

  // if it is not the last operator then split the operands in two stacks equally
  // leftOperands and rightOperands
  // left child becomes createNode(operators, leftoperands)
  // right child becomes createNode(operators, rightoperands)
  // return this treeNode

}

assuming you have only binary operators

treeNode createNode(operators, operands) {
  // take first operator and create a new treeNode with it. pop it from the operators stack 

  // if it is the last operator in the list then there should be only two operands left in the operands and you can assign them to the left and right child of the treeNode. return this treeNode.

  // if it is not the last operator then split the operands in two stacks equally
  // leftOperands and rightOperands
  // left child becomes createNode(operators, leftoperands)
  // right child becomes createNode(operators, rightoperands)
  // return this treeNode

}
浮华 2024-10-08 10:17:37

递归算法:

  • 找到最高优先级的运算符,
  • 围绕该运算符拆分表达式
  • ,递归地应用于两个部分

Recursive algorithm:

  • find the highest priority operator
  • split the expression around this operator
  • recursively apply on both parts
女皇必胜 2024-10-08 10:17:37

如果您的表达式始终是对称的(运算符每一侧的操作数和运算符数量相同),那么您描述的方法可以正常工作,只需稍作修改:

  1. 创建一个节点,分配运算符堆栈中顶部运算符的值。
  2. 如果运算符堆栈上没有运算符,则从操作数堆栈中弹出2个操作数并将它们分配给左右分支,然后退出
  3. 如果堆栈上有任何运算符,则转到节点的左分支并调用你的算法,然后转到正确的分支并调用你的算法。

(Jan 在他的回答中解释得更清楚......)

if your expression is always symmetrical (the same number of operands and operators on each side of an operator), then the method you describe works fine, with a little modification:

  1. create a node, assign the value of the top operator in the operator stack.
  2. if there are no operator left on the operator stack, pop the 2 operands from the operands stack and assign them to the left and right branch, then exit
  3. if there are any operator on the stack, go to the left brach of your node and call your algorithm, then go to the right branch and call your algorithm.

(Jan explained it so much clearer in his answer...)

最笨的告白 2024-10-08 10:17:37

一般来说,没有办法做到这一点。 “1 2 3 4”“* + /”是指“1 2 3 4 * + /”(即“1 / (2 + 3 * 4)”)还是“1 2 * 3 + 4 /”,(即“(1 * 2 + 3) / 4”)。

In general there isn't a way of doing this. Does "1 2 3 4" "* + /" mean "1 2 3 4 * + /" (that is, "1 / (2 + 3 * 4)") or "1 2 * 3 + 4 /" , (that is "(1 * 2 + 3) / 4").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文