本文介绍了后缀符号来EX pression树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在堆栈(A,2,B等是操作数)为叶节点推送操作数,没有绑定到任何树在任何方向
对于运营商,流行的必要的操作数从堆栈中,创建与操作在顶部的节点,并挂在它下面的操作数,推新节点到堆栈
在推一个压入堆栈
2推入堆栈
在弹出2和A,创建^ -node(A和2所示),将其推堆栈上
在堆栈 推2
按A对堆栈
在弹出一个和2相结合,形成了* -node
等
等
Push A onto the stack
Push 2 onto the stack
Pop 2 and A, create ^-node (with A and 2 below), push it on the stack
Push 2 on stack
Push A on stack
Pop A and 2 and combine to form the *-node
etc.
etc.
有足够的资源如何转换前pression树为后缀符号,它并不难。
There are enough resources on how to convert an expression tree into postfix notation, and it's not that hard.
不过,我要解析后缀EX pression到前pression树。
But I have to parse a postfix expression into an expression tree.
这位前pression是:
The expression is:
一个2 ^ 2 A * B * - B 2 ^ + A B - /
A 2 ^ 2 A * B * - B 2 ^ + A B - /
我真的没有一个线索如何跨preT前pression。是否有人对如何PROCES这个线索?
I don't really have a clue on how to interpret the expression. Does someone has a clue on how to proces this?
推荐答案创建一个包含节点,可能是树的一部分一叠
Create a stack containing nodes that could be part of a tree
有关数据:
下面是一个 LINQPad 程序,可以与尝试:
Here is a LINQPad program that can be experimented with:
// 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; } }
输出:
更多推荐
后缀符号来EX pression树
发布评论