public ArrayList Traverse(Ordering order) { ArrayList trvlst = new ArrayList(); SimpleBinaryTreeNode.Traverse(ref trvlst, this, order, null); return(trvlst); }
public ArrayList Traverse(Ordering order, TraverseEvent act, NodeEventArgs args)//trvlist = traversal list, rtnd = root node. { ArrayList trvlst = new ArrayList(); SimpleBinaryTreeNode.Traverse(ref trvlst, this, order, act, args); return(trvlst); }
public static void CalTest1() { string exp = "1 + 3 - (6 - 3 *7)/3"; ArrayList posfix = SSCLCLTR.ConvertInfixToPosfixTest(exp); SimpleBinaryTreeNode exptree = SSCLCLTR.MakeExpTree(posfix); exptree.Traverse(SimpleBinaryTreeNode.PostOrder, (SimpleBinaryTreeNode nd) => { Console.Write(nd.Data); }); Console.WriteLine(); exptree.Traverse(SimpleBinaryTreeNode.InOrder, (SimpleBinaryTreeNode nd) => { Console.Write(nd.Data); }); Console.WriteLine(); exptree.Traverse(SimpleBinaryTreeNode.PreOrder, (SimpleBinaryTreeNode nd) => { Console.Write(nd.Data); }); Console.WriteLine(); Console.WriteLine(SSCLCLTR.EvaluateExpTreeP2(exptree)); Console.WriteLine(SSCLCLTR.EvaluateExpTreeP(exptree)); //EvaluateExpTreeP에 의해서 exptree가 수정되기 때문에 P 쓰고 P2 쓰면 에러가 났던 것. //exptree가 수정되지 않는 EvaluateExpTreeP를 생각해봐야... Console.WriteLine(1 + 3 - (6 - 3 * 7) / 3); }
public static void SBT_Test6() { var intt = typeof(int); SBTnodeI nd1 = new SBTnodeI(intt, 1); SBTnodeI nd2 = new SBTnodeI(intt, 2); SBTnodeI nd3 = new SBTnodeI(intt, 3); SBTnodeI nd4 = new SBTnodeI(intt, 4); nd1.Left = nd2; nd1.Right = nd3; nd2.Left = nd4; ArrayList trvlst; trvlst = nd1.Traverse(SBTnodeI.InOrder, (SimpleBinaryTreeNode node) => { Console.WriteLine(node.Data); }); }
public static void SBT_Test5() { var intt = typeof(int); SBTnodeI nd1 = new SBTnodeI(intt, 1); SBTnodeI nd2 = new SBTnodeI(intt, 2); SBTnodeI nd3 = new SBTnodeI(intt, 3); SBTnodeI nd4 = new SBTnodeI(intt, 4); nd1.Left = nd2; nd1.Right = nd3; nd2.Left = nd4; ArrayList trvlst; trvlst = nd1.Traverse(SBTnodeI.InOrder); foreach (int e in trvlst) { Console.WriteLine(e); } }
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]); }//...책에 있는 방식이 더 간단한데? 애미 ㅋㅋㅋㅋ