gpt4 book ai didi

algorithm - 表达式树的后缀表示法

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:16:02 27 4
gpt4 key购买 nike

关于如何将表达式树转换为后缀表示法的资源足够多,而且并不难。

但我必须将后缀表达式解析为表达式树。

表达式为:

A 2 ^ 2 A * B * - B 2 ^ + A B - /

我真的不知道如何解释这个表达式。有人知道如何处理这个吗?

最佳答案

创建一个包含可能是树的一部分的节点的堆栈

  1. 将操作数压入堆栈(A、2、B 等是操作数)作为叶节点,不绑定(bind)到任何方向的任何树
  2. 对于运算符,将必要的操作数弹出栈,创建一个节点,运算符在顶部,操作数卡在它下面,将新节点压入堆栈

对于您的数据:

  1. 将A压入堆栈
  2. 将 2 压入堆栈
  3. 弹出2和A,创建^-节点(下面有A和2),将其压入堆栈
  4. 将 2 压入堆栈
  5. 将 A 压入堆栈
  6. 弹出A和2并组合形成*-node
  7. 等等
  8. 等等

tree structure

这是一个LINQPad可以试验的程序:

// Add the following two using-directives to LINQPad:
// System.Drawing
// System.Drawing.Imaging

static Bitmap _Dummy = new Bitmap(16, 16, PixelFormat.Format24bppRgb);
static Font _Font = new Font("Arial", 12);

void Main()
{
var elementsAsString = "A 2 ^ 2 A * B * - B 2 ^ + A B - / 2 ^";
var elements = elementsAsString.Split(' ');

var stack = new Stack<Node>();
foreach (var element in elements)
if (IsOperator(element))
{
Node rightOperand = stack.Pop();
Node leftOperand = stack.Pop();
stack.Push(new Node(element, leftOperand, rightOperand));
}
else
stack.Push(new Node(element));

Visualize(stack.Pop());
}

void Visualize(Node node)
{
node.ToBitmap().Dump();
}

class Node
{
public Node(string value)
: this(value, null, null)
{
}

public Node(string value, Node left, Node right)
{
Value = value;
Left = left;
Right = right;
}

public string Value;
public Node Left;
public Node Right;

public Bitmap ToBitmap()
{
Size valueSize;
using (Graphics g = Graphics.FromImage(_Dummy))
{
var tempSize = g.MeasureString(Value, _Font);
valueSize = new Size((int)tempSize.Width + 4, (int)tempSize.Height + 4);
}

Bitmap bitmap;
Color valueColor = Color.LightPink;
if (Left == null && Right == null)
{
bitmap = new Bitmap(valueSize.Width, valueSize.Height);
valueColor = Color.LightGreen;
}
else
{
using (var leftBitmap = Left.ToBitmap())
using (var rightBitmap = Right.ToBitmap())
{
int subNodeHeight = Math.Max(leftBitmap.Height, rightBitmap.Height);
bitmap = new Bitmap(
leftBitmap.Width + rightBitmap.Width + valueSize.Width,
valueSize.Height + 32 + subNodeHeight);

using (var g = Graphics.FromImage(bitmap))
{
int baseY = valueSize.Height + 32;

int leftTop = baseY; // + (subNodeHeight - leftBitmap.Height) / 2;
g.DrawImage(leftBitmap, 0, leftTop);

int rightTop = baseY; // + (subNodeHeight - rightBitmap.Height) / 2;
g.DrawImage(rightBitmap, bitmap.Width - rightBitmap.Width, rightTop);

g.DrawLine(Pens.Black, bitmap.Width / 2 - 4, valueSize.Height, leftBitmap.Width / 2, leftTop);
g.DrawLine(Pens.Black, bitmap.Width / 2 + 4, valueSize.Height, bitmap.Width - rightBitmap.Width / 2, rightTop);
}
}
}

using (var g = Graphics.FromImage(bitmap))
{
float x = (bitmap.Width - valueSize.Width) / 2;
using (var b = new SolidBrush(valueColor))
g.FillRectangle(b, x, 0, valueSize.Width - 1, valueSize.Height - 1);
g.DrawRectangle(Pens.Black, x, 0, valueSize.Width - 1, valueSize.Height - 1);
g.DrawString(Value, _Font, Brushes.Black, x + 1, 2);
}

return bitmap;
}
}

bool IsOperator(string s)
{
switch (s)
{
case "*":
case "/":
case "^":
case "+":
case "-":
return true;

default:
return false;
}
}

输出:

LINQPad output

关于algorithm - 表达式树的后缀表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/423898/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com