static public SimpleBinaryTreeNode MakeExpTree(ArrayList posfix) { if (posfix.Count <= 0) { throw new ArgumentNullException("The array list for posfix expression is empty."); } var stack = new SimpleLinkedListBasedStack <SimpleBinaryTreeNode>(); int pl = posfix.Count; foreach (dynamic e in posfix) { if (e is double) { stack.Push(new SimpleBinaryTreeNode(e)); } else if (e is SOpr) { if (stack.Count < 2) { throw new ArithmeticException("At least two numbers are needed to make a expression subtree."); } var right = stack.Pop(); var left = stack.Pop(); stack.Push(new SimpleBinaryTreeNode(e, left, right)); } } return(stack.Pop()); }
static public double EvaluateExpTreeP(SimpleBinaryTreeNode rtnd) { var numbuf = new SimpleLinkedListBasedStack <double>(); ArrayList trvlist = new ArrayList(); SimpleBinaryTreeNode.Traverse(ref trvlist, rtnd, SimpleBinaryTreeNode.PostOrder, (SimpleBinaryTreeNode nd) => { if ((nd.Data is SOpr) && (nd.Left.DataType == typeof(double)) && (nd.Right.DataType == typeof(double))) { double a = (double)trvlist[trvlist.Count - 2]; //stack 쓰는게 더 적절하지 않았겠냐..? 아 trvlist의 작성이... double b = (double)trvlist[trvlist.Count - 1]; double r = ((SOpr)(nd.Data)).oprt.Invoke(a, b); nd.Set(r); trvlist.RemoveAt(trvlist.Count - 1); trvlist.RemoveAt(trvlist.Count - 1); } }); return((double)trvlist[0]); }//...책에 있는 방식이 더 간단한데? 애미 ㅋㅋㅋㅋ